top of page
Video Game Developers

Arduino
Programming

Computer Programming

What is Arduino?

Arduino is both a physical (microcontroller) and a digital platform known as an Integrated Development Environment(IDE). The IDE is used to write and test code that the microcontroller will interpret and perform functions as intended by the programmer.

In this page I will explain...

1. Input Devices:

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

​

2. Output Devices:

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

b. 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 my learning reflection on the overall Arduino programming activities.

\

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

The Code

Explanation

#define POTENTIOMETER_PIN A0

int sensorVal = 0

​

​

void setup() {
  pinMode(A0, INPUT);
  pinMode(5, OUTPUT);
Serial.begin(9600);
}

​

void loop() {
sensorVal = analogRead(POTENTIOMETER_PIN);
Serial.println(analogRead(POTENTIOMETER_PIN));
digitalWrite(5, HIGH);
delay(sensorVal);
digitalWrite(5,LOW);
delay(sensorVal);

}

​

"#define" attaches a pin to the component - pin A0 is associated with POTENTIOMETER_PIN

sensor value is established to be 0

​

under void setup() //code runs once

sets pin A0 to be input pin to receive values

sets pin 5 to be output to LED attached

this code begins a line of communication between the board and the computer

​

under void loop() //code runs repeatedly

sets sensor value to be whatever analog reading is attained from pin A0

​

analogRead() tells the board to read the analog signal from the potentiometer via the pin associated with POTENTIOMETER_PIN

​

Serial.println() tells the board to display the readings in the Serial Monitor in Arduino

​

digitalWrite() tells the board to send HIGH or LOW signal to pin 5 - Connected to LED

​

delay() tells the board to set the delay between HIGH or LOW to the value detected by the board through the potentiometer via pin A0.

​

Links to resources:
https://roboticsbackend.com/arduino-potentiometer-complete-tutorial/

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

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

Initially, when compiling the code, I kept receiving error messages that would not allow me to upload the code to the board. Upon consultations and discussions among peers as to what went wrong, I realised that I have been missing the semicolons after each line of code. After inserting them, I was finally able to upload the code. However, the code did not run. I did a little bit of troubleshooting and found out that my potentiometer was not oriented the right way. After rotating it to the intended orientation, the code was finally executed the right way.

The Code In Action

Concrete Wall

Input devices: Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE:

The Code

Explanation

#define LDR_PIN A1
int sensorVal = 0;

 


void setup() {
  pinMode(A0, INPUT);
  pinMode(5, OUTPUT);
Serial.begin(9600);
}

​

void loop() {
sensorVal = analogRead(LDR_PIN);

 


Serial.println(analogRead(LDR_PIN));

 

 

 

 

 


digitalWrite(5, HIGH);

 


delay(sensorVal);
digitalWrite(5,LOW);
delay(sensorVal);
}

"#define" attaches a pin to the component - pin A1 is associated with LDR_PIN

sensor value is established to be 0

​

under void setup() //code runs once

sets pin A0 to be input pin to receive values

sets pin 5 to be output to LED attached

this code begins a line of communication between the board and the computer

​

under void loop() //code runs repeatedly

sets sensor value to be whatever analog reading is attained from pin A1

​

analogRead() tells the board to read the analog signal from the potentiometer via the pin associated with POTENTIOMETER_PIN

 

Serial.println() tells the board to display the readings in the Serial Monitor in Arduino

​

digitalWrite() tells the board to send HIGH or LOW signal to pin 5 - Connected to LED

​

delay() tells the board to set the delay between HIGH or LOW to the value detected by the board through the potentiometer via pin A0.

​

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

There was no issue with the code as it was derived from the code I used for the potentiometer. All I had to do was change the input of the analog signal to pin A1 which I had set my LDR to. However, once I uploaded my code, the LDR did not seem to work and the LDR readings on the serial monitor did not change as expected. I then took a quick look at my breadboard and realised that my LDR did not have the 5V power connected to it so the readings were super low. Once i connected it, all went well.

photo_2022-12-03_14-03-41.jpg

LDR Code In Action

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

Computer Circuit Board

The Code

Explanation

int brightness = 0;


void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}

​

void loop() {
for (brightness = 0; brightness <= 255; brightness +=5){
  analogWrite(5,brightness);
  delay(10);
  analogWrite(6,brightness);
  delay(10);
  analogWrite(9,brightness);
  delay(10);
}
 

 for (brightness = 255; brightness >= 0; brightness-=5){
   analogWrite(5,brightness);
delay(10);
analogWrite(6,brightness);
  delay(10);
  analogWrite(9,brightness);
  delay(10);}
  }

initialises brightness to be zero

​

Under void setup()// runs code once

pinMode sets pin 5, 6, and 9 to output pins for LED

begins serial communication between board and computer

​

​

​

under void loop()// runs code repeatedly

sets that from 0(OFF) to 255(ON), brightness will increase by 5 units.

​

analogWrite() allows LEDs to light up at different brightness thanks to PWM until it reaches full brightness to simulate "fading" effect

delay() sets how fast LED will reach fully ON from fully OFF and vice versa.

​

​

​

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

Initially, the code did not run properly. Upon checking the code, I realised that I did not include braces before the first analogWrite and last delay for both "for" codes. This made the board stay completely off the whole time. Once I added them, they worked well with a smooth fade in and fade out.

Illuminated Stairs

LED Fade Code In Action

Computer Processor
Music Recording

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

To add a button to the code, the same code will be used but will additional elements added to it...

The Code

Explanation

int brightness = 0;
int i = 0;


void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
}

​

void loop() {
  int sensorVal = digitalRead(2);


  Serial.println(sensorVal);
  if (sensorVal == LOW){
    for (i = 1; i <=5; i+=1){


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

 


  analogWrite(5,brightness);
  delay(10);
  analogWrite(6,brightness);
  delay(10);
  analogWrite(9,brightness);
  delay(10);
}
 

 for (brightness = 255; brightness >= 0; brightness-=5){
   analogWrite(5,brightness);
delay(10);
analogWrite(6,brightness);
  delay(10);
  analogWrite(9,brightness);
  delay(10);}
  }
  }
}

initialises brightness to be zero

initialises number of times code has been run to 0

​

Under void setup()// runs code once

pinMode sets pin 5, 6, and 9 to output pins for LED

pinMode sets pin 2 to be input/button

​

​

begins serial communication between board and computer

​

under void loop()// runs code repeatedly

sets sensorVal to be whatever is read on pin 2, a.k.a the button

prints out the value of the button in the serial monitor

if sensorVal is LOW, code starts from 1st run up to the 5th run of the code by increments of 1 after each full run

​

sets that from 0(OFF) to 255(ON), brightness will increase by 5 units.

​

analogWrite() allows LEDs to light up at different brightness thanks to PWM until it reaches full brightness to simulate "fading" effect

delay() sets how fast LED will reach fully ON from fully OFF and vice versa.

​

​

​

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

As most of this code was derived from the previous task, there was minimal problems. However, learning the code to make the entire task run specifically 5 times was a bit confusing. Having to research the codes and implementing them into my code and tweaking it to suit my needs was not as straightforward as I thought it would be but I did it eventually.

Performance Lighting

Button + LED Fade In Action

Learning
Reflection

As far as I've come, I've realized that coding is not actually difficult once you get the hang of it. It feels like I'm building LEGOs every time I code. Taking this code and fitting it into that code, mixing and matching to get my desired function. However, coding does demand some patience and meticulousness - making sure every semicolon, brace and upper/lowercase letters in the code are right where they should be. All in all, I think with more practice and experience, I will soon be able to code more complex systems and make them do more complex tasks that would benefit me in my journey in CPDD 

CP5070-2022-2B03-Group1-Adyl Bin Yani

©2022 by CP5070-2022-2B03-Group1-Adyl Bin Yani. Proudly created with Wix.com

bottom of page