ARDUINO Programming

 1.Input devices:

a) The fist task is to interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE


This is the code used: 

void setup()

{

  Serial.begin(9600);

  pinMode(A0, INPUT);

}


void loop()

{

  int sensorVal = analogRead(A0);

    Serial.println(sensorVal);

}



On Arduino board:




Explanation of code:
Serial.begin(9600); Serial. begin() establishes serial communication between Arduino board and another device. The most common use of serial communication being establish is between Arduino and computer via a USB cable.
pinMode(A0, INPUT); - This means that the input is being set at pin A0.
int sensorVal = analogRead(A0)- This means that it reads the input of PINA0 and writes into a INTEGER variable, sensorVal.
Serial.println(sensorVal);  - It displays the value of sensorVal if it is pressed.



b) The second task is to interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE


The code used: 

void setup()

{

  Serial.begin(9600);

  pinMode(10, INPUT);

}


void loop()

{

  int sensorVal = analogRead(A0);

    Serial.println(sensorVal);

}





Explanation for codes:
Serial.begin(9600); Serial. begin() establishes serial communication between Arduino board and another device. The most common use of serial communication being establish is between Arduino and computer via a USB cable.
pinMode(10, INPUT); - This means that the input is being set at pin 10.
int sensorVal = analogRead(A0)- This means that it reads the input of PINA0 and writes into a INTEGER variable, sensorVal.
Serial.println(sensorVal);  - It displays the value of sensorVal if it is pressed.

I uses the same as the previous task as the only different between this two task is that I replaced the Potentiometer with Light Dependent Resistor. 


2.Output devices

a)Next,  interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)


On Arduino:



The code used:

\// C++ code

//

int brightness = 0;


int i = 0;


void setup()

{

  pinMode(9, OUTPUT);

}


void loop()

{

  for (brightness = 0; brightness <= 255; brightness += 5) {

    analogWrite(9, brightness);

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

  }

  for (brightness = 255; brightness >= 0; brightness -= 5) {

    analogWrite(9, brightness);

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

  }

}

After watching the tutorial videos and the question requirements of having 3 LEDs interfaced and able to fade or flash, I oriented it such that the LEDs are parallel sequence so that I can control the lighting of all 3 light at once with the simplest code. As the lights are sharing one out put on the Arduino board, therefore I can simplified the code use as compared when it is in series. Through the tutorial videos, I modified the code use such that it meets my requirements. 



Link to stimulator

Source code for 2a

Explanation of codes:

For the code used, we are setting pin 9 as our output, when brightness is less than or equal to 255, there will an increment of 5 and when brightness is more than or equal to 0, there will be a decrease of 5. There will also be a delay of 30 millisecond.



b)Lastly, interface the DC motor to maker UNO board and program it to on and off using push button on the board

On Arduino: 
The code used: 

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(10, 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(10, LOW);
  } else {
    digitalWrite(10, HIGH);
  }
}




Explanation of the program code use:
Serial.begin(9600); Serial. begin() establishes serial communication between Arduino board and another device. The most common use of serial communication being establish is between Arduino and computer via a USB cable.
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
pinMode(10, OUTPUT); - This means that the output is being set at pin 10.
int sensorVal = digitalRead(2);  - It reads the input of PIN2 and writes into a INTEGER variable, sensorVal.
Serial.println(sensorVal);  - It displays the value of sensorVal if it is pressed.


Source code for 2b


Learning point from interfacing an input device to Arduino board 

From interfacing an input device to Arduino board, I learned how to connect a input device such as a potentiometer and a Light Dependent Resistor to a breadboard and Arduino board. I learnt that by learnt varying the input value, the output value will be affected by the input value. Such as, when the value of potentiometer and the lighting of Light dependent Resistor is affected, the output value will also be affected. 

Learning point from interfacing an output device to Arduino board 

From interfacing an output device to Arduino board, I learned how to program code on the Arduino kit to perform new function such as make code to light up LED together and make a moto to rotate by pressing on a button on the Arduino kit. By doing so, I am able to visualize how we can put this into practical use such as using this as traffic light and Fan moto. 

Problem faced and solution
Problem: On the first attempt of the individual assignment question part a, I first tried to came up with a series circuit but it does not work on the Arduino board. As I do not know how to solve this problem, I decided to to change my plan to making it into a parallel circuit instead. After the parallel circuit has work, I realised that I may have connect the series circuit wrongly and causes it to not function. 


Reflection

This is practical starts off with 4 pre-practical assignment with four challenges to give us a head start and foundation building on Arduino programming. With the 4 challenges, we are to have a better understanding of codes as it provides explanations on various code used. During the practical we were task to complete the competency test of using code to program the Arduino kit and make the light of the Arduino kit to blink like traffic light in sequence and interval of 2secs before each light blink. After the competency test, we had a group activity of using Arduino kit, servo moto and the programmed code that we came up with to make the wings of the Pegasus to flap. In the end, my group successfully made the wings of the Pegasus flap and added additional functions such as background music and implement push button to control when and duration of the wing flap.

The task in the practical is important as the task assigned to us during the practical is something that had never done it before which allow us to think out of the box when doing the tasks. In my opinion, this is very important as we are new to Arduino programming and this activity is able to deepen and widen our knowledge in programming and also to help us build a good foundation in programming. The most interesting part of the practical is the moment when we are able to make to wings of Pegasus flap with other functions implemented. This made the practical interesting as we feel a sense of accomplishment to achieve a success after many failure. The experiment is useful for project in CP5070 as we are to use the new functions we learned to implement on our prototype using Arduino board. With this, we can save the cost of buying new parts such as a rotating fan. 

I used to think that Arduino programming is difficult to learn especially after I failed my first competency test. However, when I went for my retest, to make sure that I understand what I am doing, my lecturer check on my understanding by asking me question on the code that I wrote and explained to me to clear my misconception after I had passed my retest for the competency test. Now I think that Arduino programming is not as hard as what I had thought before having a good understanding of it. After  having a deeper understanding of Arduino programming, next I will try to make use of Arduino programming in my prototype.

Comments