Arduino programming

Arduino Programming

For this week's CPDD lesson, we are taught to do Arduino programming on the Uno maker board. since the whole class was new to this skill, everyone had to complete 4 task to give us an idea about Arduino programming before we attend the practical. 

Activity 1: Hello World!

This activity required us to program the Arduino board so that there is a single light that will blink consistently.

Code:

/*

  Blink

 

  Turns an LED on for one second, then off for one second, repeatedly.

 

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO

  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to

  the correct LED pin independent of which board is used.

  If you want to know what pin the on-board LED is connected to on your Arduino

  model, check the Technical Specs of your board at:

  https://www.arduino.cc/en/Main/Products

 

  modified 8 May 2014

  by Scott Fitzgerald

  modified 2 Sep 2016

  by Arturo Guadalupi

  modified 8 Sep 2016

  by Colby Newman

 

  This example code is in the public domain.

 

  http://www.arduino.cc/en/Tutorial/Blink

*/

 

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(LED_BUILTIN, OUTPUT);

}

 

// the loop function runs over and over again forever

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(1000);                       // wait for a second

  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

  delay(1000);                       // wait for a second

}

Video for demonstration:

https://youtu.be/kdOCF17SDhw

 

Challenge Yourself:

This activity is similar to the "Hello World" activity except there is a twist

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(3, OUTPUT);
}
 
// the loop function runs over and over again forever
void loop() {
  digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}



Activity 2: Programmable button

In this activity we had to program the Arduino board so that by click the button on the board it would make the light light up.

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);

 

}

 

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 {

    digitalWrite(13, HIGH);

  }

}

Video link for demstration:

https://youtu.be/VxBK7-Wy2Mc

 

Challenge yourself:

The twist that was added to this challenge was that we had to program the board so that with a press of the button, the light would blink 5 times.

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);

 

}

 

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 {

    //ON PIN13 FOR 5 MS AND OFF FOR 5MS

      digitalWrite(13, HIGH);

      delay(500);

      digitalWrite(13, LOW);

      delay(500);

 

 

}

}

Video link for demonstration:

https://youtu.be/4KPleiagl0M

 

Activity 3: Make some noise!

For this activity we have to program the board to make music.

Code:

/*

  Melody

 

  Plays a melody

 

  circuit:

  - 8 ohm speaker on digital pin 8

 

  created 21 Jan 2010

  modified 30 Aug 2011

  by Tom Igoe

 

  This example code is in the public domain.

 

  http://www.arduino.cc/en/Tutorial/Tone

*/

 

#include "pitches.h"

 

// notes in the melody:

int melody[] = {

  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4

};

 

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {

  4, 8, 8, 4, 4, 4, 4, 4

};

 

void setup() {

  // iterate over the notes of the melody:

  for (int thisNote = 0; thisNote < 8; thisNote++) {

 

    // to calculate the note duration, take one second divided by the note type.

    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

    int noteDuration = 1000 / noteDurations[thisNote];

    tone(8, melody[thisNote], noteDuration);

 

    // to distinguish the notes, set a minimum time between them.

    // the note's duration + 30% seems to work well:

    int pauseBetweenNotes = noteDuration * 1.30;

    delay(pauseBetweenNotes);

    // stop the tone playing:

    noTone(8);

  }

}

 

void loop() {

  // no need to repeat the melody.

}

Video link for demonstration:

https://youtu.be/_-LxV_3K4y0

 

Challenge yourself:

The twist was that we have to combine both the "Programmable button" and "make some noise" in one code so that by clicking the button, it would produce the music

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);

 

}

 

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) {

 

    //DO NOTHING!

 

  } else {

 

    //PLAY TONE!

 

  }

}

Video link for demonstration:

https://youtu.be/2cezTbGQMqQ

  

Activity 4: Servo

This activity had us program the board to turn on a servo and make it rotate

Code:

/* Sweep

 by BARRAGAN <http://barraganstudio.com>

 This example code is in the public domain.

 

 modified 8 Nov 2013

 by Scott Fitzgerald

 http://www.arduino.cc/en/Tutorial/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

 

void setup() {

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}

 

void loop() {

  for (pos = 0; pos <= 180; 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(15);                       // waits 15ms for the servo to reach the position

  }

  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

    myservo.write(pos);              // tell servo to go to position in variable 'pos'

    delay(15);                       // waits 15ms for the servo to reach the position

  }

}

Video for demonstration:

https://youtu.be/nQrREdZdM90

 

Challenge yourself:

The challenge part makes us program the board so that the servo can rotate back and forth

Video for demonstration
https://youtu.be/mJaoyDHtnlQ


INDIVIDUAL TASK

We were then tasked with an individual assignment which required us to interfaced an input device and also an output device.


INPUT DEVICES

For input device there were 2 tasks assigned, the first being a potentiometer and the second one being a Light Resistant Diode.


POTENTIOMETER

The first task: Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE

I first made a simulation on tinkercad in order to get a rough idea how to set it up later for the Arduino board. 





URL link for tinkercad circuit: 
https://www.tinkercad.com/things/ieQPrPMv2Lq-potentiometer/editel 

By using the code function in tinkercad I created the code using blocks which generated the code.





Code:
// C++ code
//
int BRIGHTNESS = 0;

int sensorvalue = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  sensorvalue = analogRead(A0);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(sensorvalue); // Wait for sensorvalue millisecond(s)
  digitalWrite(LED_BUILTIN, LOW);
  delay(sensorvalue); // Wait for sensorvalue millisecond(s)

Once the code is uploaded to the board and the Arduino board is set up as per shown in tinkercad, the final product will look like this:

URL link for final product:
https://youtu.be/T7NjPOyLxM0   


LIGHT-RESISTANT DIODE (LDR)


For the next task: Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE

This task is actually easier as the code used from the potentiometer can be reused for this task

We start off with making the tinkercad simulation.


URL link for tinkercad circuit: 
https://www.tinkercad.com/things/bW8mF9oDKO8-ldr/editel 

After completing the circuit, we can apply it to the Arduino board and the final product will look like this,
URL link for final product: 
https://youtu.be/0DO127PHGAc


OUTPUT DEVICES


For out devices, there were also 2 tasks assigned, the first being fading 3 LEDs and the second being the DC motor

FADE

The first task: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)

To start off I used the tinkercad to make a simulation and it will look like this 


URL link for tinkercad circuit: 
https://www.tinkercad.com/things/glSx7y7XDjZ-fade/editel 

The code blocks would look like this:



By inputting the code generated by tinkercad, we can upload it to the Arduino board and the final product will look like this.

Code:
// C++ code
//
int brightness = 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)
  }
}

URL link for final product: 
https://youtu.be/rKj19m_Oppk 

DC Motor

The next task: Interface the DC motor to maker UNO board and program it to on and off using push button on the board

This task by far was the most challenging as compared to the previous task there was guiding videos to watch but for this task, there was none provided. 
So, to complete this task I had to do my own research and improvise which to this outcome:

Using the tinkercad we make the simulation which will look like this:



URL link for circuit: 
https://www.tinkercad.com/things/20RMa6ZsnOA-dc-motor

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


The final product will look like this:
URL link for final product: 
https://youtu.be/kW7osPVq0B0


Reflection

For this lesson I learnt about Arduino programming and how it can be applied from a software into the maker UNO board which to me is very intriguing. By using Arduino Programming, it is possible to turn codes into actual functioning mechanisms and products that can be used.
 I used to think that Arduino programming would be extremely challenging and hard to learn but, after actually practicing and using it, now I think it is a extremely useful skill to learn and that it would certainly benefit greatly in my capstone project if I were to master this skill. So next I will try to practice and experiment with other circuits to hone my skills even further in Arduino programming
















No comments:

Post a Comment

Home Page

Hello  This is JeromeπŸ‘¦. My hobby is playing mahjong πŸ€€πŸ€πŸ€‚πŸ€ƒπŸ€„πŸ€…πŸ€†πŸ€‡πŸ€πŸ€πŸ€˜πŸ€™πŸ€‘πŸ€€. My personal goals for this module that i want to achieve ...