Arduino Programming

 Welcome back ! πŸ‘€

This entry will be about what I have done in relation to Arduino Programming 😁

In this entry, I will be going through 2 main tasks that my group and I have completed. The first task focuses on the Input devices while the second task focuses on Output devices. Below are the tasks that my group and I have done.

  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

(1a) 
Program

int sensorValue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

Serial.begin(9600);

}

 

void loop()

{

  // read the value from the sensor

  sensorValue = analogRead(A0);

Serial.println(sensorValue);

  // turn the LED on

  digitalWrite(13, HIGH);

  // pause the program for <sensorValue> milliseconds

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

  // turn the LED off

  digitalWrite(LED_BUILTIN, LOW);

  // pause the program for <sensorValue> milliseconds

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

}



Specific Code

Explanation

int sensorValue = 0;

Reads input of 0, then writes it into an integer variable, sensorValue

void setup()

The setup will run once board is powered or reset button is pressed

pinMode(A0, INPUT);

Set pin A0 as input 

pinMode(13, OUTPUT);

Set pin number 13 as output 

Serial.begin(9600);

Starts the serial connection 

void loop()

Loops code 

sensorValue = analogRead(A0);

Reads the voltage passing through A0

Serial.println(sensorValue);

Print out value of push button 

digitalWrite(13, HIGH);

Setting voltage to the high mode, LED switches on for pin 13

delay(sensorValue);

Wait for the sensorValue's value in milliseconds

digitalWrite(LED_BUILTIN, LOW);

Setting voltage to low mode, LED switches off for the default pin

delay(sensorValue);

Wait for the sensorValue's value in milliseconds

Below are the hyperlink to the sources/references that I used to write the code/program.
https://youtu.be/yyG0koj9nNY

Below are the problems encountered and how I fixed them.
Initially we couldn't get the LED to light up even with arranging the circuit correctly. We decided to switch the LED cathode and anode by just flipping the LED around. We used trial and error and wow it worked.

Below is a short video as evidence that the program works.



(1b)
Program

int light;

 

void setup() {

Serial.begin(9600);

}

 

void loop() {

  light= analogRead(A0);

 

 if(light<50){

   digitalWrite(13, LOW);

 

 }else{

   digitalWrite(13, HIGH);

 }

 

  Serial.println(light);

  delay(0);

}


Specific Code

Explanation

int light;

Reads input based on how much light is that is shunned

void setup() {

The setup will run once board is powered or reset button is pressed

Serial.begin(9600);

Starts the serial connection 

void loop() {

Loops code 

light= analogRead(A0);

Reads the light shunned on A0

if(light<50){

When the input value of light is less than 50,

digitalWrite(13, LOW);

Setting voltage to low mode, LED switches off for the pin 13

}else{

if not,

digitalWrite(13, HIGH);

Setting voltage to the high mode, LED switches on for pin 13

Serial.println(light);

Print out the value of the light input value

delay(0);

delay for 0 milliseconds

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

Below are the problems encountered and how I fixed them.
My team and I couldn't get the LDR to work and after watching several videos and asking around, we manage to find the code as well as the set up. When I had to set it up by myself, my LED lighted up but it wasn't affected by the LDR and I thought it was a connected problem. But following the setup my team has done before, it was exactly the same. I decided to play with the code and changed light<50 to light<30. It worked, hence I think that due to the darker environment I was at, it made the LED not light up.

Below is a short video as evidence that the program works

(2a)
Program

void setup() {

 

  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

 

}

 

 

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);  

  delay(2000);                      

  digitalWrite(LED_BUILTIN, LOW);    

  delay(1000);                      

  digitalWrite(12, HIGH);  

  delay(2000);                      

  digitalWrite(12, LOW);    

  delay(1000)

  digitalWrite(11, HIGH);  

  delay(2000);                      

  digitalWrite(11, LOW);  

  delay(1000);                     

}



Specific Code

Explanation

void setup() {

The setup will run once board is powered or reset button is pressed

pinMode(LED_BUILTIN, OUTPUT);

Set default pin as output 

pinMode(12, OUTPUT);

Set pin number 12 as output 

pinMode(11, OUTPUT);

Set pin number 11 as output 

void loop() {

Loops code 

digitalWrite(LED_BUILTIN, HIGH);

Setting voltage to the high mode, LED switches on for default pin

digitalWrite(LED_BUILTIN, LOW);

Setting voltage to low mode, LED switches off for the default pin

digitalWrite(12, HIGH);

Setting voltage to the high mode, LED switches on for pin 12

digitalWrite(12, LOW);

Setting voltage to low mode, LED switches off for the pin 12

digitalWrite(11, HIGH)

Setting voltage to the high mode, LED switches on for pin 11

digitalWrite(11, LOW);

Setting voltage to low mode, LED switches off for the pin 11

delay(2000);

wait for 2000 milliseconds

delay(1000);

wait for 1000 milliseconds

Below are the hyperlink to the sources/references that I used to write the code/program.
https://youtu.be/X8dHbdhnGKY

Below are the problems encountered and how I fixed them.
There were no problems for this task.

Below is a short video as evidence that the program works.



(2b)
Program

void setup() {

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

 

 

}

 

void loop() {

  int sensorVal = digitalRead(2);

  Serial.println(sensorVal);

 

  if (sensorVal == HIGH) {

    digitalWrite(13, LOW);

    digitalWrite(12, LOW);

    digitalWrite(11, LOW);

 

   

  } else {

    digitalWrite(13, HIGH);

    digitalWrite(12, LOW);

    digitalWrite(11, LOW);

    delay(1000);

    digitalWrite(13, LOW);

    digitalWrite(12, HIGH);

    digitalWrite(11, LOW);

    delay(1000);

    digitalWrite(13, LOW);

    digitalWrite(12, LOW);

    digitalWrite(11, HIGH);

    delay(1000);

  }

}



Specific Code

Explanation

void setup() {

The setup will run once board is powered or reset button is pressed

Serial.begin(9600);

Starts the serial connection 

pinMode(2, INPUT_PULLUP);

Set pin number 2 as input

pinMode(13, OUTPUT);

Set pin number 13 as output 

pinMode(12, OUTPUT);

Set pin number 12 as output 

pinMode(11, OUTPUT);

Set pin number 11 as output 

void loop() {

Loops code 

int sensorVal = digitalRead(2);

Reads the pushbutton value into a variable 

Serial.println(sensorVal);

Print out value of push button 

if (sensorVal == HIGH) {

When button is not pressed

digitalWrite(13, LOW);

Setting voltage to low mode, LED switches off for the pin 13

digitalWrite(12, LOW);

Setting voltage to low mode, LED switches off for the pin 12

digitalWrite(11, LOW);

Setting voltage to low mode, LED switches off for the pin 11

} else {

if not,

digitalWrite(13, HIGH);

digitalWrite(12, LOW);

digitalWrite(11, LOW);

LED connected to digital pin 13 lights up while the other 2 does not light up

digitalWrite(13, LOW);

digitalWrite(12, HIGH);

digitalWrite(11, LOW);

LED connected to digital pin 12 lights up while the other 2 does not light up

digitalWrite(13, LOW);

digitalWrite(12, LOW);

digitalWrite(11, HIGH);

LED connected to digital pin 11 lights up while the other 2 does not light up

delay(1000);

Wait for 1000 milliseconds

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

Below are the problems encountered and how I fixed them.
At first, the first LED lighted up the entire time without pressing the button and the other two LED alternated, also without pressing the button. The problem was fixed by making the LED into a parallel circuit so when the button is pressed, the LEDS will then light up.

Below is a short video as evidence that the program works.


Lastly, my reflection for the overall arduino activities done. 😀

In my opinion, I really enjoy all of this. I just wish that learning to code is taught more proper and goes beyond the example given in the arduino examples in the software. It is definitely a lot different than what us chemical engineering year 2 students are going through.

On 24 November 2022, my class and I had to do the Arduino practical in the W3 workshop. The main objective of this practical is to form the cardboard pegasus and make use of the servo to flap it's wings.
 There are other factors that graded as well, such as an additional code to be used from what we have learned from the examples in the arduino software.

Once my team and I finished setting up the cardboard pegasus and the servo with its code, we had to discuss what additional code we would like to add. Me being a simple man, I suggested adding the 'programmable button' function to the pegasus, such that when the button is pressed, whatever function is coded will play. 

However, this idea of mine wasn't very appealing to the rest and disagreed. Instead, they went with the usage of the LED lights we were given to be used as the eyes of the pegasus and the other to play music.  They even wanted to mash these 2 codes in one. And to be honest, while I do like this idea, to me, I see it as over-achieving as the challenge I assumed that we were gonna face is something that we have not learned nor has it been mentioned once. An analogy used to describe this by Dr Noel as well as Kieron is and I quote "Don't climb Mount Kinabalu if you haveb't even climb Bukit Timah". But, I just let it slide.

I also knew that aesthetics is worth 10 marks out of 50 as well. When I brought it up to the team that we should try drawing or designing the pegasus itself, I got shut down and they said that it is already nice as it is. Only when they found out that other groups designed their pegasus then we did, and this was 5-10 minutes before the presentation of our 2 pegasus. And even within these 5-10 minutes we had left, we spent most of the time figuring out why we couldn't play 2 codes at the same time.

As such, the design of me and my partner, Tristan, pegasus looks very simple
and to be very honest, the only time I feel that I have contributed is helping Samantha by assembling the wires inside the unicorn. All in all, I learned that if I do have an idea that is worth mentioning, I should be firm with it and if anyone disagrees, I should at least try to find out why and hear what their idea is first and try to reach a compromise. 

Comments

Popular posts from this blog

About me