Arduino Programming

Arduino Programming

For Arduino Programming, my class was tasked to accomplish 2 things. Firstly, each of us had to complete an individual competency test, and also write a code that would make the wings of a unicorn flap, and a bonus task of combining the flapping with something else. 

To start off, my group used an "Sweep" example code provided by the Arduino Software, then tweaked it a little. We changed the Servo's initial position to from 0 to10 degrees, changed the sweeping angles to go from 10 to 120 degrees and changed the degree increase rate to steps of 10 degrees. We also changed the delay when rotating anti-clockwise to 25ms, and 15ms when rotating clockwise. At first, we set it to have a slower flapping speed, but it was too slow by our lecturer's standards, so we increased it after.

Finalized Code can be downloaded here


Prototype Design of our Unicorn


We ran into 2 main problems during the Arduino Practical.

Firstly, when we attempted to do the bonus requirement, only then we realized that combining several different tasks for the Arduino into a single page of code was a lot harder than we thought it would. We tried to combine the wing flapping action with some music that would play when the button on the Arduino board was pressed. When that didn't work out, we moved on and tried to program an 'overclock' function for the wing flapping action; when the button on the Arduino board is pressed, the flapping speed of the wings are increased. However, that did not work too, and therefore we were only able to make the wings flap for our practical.

Secondly, when we were trying to implement the new changes mentioned above, the rotational direction of the servo would bizarrely change from our original anti-clockwise direction to a clockwise direction, which meant that it would rub against the unicorn's body, damaging it. We issued a quick fix by changing the rotation degree of 60 degrees to -60 degrees so that the servo would still swing anti-clockwise and wouldn't slam into the body of the Unicorn.

To conclude, we found out the hard way that lines of code would not perform as we thought they would on the Arduino due to our lack of experience and unfamiliarity with coding, which could get every frustrating, especially when realizing that it would even affect the previous lines of untouched code,



We named him Jeff



Jeff a real one fr


TinkerCad (Individual)

We also utilized TinkerCad, a web-based simulation to develop and test Arduino Code before executing the actual code on an actual Arduino board. External Components like LED Lights, DC Motors and Buttons etc. can be attached to the simulated Arduino Board to see how they would operate in real life. Here are the 4 activities we used it for:


1. Potentiometer Analog:








Code Used:

// C++ code
//

int sensorValue = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  
}

void loop()
{
  sensorValue = analogRead(A0);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(sensorValue); // Wait for 1000 millisecond(s)
  digitalWrite(LED_BUILTIN, LOW);
  delay(sensorValue); // Wait for 1000 millisecond(s)
}




2. Light Dependent Resistor (LDR):







Code I used: 

// C++ code

//

void setup()

{

  pinMode(A0,INPUT); 

  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);

}

void loop()

{

  int LDRStatus = analogRead(A0);

  Serial.println(LDRStatus);

  digitalWrite(LED_BUILTIN, HIGH);

}


Here's a link to the Simulation: https://www.tinkercad.com/things/1qyEXEilOCw-activity-1b/editel


3. Programming 3 LEDs to make them do something:





Code I Used:

void setup()

{

  pinMode(11, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(13, OUTPUT);

}


void loop()

{

  digitalWrite(11, HIGH);

  delay(5000);

  digitalWrite(11, LOW);

  delay(0);

  digitalWrite(12, HIGH);

  delay(1500);

  digitalWrite(12, LOW);

  delay(0);

  digitalWrite(13, HIGH);

  delay(5000);

  digitalWrite(13, LOW);

  delay(0);

    

}



4. Programming a DC Motor to turn on and off at the push of a button:






Code I used: 

// C++ code

//

int ButtonState = 1; // Define button's state


void setup()

{

  pinMode(3, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}


void loop()

{

  ButtonState = digitalRead(3);

  if (ButtonState == HIGH){ // If button's state is high

    digitalWrite(LED_BUILTIN, HIGH); // motor's state is high so it starts up

  } else {

    digitalWrite(LED_BUILTIN, LOW); // else, motor stops 

  }

  delay(200); // Set some delay to show motor starting and stopping 

}


Here's a link to this Simulation: https://www.tinkercad.com/things/ekJ7N9QGHe3-activity-2b/editel


What I've Learnt: 

Through these 4 activities, I have learnt that interfacing input devices into an Arduino Board allows these input devices to collect data, and the Arduino Board sends this data over to an output device that is also interfacing into it, and this output device will react accordingly to what it is and what data that it has been provided with.

For example, the input device could be a infra-red motion sensor, which is hooked up to the Arduino Board. If this motion sensor detects any motion, it would send a signal through the Arduino Board, and to the output device, which in this case could be a Servo Motor, and once the servo motor receives the signal from the motion sensor, depending on the written code, it would perform its intended action.


My Reflection:

Overall, this Arduino programming part of the module has been a meaningful experience since it allowed us to demonstrate our Arduino programming capabilities and gave us a chance to express our creativity when trying to program the unicorn to flap and perform different features at once.

However, as mentioned earlier, getting the Arduino board to perform several functions all as once is very hard to code as the line of codes many not have the expected outcome shown on the Arduino board.

Also, I feel that our final product turned out better than expected since we managed to achieve the goal of making the unicorn wings flap by itself. Although there were mistakes made during the practical, we managed to learn from our mistakes and overcame them with some problem-solving skills.

Comments

Popular posts from this blog

Project Development

3D Printing