ARDUINO PROGRAMMING

 Hello!! In this blog we'll talk about Arduino Programming, something that I really enjoyed doing.


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

void setup() {

 Serial.begin(9600); // Start Serial.

}

 

void loop() {

 int A = analogRead(A0);

 Serial.println(A);// Display serial results in serial monitor.

 delay (1);

}

void setup() {

 Serial.begin(9600); // Start Serial.

: establishing serial communication between your Arduino board and another device, which in this case is my laptop.  


void loop() {

 int A = analogRead(A0);

 Serial.println(A);// Display serial results in serial monitor.

 delay (1);

: it refers to reading values sent to A0 and showing results on the serial monitor with 1 ms interval. 







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

https://docs.arduino.cc/built-in-examples/basics/AnalogReadSerial



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

Some problems i encountered were first, not knowing where to connect my wires, so what I did was to look up tutorials to guide me through the connections. I also wasn’t sure what to code, hence I went online and found the website above, and used the code given.


  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

void setup() {

 Serial.begin(9600); // Start Serial.

}

 

void loop() {

 int A = analogRead(A0);

 Serial.println(A);// Display serial results in serial monitor.


void setup() {

 Serial.begin(9600); // Start Serial.

}

: establishing serial communication between your Arduino board and another device, which in this case is my laptop.  


void loop() {

 int A = analogRead(A0);

 Serial.println(A);// Display serial results in serial monitor.

: it refers to reading values sent to A0 and showing results on 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/SURYATEJA/arduino-light-senser-using-ldr-5c5458


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

For this task, the code used was similar to the one used in the previous task, but I did not understand why

  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

// C++ code

//

int sensorVal = 0;

 

int counter;

 

int counter2;

 

int counter3;

 

int counter4;

 

void setup()

{

 Serial.begin(9600);

 pinMode(13, OUTPUT);

 pinMode(12, OUTPUT);

 pinMode(11, OUTPUT);

 

 for (counter4 = 0; counter4 < 5; ++counter4) {

   for (counter = 0; counter < 2; ++counter) {

     digitalWrite(13, HIGH);

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

     digitalWrite(13, LOW);

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

   }

   for (counter2 = 0; counter2 < 2; ++counter2) {

     digitalWrite(12, HIGH);

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

     digitalWrite(12, LOW);

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

   }

   for (counter3 = 0; counter3 < 2; ++counter3) {

     digitalWrite(11, HIGH);

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

     digitalWrite(11, LOW);

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

   }

 }

}

 

void loop()

{

 delay(10); // Delay a little bit to improve simulation performance

}


int sensorVal = 0;

 

int counter;

 

int counter2;

 

int counter3;

 

int counter4;

: The above code refers to the counter used for the loops and the int sensorVal = 0; makes all counter start from 0  


 Serial.begin(9600);

 pinMode(13, OUTPUT);

 pinMode(12, OUTPUT);

 pinMode(11, OUTPUT);

:refers to establishing connections between the Arduino board and my laptop, and setting pin 11, 12 and 13 as output. 


for (counter4 = 0; counter4 < 5; ++counter4)

: refers to setting the loop to repeat 5 times


for (counter = 0; counter < 2; ++counter) {

     digitalWrite(13, HIGH);

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

     digitalWrite(13, LOW);

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

   }

: refers to making the LED to blink twice with a 1 second interval between each blink.


void loop()

{

 delay(10);

: After the main loop repeats for 5 times, it’ll wait for 10 ms before repeating the whole process again.





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

https://www.tinkercad.com/things/lElM058OhDt-incredible-snicket-gogo/editel?tenant=circuits



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

For this task, the main problem I had was the connections of wire. Because there were so many wires going on I got kind of confused with the wire runs, which I then decided that I’ll do for one led bulb to be clear of my code then I do the same for the other two led bulbs.


  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

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(13, OUTPUT);

 pinMode(12, OUTPUT);

 pinMode(11, OUTPUT);

}

 

void loop() {

 //read the pushbutton value into a variable

 int sensorVal = digitalRead(2);

 

 // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes

 // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the

 // button's pressed, and off when it's not:

 if (sensorVal == HIGH) {

   digitalWrite(13, LOW);

 } else {

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

   {

     {

     digitalWrite(13, HIGH);

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

     digitalWrite(13, LOW);

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

     digitalWrite(12, HIGH);

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

     digitalWrite(12, LOW);

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

     digitalWrite(11, HIGH);

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

     digitalWrite(11, LOW);

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

 

   }

}

}

}


pinMode(2, INPUT_PULLUP);

: configuring pin 2 as the pull-up resistor and enable internal pull-up resistor for the button



 int sensorVal = digitalRead(2);

: It reads the input of PIN2 and writes into a integer variable, sensorVal. so when the button is pressed, it’ll start the whole loop.


 if (sensorVal == HIGH) {

   digitalWrite(13, LOW);

 } else {

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

: when button is not pressed, nothing will start, but when it is pressed, it’ll start the loop in 2a.




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

https://www.tinkercad.com/things/lElM058OhDt-incredible-snicket-gogo/editel?tenant=circuits



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

For this task, what I did was to copy my code in 2a and paste it to the “digitalInpuPullUp” code. I initially just copied the part i wanted to loop, then I realised i need to declare my output too at the start of the code too, so I did that too to resolve it. 



  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.


Through this learning opportunity, I finally learned how to code, which was something I've always wanted to learn. A lot of time I have to understand what I code to be able to solve whatever problem that comes up. Often, I'll even have to search online to find a solution to my problem which could be very specific and all. However, still, I really loved how when the codes finally worked, the satisfaction that came about. For example, during the actual practical, me and Kallysa had a problem where the void set up was not declared, and we could not find the reason why, but after many times of trial and error and searching online for help, I manage to debug it and make the code work. There was another incident where we could not make 2 LED bulbs blink at the same time, but after shifting the codes around, I manage to make it work, which really made me happy. Overall, these activities were really insightful, fun and something I would try out and learn more in my free time.


Also here's our stylish pegasus and a hero shot with it:






Comments