Arduino Programming

 Arduino Programming

Welcome back to my blog, for this week I learned about the Arduino program by coding and using the Arduino board. 




Before the practical, we learnt about the Arduino coding that we used during our practical which we were tasked to code a flapping mechanism to flap the wings of a cardboard unicorn.

The tools given to work with were the Arduino box which we had, a servo and a long malleable wire. Pretty straightforward given the tools, we just had to tie the wire to the wings and hook the wire to the servo that is connected to the Arduino board.
To make the wings flap using the servo, we use the sweep code menu and with the original code on the blackboard which was:
/* 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 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 15 ms 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 15 ms for the servo to reach the position
}
}



Using this as a guideline we modified the code into:

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








For this modified code there aren't any "break" or "exit" keywords hence it is an infinity loop. And the first line of the code "pos = 20; pos <= 150; pos += 1" is the code that controls how much the servo spins and controls which in this case is from 20degrees to 150degrees.  The next line is basically to control how fast the servo moves back in which this case, it moves back in 1 step and 15ms for the servo to return to its original position.


Thinkercad activity:

For the pre-practical we had to do the 4 challenges in blackboard which will be in the link below:
https://docs.google.com/document/d/1GdfKNulm_djlzZ-s1k2GEIE8-pGZjQU-CEOntgXnj_Q/edit?usp=sharing


Next, we have to do input and output devices in for the following,
1. Input devices: 
a. Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE
b. Interface an LDR to maker UNO board and measure 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. Interface the DC motor to maker UNO board and program it to on and off using the button on the board
    

Potentiometer
The potentiometer is a variable resistor that resistance can be adjusted.


the numbers from the video shows     



coding from thinkercad
// C++ code
//
int sensorValue = 0;

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

void loop()
{
  // read the value from the sensor
  sensorValue = analogRead(A0);
  // turn the LED on
  digitalWrite(LED_BUILTIN, HIGH);
  // pause the program for <sesorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause the program for <sesorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

what does code mean
void loop()
{
  // read the value from the sensor
  sensorValue = analogRead(A0);

allows the code to repeat 


 sensorValue = analogRead(A0);
  // turn the LED on
  digitalWrite(LED_BUILTIN, HIGH);
  // pause the program for <sesorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause the program for <sesorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

The code that is responsible for the flashing of the LED, digitalWrite HIGH makes LED turn on the digitalWrite LOW turns LED off




LDR
Light Dependent Resistor (LDR) is a component in which light intensity affects the resistance. When light intensity from a source is high, LDR resistance decreases hence LED will be brighter, vice-versa.
class="separator" style="clear: both; text-align: left;">






code:
// C++ code
//
int photoSensor = 0;

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);

  pinMode(9, OUTPUT);
}

void loop()
{
  photoSensor = analogRead(A0);
  Serial.println(photoSensor);
  analogWrite(9, map(photoSensor, 0, 1023, 0, 255));
  delay(1); // Wait for 1 millisecond(s)
}


what does code mean
int photoSensor = 0;

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);

  pinMode(9, OUTPUT);
}

Input is at pin A0 and Output is at pin A9 which controls the LED


void loop()
{
  photoSensor = analogRead(A0);
  Serial.println(photoSensor);
  analogWrite(9, map(photoSensor, 0, 1023, 0, 255));
  delay(1); // Wait for 1 millisecond(s)
}

For the board to register the LDR and change LED light intensity every 1ms.


Flashing LED




code:
// C++ code
//
void setup()
{
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  digitalWrite(7, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(7, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
  digitalWrite(9, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(9, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
  digitalWrite(11, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(11, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
}

what does code mean:
void setup()
{
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
}

Stating that PINs 7, 9, and 11 are the output for the green, yellow and red LEDs respectively.


void loop()
{
  digitalWrite(7, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(7, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
  digitalWrite(9, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(9, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
  digitalWrite(11, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(11, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
}


void loop()
{
causes string of codes is an infinite loop 



  digitalWrite(7, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(7, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
  digitalWrite(9, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(9, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
  digitalWrite(11, HIGH);
  delay(3000); // Wait for 1000 millisecond(s)
  digitalWrite(11, LOW);
  delay(0000); // Wait for 1000 millisecond(s)
below explains what each line does:

green LED turns on
green LED turns on for 1000ms
green LED turns off
green LED turns off for 0ms
yellow LED turns on
yellow LED turns on for 1000ms
yellow LED turns off
yellow LED turns off for 0ms
red LED turns on 
red LED turns on for 1000ms 
red LED turns off
red LED turns off for 0ms


DC motor
A direct current motor (DC motor) is a motor to converts electrical current to mechanical energy.




code:
int motorPin = 5;
int button = 12;
void setup()
{
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);
  pinMode(motorPin, OUTPUT);
}

void loop(){
  int sensorVal = digitalRead(button);
  Serial.println(sensorVal);
  
  if (sensorVal == HIGH) {
   digitalWrite(motorPin, LOW);
  } else {
    digitalWrite(motorPin, HIGH);
  }
}

what does code mean:
void loop(){
  int sensorVal = digitalRead(button);
  Serial.println(sensorVal);
  
  if (sensorVal == HIGH) {
   digitalWrite(motorPin, LOW);
  } else {
    digitalWrite(motorPin, HIGH);
  }
}

digitalRead(button) checks if the button has been pushed, once pushed, the motor will be tuned by the sensorVal=HIGH.
once the button is pressed again, the motor turns off.


Reflection
All the individual activities have helped me understand the thinkerCAD interface more, especially the input and output with the use of the breadboard and UNO board. At the start, since I have never used thinkerCAD, I was lost and a bit frustrated. Having referred to multiple youtube videos as well as seeking help from peers really made it easier for me to finish the tasks. All in all, I was able to complete and learn a lot from the thinkerCAD.
For the practical at the campus, it was quite joyous as most of the time I was just fooling around by coloring the unicorn as well as designing the background in which the unicorn and motor would be placed. Of course, I did help with the code and contributed with the ideas and my group ended up with rainbow dash from "my little pony" as well as the beat of the theme song. But it was way easier compared to the individual thinekrCAD. Though I did fail the competency test but did manage to pass the second time.













Comments

Popular posts from this blog

LASER CUTTING

Home