Arduino Programming

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

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:

5.     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.

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


Code/program in writeable format

Explanation of the code

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);  // delay in between reads for stability
}


Serial.begin(9600);

This is to establish a connection between the arduino board and another device, in this case it is the potentiometer. 

int sensorValue = analogRead(A0);

This is to be able to send the signal to analog pin A0. 

Serial.println(sensorValue);

This it to be able to view the values on the Serial monitor.

delay(1);

This is to add in delays between each reading.

















1.  

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

I got the code from this website

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

A problem that I faced while running the code was that when I first looked at this challenge, I did not know how to connect the potentiometer and how it would work. However, after searching on Google and finding the website, I found out how the connection had to be done as well as understood how the code worked. This was because I initially did not add a jumper wire to pin A0 and I connected it to pin A2. This led to me not being able to see anything on the serial monitor. 

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



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

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

void loop() {
  int A = analogRead(A0);
  Serial.println(A);// Display serial results in serial monitor.
  if (A < 100 ) {   // Change 100 to the number depending on the light in your area.
    digitalWrite(13, HIGH);
  }

  else {
    digitalWrite(13, LOW);
  }

}

Serial.begin(9600);

It is to establish a connection with the LDR in this scenario.


pinMode (13, OUTPUT);

is to show that when the LDR output changes, it will change the brightness of the LED. 

















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

The link I used it here!


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

At the start, I was having trouble with connecting the LDR and using it. This is because I was just trying to complete the connections but adding jumper wires where I thought I should add them. However this was not the case and I found out by looking online to find out the arrangement of the wires which solved the problem. However after copying the wiring, I forgot to add the LED back in. 


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




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 format

Explanation of the code

// C++ code
//

int counter;

int counter2;

int counter3;

int counter4;

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

  for (counter4 = 0; counter4 < 5; ++counter4) {
    for (counter = 0; counter < 2; ++counter) {
      digitalWrite(13, HIGH);
      delay(100); // Wait for 1000 millisecond(s)
      digitalWrite(13, LOW);
      delay(100); // Wait for 1000 millisecond(s)
    }
    for (counter2 = 0; counter2 < 2; ++counter2) {
      digitalWrite(12, HIGH);
      delay(100); // Wait for 1000 millisecond(s)
      digitalWrite(12, LOW);
      delay(100); // Wait for 1000 millisecond(s)
    }
    for (counter3 = 0; counter3 < 2; ++counter3) {
      digitalWrite(11, HIGH);
      delay(100); // Wait for 1000 millisecond(s)
      digitalWrite(11, LOW);
      delay(100); // Wait for 1000 millisecond(s)
    }
  }
}

void loop()
{
  delay(100); // Delay a little bit to improve simulation performance
}

int counter;

It is used to loop the first LED which is connected to pin 13. This explanation works for int counter 2 and 3 as well. 

int counter 4;

is the amount of times it will loop the cycle of the 3 LED used. 


(counter 4 = 0; counter4 < 5; ++counter4)

Means that counter 4 would start at the count of 0. and if it is less than 5, it will add a count each time until it reaches 5 which would break the loop.





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

The code which I referred to for the blink part is here

The code which I referred to for the counter is here


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

A problem that I encountered was that the code did not run the way I wanted it to. This led to me troubleshooting and redoing the whole code over and over again so that it would work. As my last resort, I asked Wei Ling for help as she could do it. 


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






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

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

Code/program in writeable format

Explanation of the code

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(12, OUTPUT);
  pinMode(11, 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 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    for (int i=0; i < 2; i++)
    {
      {
      digitalWrite(13, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(13, LOW);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(12, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(12, LOW);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(11, HIGH);
      delay(1000); // Wait for 1000 millisecond(s)
      digitalWrite(11, LOW);
      delay(1000); // Wait for 1000 millisecond(s)

    }
}
}
}

pinMode(2, INPUT_PULLUP); - This is where we declare that PIN 2 is an INPUT and enable the internal pull up resistor. So, by default, PIN2 status will always be HIGH





















































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

The link I used for the button is here

The link I used for the blink is here

The code which I referred to for the counter is here    

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

The first code that I ran did not work as expected. This is because when I pressed the button, the LEDs stop blinking and when I let go of the button, the LEDs blinked. This was when I saw that I was careless and flipped the HIGH and LOW for the IF/ELSE part of the code.


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




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

Overall, the Arduino has been a rollercoaster ride. This is because when you get the code running as intended on the first try is really fulfilling and satisfying. However, the opposite is true whereby when the code does not work, it takes a while to understand why it does not work. This is really frustrating and very demoralizing when I have to continuously troubleshoot to find out where the mistakes lie. 

With this being said, it has truly been a fun ride coding, and am looking forward to coding more during the prototyping of our chemical device as there will be certain components that we have to figure out how to code as well. 

During the practical, it was fun seeing how all our codes from the pre-practical could all come together to produce something even more impressive altogether. 


This was the wiring that we had when we carried out the practical, it was messy and this had us thinking on how to make it more pleasing and neater. Then the idea of poking a hole at the leg of the pegasus and running it through to hide the wiring came to mind. Which led to this:

Not only did I learn how to code from this practical, I also learnt that it is important to be able to hide the mechanism from others as it would be more aesthetically pleasing and looks more solid as a whole. 



























Comments