Thursday, 1 December 2022

Talk is cheap. Show me the codes.

 Arduino Programming🤓😵‍💫





What's popping guys! Welcome back to my third blog! In this blog I'll be walking you through my journey on arduino programming. Let me start by saying it was hella big challenge for me and I am seriously glad that I have this task given to me. Only with the help of my awesome teammates was I able to conquer my fear of coding.


Arduino Documentation Blog Entry:

There are 4 tasks that will be explained in this page:

  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

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:

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



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

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


Code/program in writable format

Explanation of the code

// C++ code

//

int sensorValue = 0;


void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}


void loop()

{

  // read value from the sensor

  sensorValue = analogRead(A0);

  // turn the LED on

  digitalWrite(LED_BUILTIN, HIGH);

  // pause the program for <sensorValue>

  // millieseconds

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

  // turn the LED off

  digitalWrite(LED_BUILTIN, LOW);

  // pause the program for <sensorValue>

  // millieseconds

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

}


⭐️Under the void setup part:

➡️Void setup() the pinMode function (A0, input) establishes A0 as an input ➡️(LED_BUILTIN, OUTPUT) states that the pin 13 is the output ➡️As default, pin 13 is named as LED_BUILTIN on the code.

⭐️The void loop section with main code:

➡️The code inside the void loop uses anew function: analogRead which listens to the pin's states. 

➡️The digitalwrite word is the command word to turn the LED on or off according to how it is set up. HIGH means on, LOW means off. 

➡️The delay function usually commands the program to halt for an amount of time, but in this case the value in the delay() function would change the LED frequency according to how much the knob on the potentiometer is turned. 

➡️You may wonder why there are so many words in coding. The // are known as comment lines whose duty is to clarify what each code means. Since they are for explanation and clarification purposes, they are not used or included in the final uploaded code.


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

With the help of Autodesk Tinkercad videos provided in brightspace, I was able to run a simulation with arduino code online then transfer code to the physical arduino board.


Link to video: https://youtu.be/-EDYMQ9lczA 



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

It was off to a bad start when the LED light did not shine when the code is uploaded with no error. This could only mean that the source of error is the physical arduino board itself. My team and I tried to tightened if not re plug the jumper wires and resistor but it still could not work. We then found out with the help of the other team that we accidentally placed the LED wrongly. As the LED cathode was just slightly longer than the anode, it did not help us realise the mistake at first, however re installing the LED, it verified the reason why our LED could not shine. Overall it was still an absolute success.


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









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

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

Code/program in writeable format

Explanation of the code

int LDR = A0;

int ledPin = 13;

int LDRvalue = 0;


void setup ()

{

pinMode (ledPin, OUTPUT);

}  


void loop ()

{

LDRvalue = analogRead (LDR);

if (LDRvalue > 950)

digitalWrite (ledPin, HIGH);

else

digitalWrite (ledPin, LOW);

}


➡️Pin 13 is established as the LED pin and A0 is established as the LDR pin.


⭐️Under the void setup part:

➡️The pinMode (ledPin, OUTPUT) establishes PIN 13 as the output.


⭐️The void loop section with main code:


➡️analogRead (LDR) reads the value of the LDR pin.


➡️When the LDR status is larger than 950, the digitalWrite command reads the LED as HIGH and LED will be turned on. On the other hand when LDR status is lower then 950 the LED is LOW and it will not shine.




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

lesson 9 on bright space Maker Uno Kit. Sadly I am unable to upload a file hence the pictures!





A video by MERT Arduino & Tech on Youtube has also help us understand the meaning of the codes

Link to video:https://youtu.be/4fN1aJMH9mM


They are kind enough to provide us with the Arduino code! Although I did not use it.

Link to code:https://create.arduino.cc/editor/mertarduinotech/40f2d22f-7853-4882-b2a9-1b6d43ac81b0/preview




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

During the initial attempt, the LDRvalue was set to >600 and it was too low. This made it such that even with a bright area, the LED was still lit up. This was due to the lighting of the area I was working in being higher than 900 LDRvalue. Therefore by adapting to the location of work the code was adjusted to be above 950, LDRvalue >950. After trial and error of with different LDRvalues, I was able to make the LED not light up in the lighting condition in the work area. Only when surrounding lights were dimmed (by placing my hand on the LDR sensor) would the LED light up.



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







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

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

Code/program in writeable formatExplanation of the code

// C++ code

//

int animationSpeed = 0;

 

void setup()

{

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

}

 

void loop()

{

  animationSpeed = 400;

  digitalWrite(13, HIGH);

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

  digitalWrite(13, LOW);

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

  digitalWrite(12, HIGH);

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

  digitalWrite(12, LOW);

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

  digitalWrite(11, HIGH);

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

  digitalWrite(11, LOW);

}


➡️This programme creates e variable: LED 1, 2 and 3 allowing us to change the output pins with ease.
➡️Animation speed is initially 0.
⭐️Under the void setup part:

➡️pinMode() command establishes pin 11, 12 and 13 as the output. ⭐️The void loop section with main code: ➡️Set animation speed to 400.
➡️The digitalWrite() command writes the pins to be HIGH or LOW which means on or off.
➡️From void loop, pin 13 is on after 400 milliseconds, pin 13 turns off.
➡️This repeats until pin 11 is LOW or offed. ➡️Then, after the specified delay time, the procedure will start from pin 13 HIGH again and continue to loop.


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

Again with the help of Autodesk Tinkercad videos provided in brightspace, I was able to test out a dry run on the simulation software and transfer it to the arduino board.


Link of video:https://youtu.be/MojSo7OtF9w




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

Initial I had forgotten to change the animation speed which meant that it was at 0. which made an error occur. The LED were also barely visible and was really dim. I tried to reconnect the jumper wires and even used new jumper wire yet nothing changed. I had to redo each step once again and realised the resistor I used was 10 thousand Ohm instead of the 1 thousand one. The red and orange were so similar in colour gradient I mistook the resistor readings.



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








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

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

Code/program in writable format

Explanation of the code

const int greenLedVehicle = 5; const int yellowLedVehicle = 6; const int redLedVehicle = 7; const int greenLedPedestrian = 3; const int redLedPedestrian = 4; const int pushButton = 2;


void setup()


{


pinMode(greenLedVehicle, OUTPUT); pinMode(yellowLedVehicle, OUTPUT); pinMode(redLedVehicle, OUTPUT); pinMode(greenLedPedestrian, OUTPUT); pinMode(redLedPedestrian, OUTPUT); pinMode(pushButton, INPUT_PULLUP); digitalWrite(greenLedVehicle, HIGH); digitalWrite(redLedPedestrian, HIGH);

}


void loop()

{

if(digitalRead(pushButton)== LOW)

{

digitalWrite(greenLedVehicle, LOW); digitalWrite(yellowLedVehicle, HIGH);

delay(2000); digitalWrite(yellowLedVehicle, LOW); digitalWrite(redLedVehicle, HIGH);

delay(1000); digitalWrite(redLedPedestrian, LOW); digitalWrite(greenLedPedestrian, HIGH);

delay(5000); digitalWrite(greenLedPedestrian, LOW); digitalWrite(redLedPedestrian, HIGH);

delay(1000); digitalWrite(redLedVehicle, LOW); digitalWrite(greenLedVehicle, HIGH);

}

}

⭐️The declaration and initialization of variables completed in the first part.

➡️The green vehicle LED was attached to pin 5.

➡️Yellow vehicle LED to pin 6.

➡️Red vehicle LED to pin 7.

➡️The green pedestrian LED was declared to be attached to pin 3.

➡️The red pedestrian to pin 4.

⭐️Under the void setup part:

➡️Initialization of the pins was done with pinMode() command.

➡️digitalWrite() command initializes the red pedestrian LED and green vehicle LED to be set to HIGH at the start. (They willl light up)

⭐️The void loop section with main code:

➡️When button is pressed, digitalRead which is the push button will command read the value of the pushbutton.

➡️The digitalWrite() commands the LED to be HIGH or LOW, on or off.

➡️The delay() commands the delay time interval between each digitalWrite() command.

⭐️When the button is activated:

➡️Initially the green vehicle LED is LOW and offed. The yellow vehicle LED will be HIGH and light up. After a 2000 millisecond delay the yellow vehicle will be LOW and turns off. Then the red vehicle LED will be HIGH. When the red LED lights up, the vehicles are signaled to stop and red pedestrian light becomes LOW after 1 second(1000 milliseconds) and turns off.

➡️When this happens, the green pedestrian light will be HIGH and turns on allowing pedestrians to scoot along. 
After 5 seconds (5000milliseconds), the green pedestrian LED will turn LOW and turn off. Then the red pedestrian LED will turn on.

➡️After a delay of 1 second, red vehicle LED will turn off and green vehicle LED turns on, signaling that cars may start driving.


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

Project 1, interactive traffic light. This can also be found in the brightspace Maker Uno kit file.





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

With the help of the guide it was very easy to operate this traffic light. Together with the previous experience from the above task, I was used to checking the arduino board even before uploading the code. Initially the red light for the traffic light part was not lighting up, however with my habit of checking the board again, I realised it was again placed wrongly where the cathode and anode should switch place. After making changes, everything just clicked and ran very smoothly.



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





🧠Below is my Learning Reflection on the overall Arduino Programming activities.🤔💭

Wow I don't even know where to begin. It felt like I have been coding my whole term away. Although it has been only 2 or 3 weeks of intensive coding, the redoing of arduino and bread box coupled with the never ending mess of re-coding really made it feel like eternity😨😫. Goodness me I never thought I would be doing arduino in a course named "chemical engineering" can you? 😭


It is surprisingly not the first time I learnt coding I did it once when I was in secondary 1👀. My little brain cells🧠 and of course the help of my buddies we programmed a small ship that had a sole purpose of catching debris like plastic and rubbish floating on water. It was really fun putting pipes and floatable pool noodles to make that construction as it is latched at the back of my mind very clearly. With this said my coding skills has deteriorated over time and now I have completely forgotten all about it. It was fine though as I had amazing lesson materials to guide me through this project.👍


Although the lesson packages were brimming with content, they were all really logical and helpful. These included resistor readings checking for connectivity, potentiometer mechanism and much more. Not to mention the school was kind enough to provide a arduino kit for a pair of students! I really enjoyed myself so much playing with such delicate pieces of equipment. 


Along the coding journey I have made countless of mistake, I think countless is not even the right word to use, anyhow through each experience I learn something new and made sure never to make the same mistake again. Some examples were placing the positive and negative electrode on the wrong end, using improper resistor misplacing a code etc. These mistake made me naturally double and triple check my construction and code before uploading it. It has made me realise the beauty of coding and why so many people are exploring and creating new and awesome codes. This few basic lessons of coding were really just the tip of the iceberg and I am certain there will be never ending trial and error if I were to venture into coding. 


Speaking of basics, it has really related me back to chemical engineering especially laboratory skills🧪 and process operation skills. Basic are so important not just in work but in life😉. Applying the basic skills and mastering it build and incredible foundation as seen in coding. I have personally learned and understood many of the coding terms such as voidsetup, voidloop, delay and much more. Knowing the purpose of each code or phrase at one glance really improved the overall efficiency when adjusting a code. Although I have to admit, saying that I understood them all is way to far-fetched as I am just a beginner in coding I still firmly believe it has been enlightening to recap coding and I truly believe it will be of great help in the future. 😎


I really appreciate the teachers who put together the learning resources as it really made an impact🫡. As coding or programming is highly experimental, explaining to students such that they could grasp the basic definitely is more difficult then it sounds😵. I am glad that I got to experience coding again and I hope that my fellow classmates can see and appreciate the complexity of each code.


Thank you guys for sticking around till the end. My journey of coding has sadly ended here for this term but fret not there will definitely be more content to be uploaded so till then peace out!✌


Disclaimer: there will be more coding in my capstone project so don't forget to tune in when that time arrives:)














No comments:

Post a Comment

Masterpiece development documetary

Hello everyone~ I am back again to show you my blog of this semester! I will be sharing with you an awesome project design that my team and ...