Arduino Programming

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

  1. Input devices:

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

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

  1. Output devices:

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

  2. 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.



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

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

int sensorvalue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);

}

 

void loop()

{

  sensorvalue = analogRead(A0);

  digitalWrite(LED_BUILTIN, HIGH);

  Serial.print(",");

  Serial.println(sensorvalue);

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

  digitalWrite(LED_BUILTIN, LOW);

  Serial.print(",");

  Serial.println(sensorvalue);

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

}

-Set sensor value to 0


-set PIN A0 and LED as output

 

 

-establish connection between Arduino board and computer

 

 

 

-define variable sensorvalue

 

-show sensorvalue in serial monitor



  1. Below are the hyperlink to the sources/references that I used to write the code/program.

https://youtu.be/-EDYMQ9lczA



  1. Below are the problems I have encountered and how I fixed them.

For this first code, everything went pretty smooth without any hiccups along the way



  1. Below is the short video as the evidence that the code/program work.





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

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

int light = 0;

 

void setup()

{

  Serial.begin(9600);

}

 

void loop()

{ light = analogRead(A0);

 Serial.println(light);

 delay(100);

-set variable light as 0

 

 

 

 

 

 

-define variable light

-print values in the serial monitor



  1. Below are the hyperlink to the sources/references that I used to write the code/program.

https://create.arduino.cc/projecthub/electronicsfan123/interfacing-arduino-uno-with-ldr-8760ba



  1. Below are the problems I have encountered and how I fixed them.

For this activity, it went pretty smooth as well since there was a video reference that I could follow. However, there were a few issues I had with the serial monitor but after watching the video multiple times and asking my friends, I managed to get that sorted


  1. Below is the short video as the evidence that the code/program work.







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

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

int animationSpeed = 0;

 

void setup()                        // setup function

{

         

  pinMode(13, OUTPUT);            

  pinMode(9, OUTPUT);             

  pinMode(5, OUTPUT);            

   

 

  Serial.begin(9600);

}

 

void loop()                         // loop function

{

 

 animationSpeed = 400;

 

      digitalWrite(13, HIGH);        // turn on LED

      digitalWrite(9, HIGH);        // turn on LED

      digitalWrite(5, HIGH);        // turn on LED

      delay(animationSpeed);

      digitalWrite(13, LOW);         // turn off LED

      digitalWrite(9, LOW);         // turn off LED

      digitalWrite(5, LOW);         // turn off LED

      delay(animationSpeed);

}

 

 

 

 

 

-set PIN 13, 9 and 5 as output

 

 

 

 

-establish connection between board and computer

 

 

 

-set animation speed to 400

 

 

-set LEDs to high mode

 

 

-delay by 400 ms

-set LEDs to low mode

 

 

-delay by 400ms



  1. Below are the hyperlink to the sources/references that I used to write the code/program.

https://youtu.be/X8dHbdhnGKY



  1. Below are the problems I have encountered and how I fixed them.

This activity was quite difficult compared to the previous 2 as there were so many issues such as the LEDs not lighting up or only 2 would light up or 1 would be lit up but dimmer than the rest. We first decided to check if there was anything wrong with the physical wiring done. Once nothing wrong was found there, we decided to check back on our code and found the issue. Once we amended that, the issue was solved and all 3 lights were working fine.



  1. Below is the short video as the evidence that the code/program work.







Output devices: Include pushbutton to start/stop the previous task 

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

int buttonState = 0;                // set value for buttonState

void setup()                        // setup function

{

         

  pinMode(13, OUTPUT);             

  pinMode(9, OUTPUT);            

  pinMode(5, OUTPUT);            

   

  pinMode(2, INPUT_PULLUP);         // initialize push button

  Serial.begin(9600);

}

 

void loop()                         // loop function

{

  if (digitalRead(2) == LOW){

    while(digitalRead(2) == LOW){}

  

    if (buttonState == 1)      

    {

      digitalWrite(13, HIGH);        // turn on LED

      digitalWrite(9, HIGH);        // turn on LED

      digitalWrite(5, HIGH);        // turn on LED

      buttonState = 0;

    }

    else

    {

      digitalWrite(13, LOW);         // turn off LED

      digitalWrite(9, LOW);         // turn off LED

      digitalWrite(5, LOW);         // turn off LED

      buttonState = 1;

  }

 }

}

-declare buttonstate is 0

 

 

 

 

-set pin 13, 9 and 5 as output for LEDs

 

 

 

 

-set pin 2 as input for button

 

-establish connection between board and computer

 

 

 

-code will run only when button is pushed

 

 

-if button state is 1 turn on LEDs and set button state to 0

 

 

 

 

 

-Else turn off LEDs and set button state to 1

 

 



  1. Below are the hyperlink to the sources/references that I used to write the code/program.

https://create.arduino.cc/projecthub/krivanja/working-with-an-led-and-a-push-button-71d8c1

            LEDs & Breadboards With Arduino in Tinkercad

  1. Below are the problems I have encountered and how I fixed them.

For this activity, the only issue I faced was that the button could not work with the rest of the code. I realized that it was something wrong with the code but did not know how to amend it but after referring back to the videos provided by Brightspace, I finally managed to make it work.


  1. Below is the short video as the evidence that the code/program work.





Below is my Learning Reflection on the overall Arduino Programming activities.

When I first heard that we had to do coding, I was quite concerned as I was very new to the programming side of things and I knew that this was going to be the hardest thing to pick up for this semester. However, with every new taught, there would be enough information on Brightspace to make learning anything new simpler. Therefore, Mr Chua went through the slides with us so that we would be well equipped so that we could use this knowledge in the future. While reading through the slides and listening to Mr Chua, I realised how difficult this would be as I really couldn't understand what was bring taught. After going through the slides, Mr Chua then provided each group with 2 Arduino kits, each containing a breadboard, resistors, a lot of wires and the Arduino 'thing' itself. To help us familiarize ourselves with Arduino, there were a couple of activities that we had to do so that we can get the hang of things. 


We were then split into smaller groups within our group as we had 2 Arduino kits. Since I wasn't that confident with doing the coding, I decided to handle the wiring while my partner handled the coding as he was quite proficient at it. While doing the activities, I would often ask my partner what he was doing as I wanted to get an idea of what he was doing. He explained to me patiently what each line of code meant and this made me understand Arduino a lot better. Since I was handling the wiring, I didn't really get a sense of how difficult coding could be so I decided to ask my partner if I could do the coding for 1 activity. While I was trying to code, it was then I realised how difficult coding could be. My partner obviously sensed my struggles and offered to give me some verbal advice by my side. After that 1 activity, I decided to let him do the rest of the coding as I couldn't see myself doing the coding for the rest of the activities. After a long time, we finally managed to complete all 4 activities and I couldn't be much happier.


Overall, Arduino was definitely harder than anything I have learnt throughout ICPD and CPDD. However with that being said, it was definitely fun and interesting as it really opened my eyes to the programming side of things which I otherwise would not have looked. Modules like ICPD and CPDD have really expanded my knowledge of engineering through things like Fusion 360, 3D Printing and Laser cutting, now adding on Arduino. With all this, I can't wait to put all this to use in my future modules!



Comments