Arduino Programming

 Hi everyone!! 🤩


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

  1. Input devices:

  • Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.

  • Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE

  1. Output devices:

  • Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)​

  • Include the pushbutton on the MakerUno board to start/stop part 2.a. above

For each of the tasks, I will describe:

  1. The program/code that I have used and 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 video of the executed program/code.

Finally, I will describe:

  1. My Learning reflection on the overall Arduino programming activities.


1. Input Devices

Activity

Code/Program in writable format 

Explanation 

Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.







// C++ code

//

int sensorValue = 0;

 

void setup()

{

  Serial.begin(9600);

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

}

 

void loop()

{

  // read the value from the sensor

  sensorValue = analogRead(A0);

  // turn LED off

  digitalWrite(13, HIGH);

  // pause the program for <sensorValue> milliseconds

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

  Serial.println(sensorValue);

  // turn LED off

  digitalWrite(13, LOW);

  // pause the program for <sensorValue> milliseconds

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

}


Serial.begin(9600);  helps establish serial communication between Arduino Board and other devices. 


Serial.println(sensorVal);   It displays the value of sensorVal if it is pressed.



pinMode(13, OUTPUT); means that we are setting PIN 13 as the output 



For this code, if SensorVal is high, PIN 13 would be low. ELSE will be HIGH. 

Source: 

Tinkercad Website: https://www.tinkercad.com/things/9yD4fG1l1Dq

Brightspace video: https://youtu.be/-EDYMQ9lczA 

Troubleshooting: To be honest, when the LDR did not blink, I started panicking thinking that the code that I found was incorrect. However, after meddling around with the board and the wires, it turns out that it was just the wires that were not attached properly. Additionally, I also had some issues with the potentiometer as I had to turn it multiple times just to make the LDR light up. Other than that, all was good 😁



Here are some photos of the process: 



Here is the video of the final result: 





Activity

Code/Program in writable format 

Explanation

Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE



int sensorvalue=0;

void setup()

{

  pinMode(A0,INPUT);

  Serial.begin(9600);

  pinMode(10,OUTPUT);

 

}

 

void loop()

{

  sensorvalue=analogRead(A0);

  Serial.println(sensorvalue);

  analogWrite(10,map(sensorvalue,0,1023,255,0));

  delay(100);

 

}

 

 

 

///////////////////////////////////

/// if we don't want to use map functin....

 

/*

 

int sensorvalue=0;

int y,x;

float rv;

void setup()

{

  pinMode(A0,INPUT);

  Serial.begin(9600);

  pinMode(10,OUTPUT);

 

}

 

void loop()

{

  sensorvalue=analogRead(A0);

  Serial.println(sensorvalue);

  y = (255.0/1023.0)*sensorvalue;

  Serial.println(y);

  analogWrite(10,y);

  rv=(5.0/255)*y;

  Serial.println(rv);

  delay(100);

 

}

*/

 

 


Serial.begin(9600);  helps establish serial communication between Arduino Board and other devices. 


pinMode(10, OUTPUT); means that we are setting PIN 10 as the output 


Serial.println(sensorVal);   It displays the value of sensorVal if it is pressed.




Source: 

Tinkercad: https://www.tinkercad.com/things/a1mE5uX9hUu 

Troubleshooting: The LED did not light up which again made me panic, however, this time the problem was easier to solve as I made a careless mistake of not adding one wire. 

The position of the LED also had to be switched. Initially, I put the positive side on the left instead of the other way around. 

Overall, it was easier to settle than the previous one ✌️





Here are some pictures of the process:


Here's the video with the final result: 


I filmed it in the dark so that it would be easier to see when the light for the LED is dimmed. As seen from the video, when there is little to no light, the graph decreases sharply and vice versa. 


2. Output 

Activity

Code/Program in writable format 

Explanation

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



void setup()

{

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

  Serial.begin(9600);

}

 

void loop()

{

      digitalWrite(13,HIGH);

        digitalWrite(12,LOW);

        digitalWrite(11,LOW);

   delay(2000);

      digitalWrite(13,LOW);

        digitalWrite(11,HIGH);

        digitalWrite(12,LOW);

  delay(2000);

      digitalWrite(13,LOW);

        digitalWrite(11,LOW);

        digitalWrite(12,HIGH);

  delay(2000);

}


delay(2000): there will be a delay of 2s between each LED lighting up 



Source: 

Tinkercad:  https://www.tinkercad.com/things/kMZoRzl9tj0  


Troubleshooting: This time ALL 3 LEDs did not light up which obviously means something is wrong. 

Again, I switched the positions of the LEDs which somehow worked. The green LED was not working which meant that I had to change the LED. After changing, I realised that the light from the green LED wasn’t as strong as the other.  But , it still lighted up so all good ✨



Here are some photos of the process: 

This is the reference I used from Tinkercad: 




Actual Set-up: 


This is the final result (I filmed it in the dark again cause the green LED is dimmer compared to the rest) :




Activity

Code/Program in writable format 

Explanation

Push button for the above activity 

int LEDdelay = 0;

 

void setup()

{

   //start serial connection

  Serial.begin(9600);

  //configure pin 2 as an input and enable the internal pull-up resistor

  pinMode(2, INPUT_PULLUP);

  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

}

 

void loop()

{

 

  //read the pushbutton value into a variable

  int sensorVal = digitalRead(2);

  //print out the value of the pushbutton

  Serial.println(sensorVal);

  if (sensorVal == LOW) {

 

    LEDdelay = 400;

    digitalWrite(LED_BUILTIN, HIGH);

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

    digitalWrite(LED_BUILTIN, LOW);

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

    digitalWrite(12, HIGH);

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

    digitalWrite(12, LOW);

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

    digitalWrite(11, HIGH);

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

    digitalWrite(11, LOW);

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

}

}


pinMode(10, OUTPUT); means that we are setting PIN 10 as the output 


Serial.begin(9600);  helps establish serial communication between Arduino Board and other devices. 


pinMode(12, OUTPUT); means that we are setting PIN 13 as the output 



For this code, if SensorVal is high, PIN 12 would be low. ELSE will be HIGH. 

Credits go to dylan since I’m using his code so the arrangement for this circuit will differ from the one above 

Read his blog here: https://dylanl21.wixsite.com/cp5070-2022-2b02-gro/post/blog-3-arduino 



Here is the final result: 




My Learning reflection on the overall Arduino programming activities.

Throughout my time doing arduino so far, it was fun yet challenging. It was my first time doing coding (I have absolutely no experience) and the learning packages on brightspace had a lot of content which made it look more intimidating. Not only that, we were left to fend for ourselves as we had to figure everything out ourselves to complete our assignments.But thanks to that, I actually learned a lot about arduino and actually had fun playing around with the circuits during my own time. Doing this blog was also really fun as I discovered that there were many methods that could produce the same results. 

To be honest, this whole arduino experience has been really enriching as it really encouraged me to think broadly on how to find the right code and even how to fix it. We were ‘forced’ to be independent as we had to find out what the mistakes were (if applicable) and fix them ourselves. We couldn’t really rely on our lecturers that much as these activities were mostly OTOT. For me, I was lucky enough as I was tasked with bringing the arduino kits back home and ‘looking after them’ which meant that I could play around with them anytime I wanted to instead of having to rush to finish everything in class. This meant that I could take as long as I wanted to find the right codes 🤡

We were then stretched during our practical session as we had to again find our own codes to make sure that the wings of our unicorn flap. It wasn’t really difficult as we already had some codes on brightspace to get us started (we used the servo). The only issue that we had with our servo was that it was either not working or too strong , meaning that it made it hard for the wings of the unicorn to flap. However, that was just a tiny issue. 

The main challenge came when we had to fulfil the other requirements such as aesthetic and ability to perform other functions. We decided to add in music which ended up being the theme of Super Mario (we got the idea to include music as another group from my class was blasting christmas songs with the arduino kit 😁) It was really difficult to find codes for songs and when we did, we couldn’t combine them to perform 2 functions at once (flapping its wings and background music).  Firzanah and I also wanted to include LED lights on its wings but we couldn’t figure out how to do so with the arduino kit since it was already used for the servo and we had limited time (finding one code was already difficult, finding another would make it worse). We ended up settling with just music and flapping its wings and thanks to Kelvin we managed to produce a favourable result 😁

Here are some photos of the process: 


We intended our unicorn to look like twinkle sparkle from 'My Little Pony' but its obviously very different so please forgive us if we ruined your childhood.

And here's the result with the music:


In conclusion, I genuinely enjoyed the process even though it was mostly stressful cause I was dealing with circuits which I’m actually terrified of (if it can even be considered a fear 😰😭) But, I definitely liked the fact that most of it was OTOT so that we could learn ourselves which made it easier to understand and figure everything out especially since I’m someone who prefers to learn everything herself 🤓 I would definitely like to learn more about arduino in fact maybe I will continue playing around with it during the holidays since I have the kit with me, so that I won’t rely so much on my group mates as I felt that I was mostly clueless even though I knew the basics already. 

But anyways I look forward to learning more!!  

Jiayouz for CRE Test 2 guys and other MSTs if yall have any 🤓🤩


Comments