top of page
Search

Arduino Programming

  • jingyue22
  • Nov 27, 2023
  • 5 min read

Updated: Jan 30, 2024

Arduino Documentation Blog Entry


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

  1. Input devices:

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

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

  4. Output devices:

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

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



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

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

Code/program in writeable format

Explanation of the code

int AnalogPin=0;

void setup()

{

pinMode(A0,INPUT);

Serial.begin (9600);

}

void loop()

{

AnalogPin=analogRead(A0);

Serial.print("Analog Pin A0=");

Serial.println(AnalogPin);

delay(100);

}

Creation of variable 'AnalogPin' .

 

Establish analog pin A0 as an input.

 

Set up connection from Arduino to serial monitor at 9600 baud rate.

 

'analogRead(A0)' command allow Arduino to listen to the pin's state.

 

'Serial.print' command to allow values to be shown on the serial monitor.

 

Delay each reading by 100ms

 

 

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



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

At first, I did not include the command of 'Serial.begin(9600) which resulted in my serial monitor appearing empty when starting up the Arduino. I solved the problem by going back to my code from my documentation to see how is my code different from the one that has serial monitor. I eventually found out quickly I was missing the 'Serial.begin(9600)' command.



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


1b. 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

const int ldrPin = A0;

 

void setup() {

 

Serial.begin(9600);

}

void loop() {

 

int ldrValue = analogRead(ldrPin);

 

Serial.print("LDR Value: ");

 

Serial.println(ldrValue);

Creation of variable ‘ldrPin’ and assigns it to analog pin A0

 

Establishes connection between Arduino and serial monitor at a baud rate of 9600

 

 

The command analog read allows the Arduino to listen to the A0 pin state

 

This command allows the Arduino to display the actual values of the LDR

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

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

One of the problems I faced was that I had diffculty creating the setup for the LDR as this time I had to make use of the breadboard as I had little to no experience dealing with it. I fixed the problem by going to the TinkerCAD website to experiement with the breadboard and my code which greatly helped in helping me to understand and to work around the breadboard.

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



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

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

Code/program in writeable format

Explanation of the code

void setup()

{

  pinMode(13, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);

}

 

void loop(){

  digitalWrite(13, HIGH);

  delay(10);

  digitalWrite(13, LOW);

  delay(10);

 

 

  digitalWrite(8, HIGH);

  delay(100);

  digitalWrite(8, LOW);

  delay(100);

 

 

  digitalWrite(9, HIGH);

  delay(1000);

  digitalWrite(9, LOW);

  delay(1000);

}

 

 

 Establishes pin 8,9 and 13 as output.

 

 


In this instance, the digitalWrite command writes a digital signal to pin 13.

The HIGH or LOW action in the digitalWrite command relates to the lighting up the LED in this case it is LED 13. HIGH to light up said LED and LOW to turn off said LED.

The delay is set up to delay the lighting up and turning off the LED for 10ms

 

 

 

 

The same can be said for the 2 remaining digitalWrite loops for LED 8 and 9 with delays of 100ms and 1000ms respectively

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



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

One of the problems I was faced was an compilation error. It occurred whenever I tried to verify my code. Eventually, through trial and error, I managed to resolve the problem by placing a semicolon on each line of code. The reason why a semicolon have to be put after each line of code is to tell the compiler that it has reached the end of the command.


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





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

  • 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;

void setup()

{

pinMode(2, INPUT_PULLUP);

pinMode(13, OUTPUT);

pinMode(8,OUTPUT);

pinMode(9,OUTPUT);

}

void loop()

{

buttonState = digitalRead(2);

if (buttonState == HIGH) {

digitalWrite(13, LOW);

digitalWrite(8, LOW);

digitalWrite(9, LOW);

}

else {

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

digitalWrite(13, HIGH);

delay(10);

digitalWrite(13,LOW);

delay(10);

digitalWrite(9, HIGH);

delay(100);

digitalWrite(9,LOW);

delay(100);

digitalWrite(8, HIGH);

delay(1000);

digitalWrite(8,LOW);

delay(1000); }

}

}

Creation of variable’buttonState’

 

declaration that PIN 2 is an INPUT and enable the internal pull up resistor. 

 

 

Establishes pin 8,9 and 13 as output

 

 

 

 

Read the state of the pushbutton value

 

If button is pressed, value buttonState is high

 

The digitalWrite command sends a low signal to LED 8, 9 and 13 so LED 8, 9 and 13 turns off

 

In the For loop,

 

‘(int i=0; i < 5; i++)’ command is to establish a temporary variable of i where i starts at 0 and will increase by 1 until i<5. In addition, the i refers to number of times the code in the for loop is going to run for before returning to the void loop.

 

The HIGH or LOW action in the digitalWrite command relates to the lighting up the LED in this case it is LED 13. HIGH to light up said LED and LOW to turn off said LED.

The delay is set up to delay the lighting up and turning off the LED for 10ms

 

 

The same can be said for the 2 remaining digitalWrite loops for LED 8 and 9 with delays of 100ms and 1000ms respectively

 

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



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

One of the problems encountered was that whenever I tried uploading my code to the Arduino board, I would be faced with this message ' Compilation error: expected '}' at end of input ' or this message ' Compilation error: expected declaration before '}' token '. To solve this problem, I just had to place an extra } for the first message and remove the last } for the second message.


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










My Learning Reflection

Before the Arduino and coding asyncronous lesson, I did not understand much about coding. Although I did have some background in the coding language which is C++ before attending the lesson, it has been a long time ago and I had forgotten much of the content. One thing that greatly helped me to complete these activities is TinkerCAD and ChatGPT. TinkerCAD was helpful as at the time of creating the codes, I did not have the physical Arduino board with me so I had to make do with the virtual one on TinkerCAD. ChatGPT has helped me by giving me reference material to be able to complete the codes by myself.


In my opinion, coding is both interesting and frustrating. To me, coding is fascinating as just by typing down sentences, I would be able to control how fast a servo moves or control how fast a LED blinks. In addition, coding to me feels very similar to mathematics as both topics revolve around being very systematic and very logic based. Coding is like writing a theorem in math. An example would be the command of int buttonState = 0; which allows the creation of the variable buttonState in c++ which is very similar in math where you have to state a variable like 'let buttonState=x'. To add on, another similarity of math and Arduino programming is that both have their own language where Arduino speaks in terms of C++ and math in terms of mathematical notations.


On the filp side, coding is frustrating as sometimes the codes just do not work out due to the numerous errors that pop up. Some of the errors include Expected Unqualified Id error and function definition is not allowed here before ‘}’ token error. I had to turn to internet sites such as StackOverflow and Arduino Forum to resolve such error but sometimes I don't even know what the people in the Forum are talking about. The absolute worst thing that can happen is that even if the code did work out, it does not do what you want so you have to edit the code again and fix the errors. Then if it does not work out, you have to repeat the vicious cycle.


Although such a problem is very tiresome, it teaches me patience and discipline to work the problem until it is done. In addition, during these activities, it has taught how to use the sensors and the analog pins. This will come into great use especially for our CPDD teamaker project as my group plan to attach temperature sensors and level sensors to our Arduino to sense the temperature in the tea tank and level in the cup to be filled with tea.


For the Arduino practical, I would say in some ways it went well as I got 40/50 for it but I don't what god is against me but at the last minute, I don't know what happened but my code decide to break on me. Instead of flapping the pegastus wings for 30 times then playing the melody, it only flapped for 1 time and played the melody and stopped. Last minute somemore! SO SUEY SIA!


Well I hope you did enjoy my rant about the practical and the blog itself. I hope to see you next blog!





 
 
 

Comments


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2035 by Train of Thoughts. Powered and secured by Wix

bottom of page