Arduino Programming

For this week's practical my group and I were programming an arduino board to control a servo to allow for automation of a unicorn toy.

We took a design of a unicorn which had a movable wing and we used a combination of a servo, Arduino board and a metal wire to achieve the mechanism. The code uses a simple edit of a already given sweep code by Arduino 
Code:
#include <Servo.h>

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

int pos = 10;    // 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 = 10; pos <= 120; pos += 10) { // goes from 10 degrees to 120 degrees
    // in steps of 10 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(25);                       // waits 25ms for the servo to reach the position
  }
  for (pos = 120; pos >= 10; pos -= 10) { // goes from 120 degrees to 10 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 10ms for the servo to reach the position
  }
}

Overall quite a simple program and we have achieved quite great results from it. The program sweeps from initial postion 10 to 120 and then back to 10 to achieve this unicorn mechanism.

Arduino Challenges

So for the Arduino tasks I was assigned to do was 4 tasks, 2 input and 2 output
For the input they were

-  Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE
- Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE

And for the output the tasks were to

- Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)
- Interface the DC motor to maker UNO board and program it to on and off using push button on the board

Task 1

All of the tasks will be done on the Tinkercad website here : https://www.tinkercad.com/

So for the first task was the potentiometer analog input which a video was provided thankfully here,




This was my first attempt
So as you can see the wires are really messy and even I can't really tell which wire is connected to where and I can't imagine to circuit in my head. Therefore to fix this, I rewired the whole thing and added the LED part.
Now the only thing left is to program it

This is what it should look like using the simplified way of programming in Tinkercad and here's the thing in action

And here is the code:
// C++ code
//
int SensorValue = 0;

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

void loop()
{
  // Read Value from sensor
  SensorValue = analogRead(A0);
  // Now turn on the LED
  digitalWrite(LED_BUILTIN, HIGH);
  // Pause the program for <SensorValue> millisecond
  delay(analogRead(A0)); // Wait for analogRead(A0) millisecond(s)
  // Turn LED off
  digitalWrite(LED_BUILTIN, LOW);
  // Pause the program for <SensorValue> millisecond
  delay(analogRead(A0)); // Wait for analogRead(A0) millisecond(s)
}

Thoughts

This first task was alright in difficulty, I am not really that good at programming and the video really helped in both the wiring and coding. From this I learned how to code a simple input device with a  varaible. The code should be quite simple to understand from the comments except for the setup. int SensorValue = 0 is basically setting a variable to the A0 slot.
Task 2

So now we will be doing the LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.
So firstly I looked up how to put in a LDR into my circuit as I am not very good at wiring and what not and I found this person's project and used it to help me : https://create.arduino.cc/projecthub/tarantula3/using-an-ldr-sensor-with-arduino-807b1c
This was how he arranged the LDR so I duplicated the same design from my first task and replaced the potentiometer with the LDR setup instead. Here's how it looks like
I moved around a few wires from the previous potentiometer setup and added a resistor to make it work. I did not change the program at all as there was no need to so it is the exact same as in the first task. Now here's a video of it in action.


Here's the code for people too lazy to scroll up:
// C++ code
//
int SensorValue = 0;

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

void loop()
{
  // Read Value from sensor
  SensorValue = analogRead(A0);
  // Now turn on the LED
  digitalWrite(LED_BUILTIN, HIGH);
  // Pause the program for <SensorValue> millisecond
  delay(analogRead(A0)); // Wait for analogRead(A0) millisecond(s)
  // Turn LED off
  digitalWrite(LED_BUILTIN, LOW);
  // Pause the program for <SensorValue> millisecond
  delay(analogRead(A0)); // Wait for analogRead(A0) millisecond(s)
}

Thoughts
I still do not fully understand circuits and my experience so far with a breadboard was a simple 3 week course 5 years ago so editing the circuit was very frustrating but in the end it worked out. From this I learnt how to change a variable like an LDR in a Arduino circuit.

Task 3

Now we will move on to the third task which is interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)
So firstly I setup a simple circuit with LEDs connecting to PINs 11,12 and 13.
So now I'll code it to blink just like a traffic light. Here's the simplifed version in Tinkercad.
Basically it turns each light on for a set amount of time and then turns it off before the next light goes off then the cycle repeats itself making it look like a traffic light. 


Here is the code :
// C++ code
//
void setup()
{
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  // PIN 11 turns on first
  digitalWrite(11, HIGH);
  // Let's leave it on for 5 seconds
  delay(5000); // Wait for 5000 millisecond(s)
  // Now we turn the green light off
  digitalWrite(11, LOW);
  // PIN 12 turns on next
  digitalWrite(12, HIGH);
  // Let's leave it on for 1 seconds
  delay(1000); // Wait for 1000 millisecond(s)
  // Now we turn the yellow light off
  digitalWrite(12, LOW);
  // PIN 13 turns on next
  digitalWrite(13, HIGH);
  // Let's leave it on for 4 seconds
  delay(4000); // Wait for 4000 millisecond(s)
  // Now we turn the red light off
  digitalWrite(13, LOW);
}

Thoughts
This one was nice the circuit is very easy to setup, just make sure the resistors are the correct resistance (220 Ohms). The code was fairly simple also its just 3 things lighting up in order just have to copy and paste the first one 2 more times and editing the time value for each. I leant how to do some LED coding from this task.

Task 4

So this is the final task and I have to interface the DC motor to maker UNO board and program it to on and off using push button on the board.
I have had a few experiences with dc motors so the wiring was not that much of a challenge
Here's what it should look like and here it is in action


By changing a few variables I was able to code for this dc motor.


Here is the code : 
// C++ code
//
const int button = 13;
const int motor = 12;
int motorflag = 0;
void setup() {
  pinMode(motor, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  if (digitalRead(button)==HIGH){ // if button is pressed
    if (motorflag==0) {             // and the status flag is LOW
      motorflag=1;                  // make status flag HIGH
      digitalWrite(motor,HIGH);     // and turn on the motor
      }                           // 
    else {                        // otherwise...
      motorflag=0;                  // make status flag LOW
      digitalWrite(motor,LOW);      // and turn off the motor
    }
  delay(1000);                    // wait a sec for the 
  }                               // hardware to stabilize
}                                 // begin again

Thoughts
Had to think for this one as I had never done a double if in a code before so it took me a while to understand the code but the comments were good in helping me understand. I learnt how to program a button for a function after doing this.

Reflection
Overall I feel that after doing the tasks and practical that programing is a very important in building products and almost all products have some form of programming in it. I will keep this in mind when building my product in the future. I also feel that programming is quite fun and I wouldn't mind programming for my team next time. My programming might not be that advanced now but I have hopes to improve it.
Well thats all for this blog. More to come!

No comments:

Post a Comment

Week 2: Laser Cutting

  Hello everyone. This is the second week of documentation and this week we learnt about laser cutting. Its a really useful tool, being able...