Arduino Programming

 There are 4 tasks that will be explained on this page:

  • Input devices:
    • Interface a potentiometer analog input to the MakerUNO board and measure/show its signal in serial monitor Arduino IDE
    • Interface an LDR to the MakerUNO board and measure/show its signal in serial monitor Arduino IDE
  • Output devices:
    • Interface 3 LEDs (Red, Yellow and Green to the MakerUNO board and program it to perform something (fade or flash, etc)
    • Include the pushbutton on the MakerUNO board to stop/start the task above
For each of the tasks I will describe the following:
  1. The code that I have used and an explanation of the code. The code is in writable format (not an image)
  2. The sources/references that I used to write the code/program.
  3. The problems I encountered and how I fixed them
  4. The evidence that the code/program worked in the form of a video of the executed code
Finally, I will describe:
  • My Learning reflection on the overall Arduino programming activities

Input Devices:

Interface a potentiometer analog input to the MakerUNO board and measure/show its signal in serial monitor Arduino IDE

Code in writable format

Explanation

int sensorValue = 0;


void setup()

{

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

}


void loop()

{

  // read the value from the sensor

  sensorValue = analogRead(A0);

  // turn the LED on

  digitalWrite(13, HIGH);

  // pause the program for <sensorValue> milliseconds

  delay(sensorValue); // Wait for sensorValue millisecond(s)

  // turn LED off

  digitalWrite(13, LOW);

  // pause the program for <sensorValue> milliseconds

  delay(sensorValue); // Wait for sensorValue millisecond(s)

}



Let the initial value of the potentiometer be 0


A0 is used as the input for the potentiometer

13 is used as the output where the LED is










analogRead reads the value of the potentiometer on A0


Using the digitalWrite lines, the brightness of the LED is controlled from HIGH to LOW

Sources:

https://www.youtube.com/watch?v=-EDYMQ9lczA

Problems that I faced:

My serial monitor on the Arduino app did not work for me and I could not show it on this blog post. I tried to look up the issue but I could not find anything.

Video evidence:

Interface an LDR to the MakerUNO board and measure/show its signal in serial monitor Arduino IDE

Code in writable format

Explanation

int light;

void setup() {

Serial.begin(9600);

}


void loop() {

  light= analogRead(A0);

 if(light<50){

   digitalWrite(13, LOW); 

 }else{

   digitalWrite(13, HIGH);

 }

  Serial.println(light);

  delay(0);

}

This code is similar to the previous one with the potentiometer, except that the input is from the LDR instead


Problems I faced:

Again I could not get the serial monitor to work. Also the 

Video Evidence:


Output Devices

Interface 3 LEDs (Red, Yellow and Green to the MakerUNO board and program it to perform something (fade or flash, etc)

Code in writable format

Explanation

int brightness = 0; 

void setup() {

  // put your setup code here, to run once:

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}


void loop() {

  // put your main code here, to run repeatedly:

for (brightness = 0; brightness <= 255; brightness ++);

analogWrite(9, brightness); 

delay(150);

analogWrite(10, brightness); 

delay(150);

analogWrite(11, brightness); 

delay(150);

{

for (brightness = 255; brightness >= 0; brightness --);

analogWrite(9, brightness); 

delay(150);

analogWrite(10, brightness); 

delay(150);

analogWrite(11, brightness); 

delay(150);

}


}

Using pins 9, 10 and 11 as the output for the LEDs



Using the void loop() line, a loop was created where each LED is set to 0 brightness at first. The LED will then raise its brightness to its maximum of 255 within 150ms




This next block of code does the same thing as the previous one but instead, it lowers the brightness of each LED from 255 to 0


This ultimately makes it so that the LEDs flash on and off consistently in a loop.


Sources:

https://www.youtube.com/watch?v=yyG0koj9nNY
https://www.youtube.com/watch?v=X8dHbdhnGKY

Problems I faced:

I was quite unsure about the setup for the Arduino board that was meant to be made. after trying for a few times I managed to figure it out.

Video Evidence:

Include the pushbutton on the MakerUNO board to stop/start the task above

Code in writable format

Explanation

int brightness = 0; 

void setup() {

  // put your setup code here, to run once:

Serial.begin(9600);

pinMode(2, INPUT_PULLUP);

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}


void loop() {

int sensorVal = digitalRead(2);

  //print out the value of the pushbutton

  Serial.println(sensorVal);

  // put your main code here, to run repeatedly:

   if (sensorVal == HIGH) {

    digitalWrite(9,LOW);

    digitalWrite(10,LOW);

    digitalWrite(11,LOW);

for (brightness = 0; brightness <= 255; brightness ++);

analogWrite(9, brightness); 

delay(150);

analogWrite(10, brightness); 

delay(150);

analogWrite(11, brightness); 

delay(150);

 } else {

    for (int i=0; i < 5; i++)

    {

      digitalWrite(9,HIGH);

      delay(500);

      digitalWrite(9,LOW);

      delay(500);

      digitalWrite(10,HIGH);

      delay(500);

      digitalWrite(10,LOW);

      delay(500);

      digitalWrite(11,HIGH);

      delay(500);

      digitalWrite(11,LOW);

      delay(500);

  

{

for (brightness = 255; brightness >= 0; brightness --);

analogWrite(9, brightness); 

delay(150);

analogWrite(10, brightness); 

delay(150);

analogWrite(11, brightness); 

delay(150);

}

    }

}}

This code does the same thing as the previous one, except that it uses the line of “pinMode(2, INPUT_PULLUP)” to allow the button on the Arduino board to be used to activate the code. This is because the button is connected to pin 2, which is how we use the button in the code.


Other than this the rest of the code is similar to the one above. The only thing that changed was the lighting code for the LEDs such that the pattern of flashing is different.

Sources:

The same ones I used for the previous task

Problems I faced:

Since this task used the same setup as the previous task, I did not have many issues with this task.

Video Evidence:


Learning Reflection on the overall Arduino Programming activities

Overall, my thoughts on these activities are very positive. I think that learning Arduino programming would be very useful for my future endeavours. However, getting used to having to write this much text in a systemic matter was kind of difficult for me. I am used to writing in a more free-form manner, but coding required me to write in a way that doesn't make much sense to me. Though I did get used to it somewhat, I still have much to learn.

The practical we did for Arduino programming was conducted on week 6 and it was a fun activity. It first required us to do 4 types of codes to learn how to use the buzzer on the board to make noise, light up the LEDs on the board, use the button and how to code a servo properly. After this was the actual practical which went not so smoothly. 

During this practical, we were supposed to make a cardboard pegasus and complete a few tasks using it and the Arduino. These tasks being: 
  1. Perform its main function (flapping)
  2. Compact and can easily be moved (no loose pieces)
  3. Reliable and durable (doesn't fall and no parts failing)
  4. Aesthetically pleasing
  5. Can perform another function
We made its wings move by attaching the servo inside of the pegasus and connecting a metal wire to the wings on the inside of the model. After that, we just coded the servo to move back and forth. Afterwards, we wanted the Arduino to make music while the wings were moving. We found a code to make the music work, but this is where the problem started. We could not find a way to make it so that the programs could work concurrently. This is because both programs needed their own "void setup" and "void loop" lines to work. However, Arduino does not allow more than one of each line to exist in the same code. In the end, we could not figure out how to make this work, so we just made it play music first and then flap its wings.

Here are some pictures and videos we took during the practical:


As I had said at the start of this reflection, my thoughts on these Arduino activities have been quite positive. I am looking forward to learning more about this topic and how to implement it better. Well then, until next time!!