top of page

Arduino Programming

  • Writer: Ethan Wee
    Ethan Wee
  • Dec 2, 2022
  • 2 min read

Updated: Dec 2, 2022

Hi everyone! Welcome back to the latest blog entry. Today's entry will be all about Arduino Programming!


1.a) Input devices: Interface a potentiometer analog input to maker UNO board and

measure/show its signal in serial monitor Arduino IDE.

Code and Video (1.a)

Code

int sensorPin = A0;

int ledPin = 13;

int sensorValue = 0;


void setup()


{

Serial.begin(9600);



pinMode(ledPin,OUTPUT);


}


void loop()


{



sensorValue = analogRead(A0);


Serial.println(sensorValue);



sensorValue = analogRead(sensorPin);


digitalWrite(ledPin, HIGH);


delay(sensorValue);



digitalWrite(ledPin, LOW);


delay(sensorValue);


}


Video: https://drive.google.com/file/d/1giaEh1jM9fEHBnFeAWBTMX_8d4VZweaM/view?usp=share_link



​Code

Code Explanation

​int sensorPin = A0;

​The potentiometer is connected to analog pin 0

int ledPin = 13;

​The LED is connected to digital pin 13

int sensorValue = 0;

It reads the input of 0 and writes into a integer variable, sensorVal

void setup()

​Runs the Code once after setup finishes

Serial.begin(9600);


To establishes serial communication between your Arduino board and another device

pinMode(ledPin,OUTPUT);

​Configures the pin as an output

void loop()

This function runs repeatedly after setup() finishes

Serial.println(sensorVal);

It displays the value of sensorVal if it is pressed.

sensorValue = analogRead(sensorPin);

​Sensor value is read from the special analog pin

digitalWrite(ledPin, HIGH);

Turn the LED on

​digitalWrite(ledPin, LOW);

Turn the LED off

​delay(sensorValue);

​Pause for sensorValue in milliseconds

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

https://www.youtube.com/watch?v=-EDYMQ9lczA


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

There were no problems for this particular part.



1.b) Input devices: Interface a LDR to maker UNO board and measure/show its signal in

serial monitor Arduino IDE.

Code and Video (1.b)

Code

int LDR = A0;


int ledPin = 13;


int LDRvalue = 0;


void setup()

{

Serial.begin(9600);


pinMode(ledPin,OUTPUT);


}


void loop()


{


Serial.println(LDRvalue);


LDRvalue = analogRead(LDR);



if(LDRvalue > 600)


digitalWrite(ledPin, HIGH);


else


digitalWrite(ledPin, LOW);


}


Video: https://drive.google.com/file/d/1ToxzYE5G93Jw_8BqTvH-czSUxCGkfgcP/view?usp=share_link (Serial)

https://drive.google.com/file/d/1_n7Hda7H_A0VKYz_dhUnIa1vwvvNPIWx/view?usp=share_link (Tap to off light)

Code Explanation

​int LDR = A0;

​The LDR is connected to thte analog pin

int ledpin = 13

​The LED is connected to pin 13

​int LDRvalue = 0

​The LDR value is 0 initially

​void setup()

{

Serial.begin(9600);

​This code establishes the serial communication between your Arduino board and another device

pinMode(ledPin,OUTPUT);

​Sets LED as the pin output

void loop()

​This function runs repeatedly after setup() finishes

Serial.println(sensorVal);

Displays the value of the sensor when pressed

​ LDRvalue = analogRead(LDR);

if(LDRvalue > 600) digitalWrite(ledPin, HIGH);

else digitalWrite(ledPin, LOW);

}

If light shone on the LDR is a low value, the LED connected to pin 13 will not be turned on.

Else, if there are light shun on LDR, the LED will be turned on.

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.

The only problem with this part was the understanding of the placement of wires and resistors as I could not understand the reasoning behind their positioning. I mitigated this setback by outsourcing more online resources to aid in my understanding.



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



Code and Video (2.a)

Code

void setup()

{

pinMode(11,OUTPUT);

pinMode(12,OUTPUT);

pinMode(13,OUTPUT);

}


void loop()

{

digitalWrite(11,HIGH);

delay(1000);

digitalWrite(11,LOW);

delay(1000);

digitalWrite(12,HIGH);

delay(1000);

digitalWrite(12,LOW);

delay(1000);


digitalWrite(13,HIGH);

delay(1000);

digitalWrite(13,LOW);

delay(1000);

}


Video: https://drive.google.com/file/d/1PFXkGWThIBl4ZQyhoGCg3XMtyYanAGny/view?usp=share_link

Code Explanation

void setup()

​​Runs the Code once after setup finishes

pinMode(int,OUTPUT);

​Sets the pin (intval) as output

void loop()

​This function runs repeatedly after setup() finishes

​digitalWrite(int,HIGH);

​To make pin (intval) change the voltage to high (turn on

​digitalWrite(int,LOW);

To make pin (intval) change the voltage to low (turn off

delay()

​​Pause for sensorValue in milliseconds

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.

The problem with this part was similar to the previous part where I found it troubling to understand the placement of wires and resistors as I could not understand the reasoning behind their positioning. I mitigated this setback by outsourcing more online resources to aid in my understanding. Me and my partner Jing Yang did some trial and error to explore how different positioning would affect the outcome.



2.b) Output devices: Include the pushbutton on the MakerUno board to start/stop part 2.a.

above



Code and Video (2.b)

Code

void setup()

{


Serial.begin(9600);


pinMode(2, INPUT_PULLUP);

pinMode(12, OUTPUT);

pinMode(12, OUTPUT);

pinMode(13, OUTPUT);

}


void loop()

{

int sensorVal = digitalRead(2);


Serial.println(sensorVal);



if (sensorVal == HIGH)

{

digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}

else

{

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

delay(1000);


digitalWrite(11, LOW);

digitalWrite(12, HIGH);

digitalWrite(13, LOW);

delay(1000);


digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, HIGH);

delay(1000);

}

}


Video: https://drive.google.com/file/d/1O89nGlY1DpXsXa5QDyTPINooDSsKt4Yy/view?usp=share_link

Code Explanation

​void setup()

{


Serial.begin(9600);

​​This code establishes the serial communication between your Arduino board and another device

​pinMode(intval, INPUT_PULLUP);

​​Sets the pin (intval) as input with pull up resistor

​pinMode(intval, OUTPUT);

​​Sets the pin (intval) as output

​void loop()

​This function runs repeatedly after setup() finishes

​if (sensorVal == HIGH)

{

digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}

else

{

digitalWrite(11, HIGH);

digitalWrite(12, LOW);

digitalWrite(13, LOW);

delay(1000);


digitalWrite(11, LOW);

digitalWrite(12, HIGH);

digitalWrite(13, LOW);

delay(1000);


digitalWrite(11, LOW);

digitalWrite(12, LOW);

digitalWrite(13, HIGH);

delay(1000);

}

}


​If sensor is activated and all the LEDs are not activated, the LEDs will take turn to light up in intervals of 1 second for a continuous loop.

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.

With the aid of online resources, we tinkered with part 2.a and managed to quite easily handle this part.


Learning Reflection:

I've dabbled in the world of coding before however, these few weeks of tinkering with Arduino have been very taxing. I've come to the conclusion that I'm not a fan of Arduino at all. There were many instances where I did not understand how the code works and the placement of the wires, resistors, and LEDs on the breadboard and the maker UNO.


However, with great struggle comes great satisfaction. When we manage to successfully run the code and have no errors we feel very accomplished.


Throughout the Arduino Practical, It was very frustrating to insert the wires and the servo into the pegasus to operate well in such a small frame. However, we were able to make our pegasus eyes blink and wings flap (Although the flapping was rather weak hahaha).



ree


If I could redo the practical, I would like to change the way we coiled the wings of the pegasus to include more tension and more force when flapping.



ree

Here is a video of our pegaSUS in all its glory:



 
 
 

Comments


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2023 by Train of Thoughts. Proudly created with Wix.com

bottom of page