top of page
Abstract Futuristic Background
Laser Cutting
Coding Station
image.png

Blog 3: Arduino Documentation Blog EntryšŸ‘©ā€šŸ’»šŸ’»

Updated: Jan 19, 2023


ree


ree

Yo sup everyone! 🤟 I am back with another blog for CPDD woohoo!🄳 So today I will be sharingšŸ”Š my journey in practical 3 about Ardunio Programming with you all. So let's kick off with the tutorial lesson of the Ardunio Programming session experiencešŸ¤“ and I hope yall will learn something from it woohoo! šŸ˜‰



There are 4⃣tasks that I will be explaining on this blog page🧐:

ree
  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

  2. Output devicesšŸ“¤:

ree
  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



For each of the tasks, I will describešŸ•µļøā€ā™€ļø:

ree
  1. The program/code that I have used and an 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 a video of the executed program/code.


Finally, I will describešŸ§‘ā€šŸ’»:

ree

  1. My Learning reflection on the overall Arduino programming activities.


ree

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

ree
ree

  • Below are the code/program I have used and the explanation of the codešŸ‘©ā€šŸ’».


​Code/program in a writeable formatšŸ’»


ree

​void setup()
{
    // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  // read the input on analog pin 0:
   int sensorValue = analogRead(A0);
  // print out the value you read:
   Serial.println(sensorValue);
  // turn the LED on
  digitalWrite(LED_BUILTIN, HIGH);
  // pause the program for <sensorValue> millseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause the program for <sensorValue> millseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}


​Explanation of the codešŸ“„


ree

To show that it can blink with the speed when we are turning the potentiometer šŸ”†

  • void setup()

    • Serial. begin(9600); - The setup function is to begin serial communications, at 9600 bits of data per second, between your board and your computer with the command

    • pinMode(A0, INPUT); - Pin A0 is configured as an input, so we can "listen" to the electrical state of the potentiometer.

    • pinMode(LED_BUILTIN, OUTPUT); - LED_BUILTIN is configured as an output to control the LED.

  • void loop()

    • int sensorValue = analogRead(A0); - Establish a variable to store the resistance value (which will be between 0 and 1023, perfect for an int datatype) coming in from your potentiometer

    • Serial.println(sensorValue); - you need to print this information to your serial monitor window using this command

    • digitalWrite(LED_BUILTIN, HIGH); - turn the LED on (HIGH is the voltage level)

    • digitalWrite(LED_BUILTIN, LOW); - turn the LED off by making the voltage LOW

    • delay(sensorValue); - So if sensorValue is 1023, the program will pause for 1023 milliseconds when delay(sensorValue); is executed.

ree

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


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


ree
Problem 1:
I did not connect the potentiometer properly as the wire below is short therefore leads didn't blink or light up. I fixed them by using a small tweezer to turn the wire in and insert it into the board properly.

ree

Problem 2:
In the code, I forget to insert the serial.printIn and thought it will appear in the serial monitor, therefore, it lead to nothing showing up in the serial monitor. However, using the reference from the internet I am able to make the serial monitor able to show the signal by using these codes:
Serial.begin(9600);
Serial.println(sensorValue);
int sensorValue = analogRead(A0);
delay(sensorValue);

ree

  • Below is the short video as evidence that the code/program works.


ree


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

ree

ree

  • Below are the code/program I have used and the explanation of the code.


​Code/program in writeable format.šŸ’»


ree

void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop() {
int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("value of LDR :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
  {
    digitalWrite(13,HIGH);//Makes the LED glow in Dark.
  }
  else
  {
    digitalWrite(13,LOW);//Turns the LED OFF in Light.
  }
}


​Explanation of the codešŸ“„

​​To show that the light can blink when it senses the LDRšŸ’”

ree
  • void setup()

    • Serial. begin(9600); - The setup function is to begin serial communications, at 9600 bits of data per second, between your board and your computer with the command

    • pinMode(13, OUTPUT); - Pin 13 is configured as an output, to control the LED

  • void loop()

    • int ldr=A0 - Set A0(Analog Input) for LDR.

    • int value=0; - Analog reading starts from 0

    • value=analogRead(LDR); - Reads the Value of LDR(light).

    • Serial. println("value of LDR:"); - you need to print this information to your serial monitor window using this command

    • digitalWrite (13, HIGH); - Makes the LED glow in Dark.

    • digitalWrite(13, LOW); - Turns the LED OFF in Light.


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



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


ree

Problem 1:
I did not connect the wiring correctly causing the sensor of the LDR not to work however I fixed them by taking some parts of the wiring out to see which one is in the wrong position and I try to organize my wiring in an organized method.

ree

Problem 2:
In the codes, I tend to miss out on the semi-colon at the end of the code causing errors to occur therefore, I decided to remember to put a semi-colon by ensuring that the codes are in an organizable way in order to have a better view of the codes.


ree

  • Below is the short video as evidence that the code/program works.



ree

Output devices: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)šŸ’”šŸ’»


ree

ree

  • Below are the code/program I have used and the explanation of the code.

​Code/program in writeable formatšŸ’»


ree

int ledRed = 9;
int ledYellow = 6;
int ledGreen = 3;
const int fade = 5;

void setup() {
 pinMode(ledRed, OUTPUT);
 pinMode(ledYellow, OUTPUT);
 pinMode(ledGreen, OUTPUT);
}

void loop() {
 //ledRed
 for (int i = 0; i <= 255; i++) {
   analogWrite(ledRed, i);
   delay (fade);
 }
 for (int i = 255; i >= 0; i--) {
   analogWrite(ledRed, i);
   delay (fade);
 }
 //ledYellow
 for (int i = 0; i <= 255; i++) {
   analogWrite(ledYellow, i);
   delay (fade);
 }
 for (int i = 255; i >= 0; i--) {
   analogWrite(ledYellow, i);
   delay (fade);
 }
 //ledGreen
 for (int i = 0; i <= 255; i++) {
   analogWrite(ledGreen, i);
   delay (fade);
 }
 for (int i = 255; i >= 0; i--) {
   analogWrite(ledGreen, i);
   delay (fade);
 }
 //ledYellow
 for (int i = 0; i <= 255; i++) {
   analogWrite(ledYellow, i);
   delay (fade);
 }
 for (int i = 255; i >= 0; i--) {
   analogWrite(ledYellow, i);
   delay (fade);
 }
}

​Explanation of the codešŸ“„

​​To show that the light can fade after 5 milliseconds.šŸ’”

ree
  • void setup() - function, state whether the pins are Input or Output.

    • pinMode(ledRed, OUTPUT); Setting ledRed as an output

    • pinMode(ledYellow, OUTPUT); - Setting ledYellow as an output

    • pinMode(ledGreen, OUTPUT); - Setting ledGreen as an output

  • void loop() - It tells the MCU what to do.

    • int ledRed = 9; - It indicates which components go to which PIN number

    • int ledYellow = 6; - It indicates which components go to which PIN number

    • int ledGreen = 3; - It indicates which components go to which PIN number

    • const int fade = 10; - fading in milliseconds

    • for (int i = 0; i <= 255; i++) - To decrypt the for loop

      • int i=0; -> for temporary integer "i" equals to zero

      • i < 255; -> if "i" less than and equal value 255

      • i++; -> "i" value + 1

    • analogWrite(ledRed, i); - Analog reading starts from 0

    • delay (fade); - Reads the Value of LDR(light).

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

https://www.youtube.com/watch?v=GdNg7rK9GCk&t=4s&ab_channel=PhuongVo



ree

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


Problem 1:
ree
The green and yellow LED blinked instead of fading therefore after many checks by looking at each part of the wiring to see where I go wrong and I found out that I put the wiring in the wrong PIN number I change it immediately and it works instantly.

Problem 2:
ree
In the codes, I did not have any problems however one slight problem is that I tend to add extra stuff like full stop or colon, and how I fixed this will be by checking the codes first before sending them out to the Arduino Board.



ree

  • Below is the short video as the evidence that the code/program work


ree

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


ree

ree

  • Below are the code/program I have used and the explanation of the code.

​Code/program in writeable formatšŸ’»


ree

​int ledRed = 9;
int ledYellow = 6;
int ledGreen = 3;
int button = 7;
const int fade = 10;

void setup() {
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  if(digitalRead(button) == HIGH) {
    delay(15);
    if(digitalRead(button) == HIGH){
      fadeLights();
    }
  } else {
    digitalWrite(ledRed, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, HIGH);
   }
} 


void fadeLights(){
  
    for( int i=255; i>=0; i--) {
    analogWrite(ledGreen, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

   //ledYellow
  for( int i=0; i<=255; i++) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  for( int i=255; i>=0; i--) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }
  
  //ledRed
  for( int i=0; i<=255; i++) {
    analogWrite(ledRed, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }
  for( int i=255; i>=0; i--) {
    analogWrite(ledRed, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  //ledYellow
  for( int i=0; i<=255; i++) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  for( int i=255; i>=0; i--) {
    analogWrite(ledYellow, i);
    for( int y = 0; y < 400; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }

  //ledGreen
  for( int i=0; i<=255; i++) {
    analogWrite(ledGreen, i);
    for( int y = 0; y < 1000; y++){
      // if button is released
      if (digitalRead(button) == LOW) {
      return;
      }
    }
  }
}

​Explanation of the codešŸ“„

​To show the pushbutton able to utilize to on and off the switch.šŸ”˜šŸ’”

ree
  • void setup() - function, state whether the pins are Input or Output.

    • pinMode(ledRed, OUTPUT); Setting ledRed as an output

    • pinMode(ledYellow, OUTPUT); - Setting ledYellow as an output

    • pinMode(ledGreen, OUTPUT); - Setting ledGreen as an output

    • pinMode(button, INPUT); - This is where we declare that button is an INPUT. So, by default, the button status will always be HIGH

  • void loop() - It tells the MCU what to do.

    • digitalWrite(ledRed, LOW); - turn the LED off by making the voltage LOW

    • digitalWrite(ledYellow, LOW); - turn the LED off by making the voltage LOW

    • digitalWrite(ledGreen, HIGH); - turn the LED off by making the voltage HIGH

    • delay(15); - this will then have a delay of 15 milliseconds

    • int ledRed = 9; - It indicates which components go to which PIN number

    • int ledYellow = 6; - It indicates which components go to which PIN number

    • int ledGreen = 3; - It indicates which components go to which PIN number

    • const int fade = 10; - fading in milliseconds

    • for (int i = 0; i <= 255; i++) - To decrypt the for loop

      • int i=0; -> for temporary integer "i" equals to zero

      • i < 255; -> if "i" less than and equal value 255

      • i++; -> "i" value + 1

    • analogWrite(ledRed, i); - Analog reading starts from 0

    • delay (fade); - Reads the Value of LDR(light).


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




ree

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

Problem 1:
ree

I have a problem pressing the button in the Arduino Uno as my hand is a bit big however I am able to do it once I get the hang of it or use a tweezer to press the button and see whether it can start and stop the system.


Problem 2:
ree
In the codes, I did not have any problems however one slight problem is that I tend to add extra stuff like full stop or colon, and how I fixed this will be by checking the codes first before sending them out to the Arduino Board.


ree

  • Below is the short video as the evidence that the code/program work.



ree

  • Below is my Learning Reflection on the overall Arduino Programming activities.


ree

I feel that the overall experiencešŸ’ā€ā™€ļø with Arduino ProgrammingšŸ’» activities was greatšŸ‘ and challengingšŸ’Ŗ like the ones we did in the labšŸ‘©ā€šŸ”¬ by using different types of codesšŸ’» to make the horsešŸ¦„ able to flap its wingsšŸ¦… and ensure no lossen parts inside this horsešŸ¦„. Even though we struggle a bit for the wingsšŸ¦… to flap it was still manageable🤠 as me and Asraf able to complete it within the time frame⌚. Other than that we struggle😫 trying out the millisšŸ’», however, it didn't turn out wellšŸ‘Ž, therefore, we decided to stop🤚 and continue what we havešŸ˜„. This is what I and Asraf build for the horsešŸ¦„ and below are the codesšŸ’» on how to operate thisšŸ‘©ā€šŸ”§:


​​Code/program in writeable formatšŸ’»


ree


​#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    // initialize digital pin 13 and 12 as an output.
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(2);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees]

    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);                       // waits 15 ms for the servo to reach the position
  }
  // the loop function runs over and over again forever
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                       // wait for a second
    digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                       // wait for a second
}



ree

Finalize the Design of the horsešŸ–‹šŸ¦„:




In the previous tutorial class, taught by Mr. TingšŸ‘Øā€šŸ« he let us have funšŸ˜ with Arduino ProgrammingšŸ‘©ā€šŸ’», and not to lie I grew interested 🤩 in doing programming stuffšŸ‘©ā€šŸ’» but overall it has been a wonderfulšŸ˜† and fruitful journeyšŸ˜‹ for this Arduino Programme activities. I feel that these activities can help me through my CA1 prototypingšŸ¤” as one of the requirements is using Arduino ProgrammingšŸ‘©ā€šŸ’» and in the future used like in my FYP and many more.🧐


As you can see I have come to the end of my blog🤣 I hope you enjoy my blogšŸ˜Ž and remember to likeā¤ļøļø, sharešŸ¤— and subscribe🤳. WatchšŸ‘€ my other cool blogs😱 in my cpdd blog categories🤩 have a great weekend aheadšŸ˜† and all the best for your MST to my SP classmates and friends🤭🤘. I will be back next year strongeršŸ’Ŗ and better blog 🤟see yašŸ‘‹!!!


Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page