ARDUINO Programming

Arduino

In this page, I will be sharing with you my Arduino codes and the resulting outcomes of the codes👽. The page is divided into two parts Input devices and Output devices and will be segmented as such:

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 programme it to perform something (fade or flash etc.)

b. Include the pushbutton on the Maker UNO board to start/stop part 2a. above.


1. Input devices

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


Explanation:

As the potentiometer changes, the blinking speed of light will also change.


Code Used:

 int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
sensorValue = analogRead(sensorPin);
// print out the value you read:
  Serial.println(sensorValue);
  delay(1);  // delay in between reads for stability
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}


Sources used to write the programme:

  • Analog Serial Read

https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial


Setup:





Video:




b. Interface a LDR to Maker UNO board and measure/show its signal in serial monitor Arduino IDE.


Explanation:

When it is dark, the LED on pin 13 will light up.


Code Used:

 int LDR = A0;
int ledPin = 13;
int LDRvalue = 0;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
LDRvalue = analogRead(LDR);
  Serial.print("LDR Value is: ");
  Serial.println(LDRvalue);
if(LDRvalue > 600)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}



Sources used to write the programme:

  • Analog Serial Read (for LDR)

https://create.arduino.cc/projecthub/krivanja/working-with-light-dependent-resistor-ldr-1ded4f


Setup:







Video:



2. Output devices

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


Explanation:

LED lights will flash one by one in sequence (yellow, green, red).


Code Used:

 int animationSpeed = 1000;

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, 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(8, HIGH);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(8, LOW);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
}



Sources used to write the programme:

  • 3 LEDs Blinking Circuit

https://www.youtube.com/watch?v=Vs9BzdnqNlc&ab_channel=Thetips4you

  • Multiple LEDs and Breadboard

https://www.instructables.com/Multiple-LEDs-Breadboards-With-Arduino-in-Tinkerca/


Setup:







Video:




b. Include the pushbutton on the Maker UNO board to start/stop part 2a. above.


Explanation: 

When button is pressed, LED lights will flash in sequence (yellow, green, red).


Code Used:

 int animationSpeed = 1000;

void setup()
{
  //start serial connection
  Serial.begin(9600);
   //configure pin 2 as an input and enable the internal pull-up resistor
 pinMode(2, INPUT_PULLUP);
 pinMode(13, OUTPUT);
 
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 8 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(8, LOW);
  } else {
    digitalWrite(13, HIGH);
 
  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(8, HIGH);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(8, LOW);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
}
}


Sources used to write the programme:

  • Pushbutton Arduino Programming Document

file:///C:/Users/kally/OneDrive/Documents/SP%20DCHE/Y2S2/CP5070%20-%20Chemical%20Product%20Design%20and%20Development/Arduino%20Programming/CP5070-Arduino-DCHE2B-03-KallysaBteMohdNoh.pdf


Setup:

Same setup as part 2a. above.


Video:




Learning Reflection

The Arduino programming activities were mostly self-learning activities. I had to manage my time wisely to complete the pre-practical tasks and the Arduino blog tasks since each pair were only given one Maker UNO kit. As we had to share the kit, I felt a bigger responsibility to ensure that there were no missing pieces in the kit when I had to pass it to my partner, Wei Ling, for her to use. It was also my responsibility to complete my part in time so that Wei Ling had sufficient time to complete her work too. All went smoothly and both Wei Ling and I had the time to complete our respective Arduino tasks👍.

During the lesson, Wei Ling and I had partnered up to complete the pre-practical activities together during lessons. This was to ensure that we could finish up at least a few of the pre-practical challenges together during lessons and then I will have lesser tasks to complete at home and consequently, I can pass the Maker UNO kit to Wei Ling earlier. When I was in doubt, I asked Wei Ling for help whenever I had encountered a problem with my codes. When we both could not figure out the error, we also asked my friends, Isabella and Joelle for help. It was satisfying when we both figured out the correct codes and could achieve the final outcome that we wanted. Wei Ling also helped me to record the videos for my pre-practical challenges and in return I helped record some of hers too and I am thankful for Wei Ling's help😀.

As far as I can recall, this was my first or second time learning how to code. Some difficulties that I experienced is that the e-learning packages were not easy to understand and I had to analyse every little information and detail so as to not make a mistake on my codes. However, I had found the step-by-step slides on Arduino programming useful to my learning because it helped guide me at every step. However, as not all the information that I needed  to complete my Arduino blog tasks are provided in Brightspace, I had to look up on information on Serial Monitors and LED setup on breadboard from the web. Youtube videos were the resources that I found most useful to me because it provides a visual aid on how to put together the setup. Additionally, the links provided by the Youtubers under the description box were relevant to learning as well💻.

Most of the time, it was frustrating to get the codes right and it was even more frustrating when I do not understand the error in Arduino😡. Besides watching Youtube videos and browsing the web for help, tips and help from my friends were also useful in aiding my work. I have learnt keyboard shortcuts to navigate around Arduino's serial monitor from Isabella and how to combine multiple functions to create one final outcome from Wei Ling. Due to multiple trial and errors, I have learnt to better understand the different coding functions and I can identify my mistakes in my codes😌.

Overall, I have attained a new skill in programming using Arduino and I anticipate in using and improving on my skills in the near future when I am creating my product with my team which is a breath analyser😃.