ARDUINO PROGRAMMING

Hello guys, welcome back to my blog. This time, new adventures await, as I bring you guys through my new adventure called Arduino Programming. 

Well, if you guys are not wondering what Arduino Programming is, then you must know your coding really well. For those still wondering what Arduino Programming is, dont worry, this blog will make you understand what Arduino Programming is.

I will be explaining about the 4 tasks that was carried out in great details, for you guys to have a further understanding.

 

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

 int sensorPin = A0;

int ledPin = 13;

int sensorValue = 0;

void setup()

{

   Serial.begin(9600);

 pinMode(ledPin,OUTPUT);

}


void loop()

{

  int sensorVal = digitalRead(2);

 

  Serial.println(sensorVal);

 sensorValue = analogRead(sensorPin);

 digitalWrite(ledPin, HIGH);

 delay(sensorValue);

 digitalWrite(ledPin, LOW);

 delay(sensorValue);

}

int sensorPin = A0; means that the potentiometer is connected to analog pin 0.

int ledPin = 13; means that the LED is connected to digital pin 13.

int sensorValue = 0; declares another integer variable to store the value of the potentiometer.

void setup()  is where you set up the board's pin identity.

Serial.begin(9600); establishes serial communication between your Arduino board and another device.This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second. 

pinMode(ledPin,OUTPUT); means that pin 13 is the output.

void loop() means that the code runs on a loop.

int sensorVal = digitalRead(2); means that it reads the input of pin 2 and writes into a integer variable, sensorVal.

Serial.println(sensorVal); means it displays the value of sensorVal if the analog is pressed.

sensorValue = analogRead(sensorPin); means the value is read from the analog pin, A0.

digitalWrite(ledPin, HIGH); means pin 13 is set to 1, making pin 13 HIGH, causing it to ON.

delay(sensorValue); a delay of 0 milliseconds.

digitalWrite(ledPin, LOW); means pin 13 is set to 0, making pin 13 LOW, causing it to OFF.

delay(sensorValue); a delay of 0 milliseconds.





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

    https://www.youtube.com/watch?v=yyG0koj9nNY
    https://www.youtube.com/watch?v=-EDYMQ9lczA

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

When inserting the LED into the breadboard, we switched the sides of the anode and the cathode, causing the LED to not light up. However, after looking through the video in brightspace, we checked the arrangement of the LED and realised we had made the mistake.

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

int LDR = A0;

int ledPin = 13;

int LDRvalue = 0

void setup()

{

   Serial.begin(9600);

 pinMode(ledPin,OUTPUT);

}


void loop()

{


    int sensorVal = digitalRead(13);


  Serial.println(sensorVal);


 LDRvalue = analogRead(LDR);

 if(LDRvalue > 600)

 digitalWrite(ledPin, HIGH);

 else

 digitalWrite(ledPin, LOW);

int LDR = A0; means that the LDR is connected to analog pin 0

int ledPin = 13; refers to digital pin 13

int LDRvalue = 0; declares another integer variable to store the value of the LDR

void setup() is where you set up the board's pin identity.


Serial.begin(9600); establishes serial communication between your Arduino board and another device.This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second. 

pinMode(ledPin,OUTPUT); means that pin 13 is the output.


void loop() means that the code runs on a loop.


int sensorVal = digitalRead(13); means that it reads the input of pin 13 and writes into a integer variable, sensorVal.


Serial.println(sensorVal); means it displays the value of sensorVal if it is pressed.

LDRvalue = analogRead(LDR); refers to reading the voltage analog value through the A0 pin of the Arduino. This analog Voltage will be increased or decreased according to the resistance of LDR.

if(LDRvalue > 600) means that LDR valye 

digitalWrite(ledPin, HIGH);  means pin 13 is set to 1, making pin 13 HIGH, causing the LDR to ON.

else

digitalWrite(ledPin, LOW); means pin 13 is set to 0, making pin 13 LOW, causing the LDR to OFF.




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

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/172726?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequences.api.brightspace.com%2F172726%2Factivity%2F5399005%3FfilterOnDatesAndDepth%3D1

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

When we first connected the set-up, we could not make the light dependent resistor work on a continuous loop. This was due to the fact that we did not include the void loop. We realised the mistake after looking through the code, and changed it.



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

void setup() {

  pinMode(8,OUTPUT);

  pinMode(12,OUTPUT);

  pinMode(13,OUTPUT);

}

void loop() {

  digitalWrite(8,HIGH);

  delay(1000);

  digitalWrite(8,LOW);

  delay(1000);

  digitalWrite(12,HIGH);

  delay(1000);

  digitalWrite(12,LOW);

  delay(1000);

  digitalWrite(13,HIGH);

  delay(1000);

  digitalWrite(13,LOW);

  delay(1000);

}

void setup() is where you set up the board's pin identity.

pinMode(8,OUTPUT); means that pin 8 is set as an output.

pinMode(12,OUTPUT); means that pin 12 is set as an output.

pinMode(13,OUTPUT); means that pin 13 is set as an output.

void loop() means that the code runs on a loop.

digitalWrite(8,HIGH); increases pin 8 to 1, meaning HIGH, turning on the LED.

delay(1000); means there is a delay of 1 second.

digitalWrite(8,LOW); decreases pin 8 to 0, meaning LOW, turning off the LED. 

delay(1000); means there is a delay of 1 second.

digitalWrite(12,HIGH); increases pin 12 to 1, meaning HIGH, turning on the LED.

delay(1000); means there is a delay of 1 second.

digitalWrite(12,LOW); decreases pin 12 to 0, meaning LOW, turning off the LED. 

delay(1000); means there is a delay of 1 second.

digitalWrite(13,HIGH); increases pin 13 to 1, meaning HIGH, turning on the LED.

delay(1000); means there is a delay of 1 second.

digitalWrite(13,LOW); decreases pin 13 to 0, meaning LOW, turning off the LED. 

delay(1000); means there is a delay of 1 second.



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

https://youtu.be /X8dHbdhnGKY



https://www.youtube.com/watch?v=X8dHbdhnGKY


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

We had not included the delay , causing all the 3 LED to light up at the same time and off at the same time. Then we added the delay of 1 second, causing it to work perfectly.



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





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 writeable format

 Explanation of the code

void setup() {

 

  pinMode(8, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(13, OUTPUT);

  pinMode(2,INPUT_PULLUP);

 

}

 

 

void loop() {

  byte buttonstate = digitalRead(2);


  if (buttonstate == LOW){

  digitalWrite(8, HIGH);  

  delay(1000);                      

  digitalWrite(8, LOW);    

  delay(1000);                      

  digitalWrite(12, HIGH);  

  delay(1000);                      

  digitalWrite(12, LOW);    

  delay(1000); 

  digitalWrite(13, HIGH);  

  delay(1000);                      

  digitalWrite(13, LOW);  

  delay(1000);                     

  }

 else{

  digitalWrite(8,LOW);

  digitalWrite(12,LOW);

  digitalWrite(13,LOW);

}

}

void setup() is where you set up the board's pin identity.

pinMode(8, OUTPUT); means that pin 8 is set as the output.

pinMode(12, OUTPUT); means that pin 12 is set as the output.

pinMode(13, OUTPUT); means that pin 13 is set as the output.

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.

void loop() means that the code runs on a loop.

byte buttonstate = digitalRead(2); This variable stores the current digital state of the button while it reads from a specific digital PIN, PIN2.


if (buttonstate == LOW) means the button is released.

digitalWrite(8, HIGH);  increases pin 8 to 1, meaning HIGH, turning on the LED.

delay(1000); means there is a delay of 1 second.                     

digitalWrite(8, LOW);  decreases pin 8 to 0, meaning LOW, turning off the LED.  

delay(1000); means there is a delay of 1 second.                      

digitalWrite(12, HIGH); increases pin 12 to 1, meaning HIGH, turning on the LED.

delay(1000); means there is a delay of 1 second.                     

digitalWrite(12, LOW); decreases pin 12 to 0, meaning LOW, turning off the LED.   

delay(1000); means there is a delay of 1 second.

digitalWrite(13, HIGH); increases pin 13 to 1, meaning HIGH, turning on the LED.

delay(1000); means there is a delay of 1 second.

digitalWrite(13, LOW);  increases pin 13 to 0, meaning LOW, turning off the LED.

delay(1000);  means there is a delay of 1 second.                   

digitalWrite(8,LOW); decreases pin 8 to 0, meaning LOW, turning off the LED.

digitalWrite(12,LOW); decreases pin 12 to 0, meaning LOW, turning off the LED.

digitalWrite(13,LOW); decreases pin 13 to 0, meaning LOW, turning off the LED.


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

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/172726?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequences.api.brightspace.com%2F172726%2Factivity%2F5399005%3FfilterOnDatesAndDepth%3D1


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

As there were many wires involved, we had swapped positions of 2 wires. Moreover, as this activity included a button, it was difficult to figure out to run the code with the button function , as we already  had done it without the button in the previous activity. However, due to my team mates perseverence , we managed to solve the problems.


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





Below is my Learning Reflection on the overall Arduino Programming activities

Reflection of 1st part of the arduino programming activities

On the overall, the Arduino Programming activities were extremely mind-boggling and difficult to do. As this was my first time doing coding, this whole process was also a scary experience, as I know it is graded, and I had to do well in it. When I looked at the tutorial and explanations of all the codes, they all seem alien to me and I could not figure anything out. However, when I started to take my own time to look at the commands one-by-one, I was able to understand the codes one-by-one. Moreover , combining the codes was also another difficulty I faced as I often ended up making mistakes. For example, when I was doing 'Make Some Noise' for part 1 of the arduino programming, there was a time I could not make the program load due to a certain error. This error included not adding a certain code. Therefore, I had to type the code to input into the programming. It took quite some time to type it. However, I managed to finish the activity. 

Reflection of the practical activity 

As we already done our pre practical activities for the practical, we were pretty much confident that we would ace the practical. However, this time , we also had to build our pegasus from the cardboard pieces provided by the lab. This part was a little difficult as we had to figure out the correct alignment of the parts of the cardboard. Moreover, the cardboard had little fittings for the cardboards to be inserted into each other. It was really difficult for one cardboard parts to be fitted into another cardboard part due to the extremely small gap provided. Moreover, we also could not force to fit the cardboard part into the other cardboard part as we were provided only one cardboard set of the pegasus and was scared to damage the cardboard pieces. However, after some thinking, I decided to use a ruler to bend the opening of the cardboard fitting to allow some space for the other cardboard fitting to be inserted into. This taught me to persevere through the activity, as I should not give up when faced with a problem, and that I should come up with another way to do the activity. Moreover, we also had to colour the pegasus and decorate it.When we were doing the coding , we wanted to make the pegasus flap its wings and make some music. However, we could only come up with the code to make the pegasus flap its wings, as we had faced a lot of issues with coming up with the code to make the music. We had almost spent 2 hours trying to correct the quote to make the music. We tried many different possibilities but had failed in the end. Moreover, we had also tried inserting the servo motor into the pegasus to make it hit the wings to flap the wings. However, the servo motor kept on dropping onto the ground, causing us to change the method of making the pegasus flap its wings. We decided to connect a metal wire to the inside part of the wings to the servo motor which will be located outside the pegasus , on the cardboard piece. This was better,as the servo motor would also not damage the cardboard pieces. This also improved our problem solving skills as we had to come up with different possibilities until we reached the solution. This is an important skill I should acquire in chemical engineering as I would be facing many problems in the plant, and I would have to find many ways to achieve the solution. 

Code used by our group

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
#define PIEZO 8
#define TEMPO 1500

#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_D5  587

int noteDurations[] = {
    4, 4, 8, 8, 8, 8, 4,
    4, 4, 4, 8, 8, 8, 8, 4,
    4, 4, 4, 8, 8, 8, 8, 4,
    4, 8, 8, 4, 4, 4, 2, // 30

    4, 4, 4, 4, 2,
    4, 4, 4, 4, 2,
    4, 4, 8, 8, 8, 8, 4,
    4, 8, 8, 4, 4, 4, 2, // 24

    4, 4, 8, 8, 8, 8, 4,
    4, 4, 4, 8, 8, 8, 8, 4,
    4, 4, 4, 8, 8, 8, 8, 4,
    4, 8, 8, 4, 4, 4, 2, // 30
  };
int melody[] = {
    NOTE_D4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_E4,
    NOTE_E4, NOTE_E4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_FS4,
    NOTE_D4, NOTE_D4, NOTE_B4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
    NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_A4, NOTE_FS4, NOTE_G4,

    NOTE_D4, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_FS4,
    NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_D4,
    NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_D5,
    NOTE_D4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_A4, NOTE_FS4, NOTE_G4,

    NOTE_D4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_E4,
    NOTE_E4, NOTE_E4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_FS4,
    NOTE_D4, NOTE_D4, NOTE_B4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
    NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_A4, NOTE_FS4, NOTE_G4,
  };


void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  for (int thisNote = 0; thisNote < 84; thisNote++) {
    int noteDuration = TEMPO / noteDurations[thisNote];
    tone(PIEZO, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(PIEZO);
  }

  delay(2000);
}


void loop() {
  for (pos = 0; pos <= 150; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(4);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 150; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(4);                       // waits 15 ms for the servo to reach the position
  }
}

Comments

Popular posts from this blog

About me