Arduino programming

 Hello!! welcome back to my blog, today i will be covering my learning experience on arduino programming. specifically, input and output devices, with 2 parts to each


input device:

a) interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor arduino IDE.

b) interface a LDR to maker UNO board and measure/show its signal in serial monitor arduino IDE

output device:

a) interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something 

b) include the pushbutton on the MakerUno board to start/stop part a



1. the progam/code that i have used and explanation of the code. The code is in writable format
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


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

1. The code that i have used and explanation of the code

Code/program in writeable format

code

Explanation of the code

const int analogInPin = A0; 

const int analogOutPin = 9;

int sensorValue = 0;       

int outputValue = 0;       

void setup() {

  Serial.begin(9600);

}

void loop() {

  sensorValue = analogRead(analogInPin);

  outputValue = map(sensorValue, 0, 1023, 0, 255);

  analogWrite(analogOutPin, outputValue);

  Serial.print("sensor = ");

  Serial.print(sensorValue);

  Serial.print("\t output = ");

  Serial.println(outputValue);

  delay(2);

}

const int

Const int are constants used to give names to pins used.

 

const int analogInPin = A0

 

In this case, the analog input pin that the potentiometer is attached to is A0.

const int analogOutPin = 9

 

The analog output pin that the LED is attached to is PIN9

int sensorValue = 0

reads the sensorValue, which refers to the reading on the potentiometer

int outputValue = 0

Reads the output values of the potentiometer

void setup() {

 

Sets up a connection between the laptop and the program

Serial.begin(9600);

Initialize serial communications at 9600 bps

void loop() {

 

Tells the program to run this line of code infinitely

sensorValue = analogRead(analogInPin);

Read the analog in value

outputValue = map(sensorValue, 0, 1023, 0, 255)

Maps the value from analog to a range of 0 to 1023 and 0 to 255

analogWrite(analogOutPin, outputValue)

Change the analog out value

Serial.print("sensor = ");

Serial.print(sensorValue);

Serial.print("\t output = ");

Serial.println(outputValue);

Prints the results to the serial monitor

delay(2)

Wait for 2 milliseconds before repeating the whole loop code



2.hyperlink/sources that i used to write the program:

example code from arduino:
file > examples > 03.Analog > AnalogInOutSerial
TinkerCad:



3. Problems i encountered and how i fixed them

The first problem that i encountered was connecting the arduino, bread board, wires and potentiometer all together. In the beginning, reinard and i were trying to figure out how the bread board works in the first place because there were so many different ports and we could not figure out which to put into. So what we did was to actually tear out the bottom sticker of the bread board, giving us a better insight on how the connections work. Using this knowledge and online sources provided to us on how to connect the potentiometer, we finally figured it out. 

Another problem that i had was trouble understanding what the codes meant, so what i did was to actually re-do the learning package from lesson 1 which gave me a refresher on what i am actually doing.

4. short video as the evidence that the code/program works






input devices: interface a LDR to maker UNO board and measure/show its signal in serial monitor arduino IDE

1. The code that i have used and explanation of the code

Code/program in writeable format

code

Explanation of the code

const int analogInPin = A0; 

const int analogOutPin = 9;

int sensorValue = 0;       

int outputValue = 0;       

void setup() {

}

void loop() {

  sensorValue = analogRead(analogInPin);

  outputValue = map(sensorValue, 0, 1023, 0, 255);

  analogWrite(analogOutPin, outputValue);

  Serial.print("sensor = ");

  Serial.print(sensorValue);

  Serial.print("\t output = ");

  Serial.println(outputValue);

  delay(2);

}

const int

Const int are constants used to give names to pins used.

 

const int analogInPin = A0

 

In this case, the analog input pin that the LDR is attached to is A0.

const int analogOutPin = 9

 

The analog output pin that the LED is attached to is PIN9

int sensorValue = 0

reads the sensorValue, which refers to the reading on the potentiometer

int outputValue = 0

Reads the output values of the LDR

void setup() {

 

Sets up a connection between the laptop and the program



void loop() {

 

Tells the program to run this line of code infinitely

sensorValue = analogRead(analogInPin);

Read the analog in value

outputValue = map(sensorValue, 0, 1023, 0, 255)

Maps the value from analog to a range of 0 to 1023 and 0 to 255

analogWrite(analogOutPin, outputValue)

Change the analog out value

Serial.print("sensor = ");

Serial.print(sensorValue);

Serial.print("\t output = ");

Serial.println(outputValue);

Prints the results to the serial monitor

delay(2)

Wait for 2 milliseconds before repeating the whole loop code



2.hyperlink/sources that i used to write the program:

https://youtu.be/99dUAe9uqSI 
and 
example code from arduino:
file > examples > 03.Analog > AnalogInOutSerial

3. Problems i encountered and how i fixed them

For part b, because reinard and i spent a lot of time trying to understand connections in part a. We did not run into many problems so things went pretty smoothly for this part. 

However, one thing worth mentioning is definitely the connecting and set up of the LDR. The main issue was not that we did not know how to connect the pieces, it was that the thin metal wires from the LDR and resistor kept bending away when we were trying to insert it into the holes because our fingers kept slipping away, bending the wires. so to resolve this issue, we placed the wire on the hole and proceeded to hammer it in with our pens and it worked!

4. short video as the evidence that the code/program works



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

1. The code that i have used and explanation of the code

Code/program in writeable format

code

Explanation of the code

int brightness = 0;

 

void setup()

{

  pinMode(9, OUTPUT);

}

 

void loop()

{

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

    analogWrite(9, brightness);

    delay(5);

  }

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

    analogWrite(9, brightness);

    delay(5);

  }

}

int brightness = 0;

 

Creates a variable called brightness and sets it to 0

pinMode(9, OUTPUT);

Sets PIN9 as output because we want to send signals to the LED

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

Program will read the brightness of the LED and if it is equal to 0 and lesser than or equals to 255. It will slowly increase the brightness by 1. Causing the LED to slowly light up

{ analogWrite(9, brightness);

Writes the brightness into the selected pin, which is 9 in this case

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

Program will read the brightness of the LED and if it is equal to 255 and more than or equals to 0. It will slowly decrease the brightness by 1.

delay(5);

Wait for 2 milliseconds before repeating the whole loop code


2.hyperlink/sources that i used to write the program:

https://youtu.be/X8dHbdhnGKY 

3. Problems i encountered and how i fixed them

For this part, i did not have much problems with the coding because it was pretty straightfoward from the source. however one issue we had once again was figuring out how to connect 3 LEDs together so that they can all fade in and out together. to counter this issue, we used what we have learnt on how the bread board works and put it into good use by using the sides as a 'main supply' to all the LEDs. 


4. short video as the evidence that the code/program works



Output devices: include the pushbutton on the MakerUno board to start/stop part a

1. The code that i have used and explanation of the code

Code/program in writeable format

code

Explanation of the code

int brightness = 0;

 

void setup() {

 Serial.begin(9600);

 pinMode(2, INPUT_PULLUP);

 pinMode(13, OUTPUT);

 

}

 

void loop() {

 int sensorVal = digitalRead(2);

 Serial.println(sensorVal);

 

 if (sensorVal == HIGH) {}

 

 else {

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

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

    analogWrite(9, brightness);

    delay(10);

  }

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

    analogWrite(9, brightness);

delay(10);

}

  }

 }

 }

}

int brightness = 0;

Creates a variable called brightness and sets it to 0

Serial.begin(9600);

 

Initialize serial communications at 9600 bps

pinMode(2, INPUT_PULLUP);

Selects PIN2 as the input. Program will read the signal sent from PIN2

pinMode(13, OUTPUT);

 

Selects PIN13 as the output. Program will take signal from PIN13

int sensorVal = digitalRead(2);

Creates an integer for sensorVal  and sets it to digitalRead(2)

Serial.println(sensorVal);

Displays the value of sensorVal

if (sensorVal == HIGH) {}

 

 else {

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

 

If the sensorVal is read to be HIGH (button is not pressed), do nothing

 

If the sensorVal reads other readings(sensorVal =LOW) or button is pressed, it will run the two brightness code 5 times

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

    analogWrite(9, brightness);

    delay(10);

  }

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

   analogWrite(9, brightness);

delay(10);

Program will read the brightness of the LED and if it is equal to 0 and lesser than or equals to 255. It will slowly increase the brightness by 1.

 

Program will read the brightness of the LED and if it is equal to 255 and more than or equals to 0. It will slowly decrease the brightness by 1.

 

There will then be a delay of 10 milliseconds after running the code above it

 


2.hyperlink/sources that i used to write the program:

my big brain + past knowledge :D

3. Problems i encountered and how i fixed them

for the second part, there are not really many issues because i am now very familiar with coding LEDs and i have gotten better at placing the resistors in. However one problem that we had was that when we put in the code for (int i=0; i < 5; i++) it was only running the first part of the brightness code but it was quickly resolved when we realised that we did not bracket the entire brightness code.

4. short video as the evidence that the code/program works



learning reflection on the overall Arduino programming activities

coding... i really do not like it because it does not stick with me, i tend to forget what the codes do and how they work if i do not revise them after a few days or weeks. i would then have to take a quick look at my past work and refresh my mind before i start on my arduino work.

Although i do not look forward to coding, it was really satisfying when my code was able to function. A prime example of this can be when we were doing the Arduino lesson part 2, where we had to use our knowledge from the previous lesson and apply it by using and writing new codes along with old ones and combine them to make something function.

However there is also another big takeaway from this and that is that i should look at my problems from different perspectives/options. During the first task in lesson 2, we could not figure out what we were doing because up until that point, we only touched on the arduino board and nothing related to the bread board, resulting in a lot of frustrations when using it. We were then given a model of how we should place the potentiometers etc. but even after that we were still not satisfied because it felt as if we were just copying the answer and not learning the process. 

To counter this issue, we decided to work backwards. After placing the model arrangement, we tore the bottom piece of paper below the breadboard and studied the ways the items are connected and slowly worked our way to the answer. And from doing that, we got a deeper understanding of how the board works and that the pins can actually be anywhere as long as they reside in the same 'line'. 

learning this actually gave me a lot of confidence in using a bread board/LEDs, which came in handy when we had our Arduino practical lesson

Our coding had little to no issues for meeting the practical criteria and i feel that one of the reasons was that i actually understood what i was doing. instead of using a breadboard to light up the LED, i connected it directly to the Arduino board, this then reduces the amount of wires and objects used, giving more plus points to the 'aesthetic' portion of the criteria.

Here is a picture of our Pegasus we are very proud of :)


However there are definitely problems that we run into, which is that our wings could not flap during presentation. This was because initially, our pegasus' wings were flapping only slightly and we wanted to improve the range of its flapping. Thus we decided to bend the wings around, which instead of making it flap better, it made it worse. The cardboard had lost its tension to move back to its original position. This was a very sad experience because our pegasus could actually flap its wings initially D: 

But i guess this was a good learning experience as i should have taken into consideration the properties of cardboard and that it behaves very differently from plastic or metal. I also regretted not applying my past knowledge from ICPD, which was cardboard joinery. we could have eliminated the option of using a glue gun, which saves waste. 

















Comments