Arduino Programming

 Hello everybody! Welcome back to my final blog entry for this module which will be Arduino programming!  I have studied Arduino programming for the past 4 weeks and this blog will show my progress.

For part 1 of this blog will be focused on input devices. I have learnt to use 2 input devices which are a potentiometer analog input and a light dependent resistor (LDR).

For part 2 of this blog will be focused on 2 output devices which are light-emitting diodes (LED) and a push button.

Input devices

This is the code used to make the potentiometer analog input setup work:

Code

Explanation

int sensorvalue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);

}

 

void loop()

{

  sensorvalue = analogRead(A0);

  digitalWrite(LED_BUILTIN, HIGH);

  Serial.print(",");

  Serial.println(sensorvalue);

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

  digitalWrite(LED_BUILTIN, LOW);

  Serial.print(",");

  Serial.println(sensorvalue);

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

}

-Set sensor value to 0

 

 

-set PIN A0 and LED as output

 

 

-establish connection between Arduino board and computer

 

 

 

-define variable sensorvalue

 

-show sensorvalue in serial monitor

 

 

For this challenge there were no problems I had to fix as the source I had used was easy to follow, so all I had to do was copy exactly what was done.

Sources/references used:

https://youtu.be/-EDYMQ9lczA

Video proof:

 


 

This is the code used to make the LDR setup work:

Code

Explanation

int light = 0;

 

void setup()

{

  Serial.begin(9600);

}

 

void loop()

{ light = analogRead(A0);

 Serial.println(light);

 delay(100);

}

-set variable light as 0

 

 

 

 

 

 

-define variable light

-print values in the serial monitor

 

There was a slight problem when I couldn’t get the serial monitor to show up on my computer, so I thought the code had problems showing the numbers in the serial monitor. A quick google search later, I learnt how to pull up the serial monitor. On one occasion, the serial monitor was show numbers in 1 line. This was because I used the command Serial.Print and not Serial.Println which prints the values on another line.

Sources/references used:

https://create.arduino.cc/projecthub/electronicsfan123/interfacing-arduino-uno-with-ldr-8760ba

Video proof:

 


Output devices

This is the code used to make the light-emitting diode setup work:

Code

Explanation

int animationSpeed = 0;

 

void setup()                        // setup function

{

         

  pinMode(13, OUTPUT);            

  pinMode(9, OUTPUT);             

  pinMode(5, OUTPUT);            

   

 

  Serial.begin(9600);

}

 

void loop()                         // loop function

{

 

 animationSpeed = 400;

 

      digitalWrite(13, HIGH);        // turn on LED

      digitalWrite(9, HIGH);        // turn on LED

      digitalWrite(5, HIGH);        // turn on LED

      delay(animationSpeed);

      digitalWrite(13, LOW);         // turn off LED

      digitalWrite(9, LOW);         // turn off LED

      digitalWrite(5, LOW);         // turn off LED

      delay(animationSpeed);

}

 

 

 

 

 

-set PIN 13, 9 and 5 as output

 

 

 

 

-establish connection between board and computer

 

 

 

-set animation speed to 400

 

 

-set LEDs to high mode

 

 

-delay by 400 ms

-set LEDs to low mode

 

 

-delay by 400ms

 

After uploading the code for the first time I realised that the green LED was not lighting up as brightly as the yellow and red LEDs and after some inspection, I realised that the resistor used for the green LED was higher than the 1s used for the yellow and red LEDs.  As  such, swapping the resistor solved the problem.

Sources/references used:

https://youtu.be/X8dHbdhnGKY

Video proof:

 


This is the code used to make the push button setup work:

Code

Explanation

int buttonState = 0;                // set value for buttonState

void setup()                        // setup function

{

         

  pinMode(13, OUTPUT);             

  pinMode(9, OUTPUT);            

  pinMode(5, OUTPUT);            

   

  pinMode(2, INPUT_PULLUP);         // initialize push button

  Serial.begin(9600);

}

 

void loop()                         // loop function

{

  if (digitalRead(2) == LOW){

    while(digitalRead(2) == LOW){}

  

    if (buttonState == 1)      

    {

      digitalWrite(13, HIGH);        // turn on LED

      digitalWrite(9, HIGH);        // turn on LED

      digitalWrite(5, HIGH);        // turn on LED

      buttonState = 0;

    }

    else

    {

      digitalWrite(13, LOW);         // turn off LED

      digitalWrite(9, LOW);         // turn off LED

      digitalWrite(5, LOW);         // turn off LED

      buttonState = 1;

  }

 }

}

-declare buttonstate is 0

 

 

 

 

-set pin 13, 9 and 5 as output for LEDs

 

 

 

 

-set pin 2 as input for button

 

-establish connection between board and computer

 

 

 

-code will run only when button is pushed

 

 

-if button state is 1 turn on LEDs and set button state to 0

 

 

 

 

 

-Else turn off LEDs and set button state to 1

 

 

 

There were many occasions where the code did not work because I did not start on a new sketch or the capitalization of words was not accurate or simply spelling mistakes. After double checking, most of the problems was solved.

Sources/references used:

https://create.arduino.cc/projecthub/krivanja/working-with-an-led-and-a-push-button-71d8c1

video proof:


 


Learning reflection

I feel conflicted with regards to Arduino programming, on one hand it is frustrating when the code has errors when I think it is going to work and it takes a lot of time to fix the errors, but on the other hand when the code does work it gives an immense sense of satisfaction and accomplishment. There were many times where I wanted to give up because I had been struggling with the same problem for half an hour trying to find a solution.


Comments

Popular posts from this blog

DOE Case Study

Project development: Door Handle Streilizer