BUS4TN007-3 / tehtävä 5 (assignment 5)

This is my fifth assignment where we try to build a working small project with a servo. The servo that I will use is made by Tower Pro. The name of the servo is Micro Serve 9g SG90 The range is 0 – 180 degrees  where maximum speed is 60 degrees/s. I will also use the component from my previous assignment where the sensor was HC-SR04 (picture below).

IMG_0213[1]

When I started this assignment, the first problem I encountered was that the electricity that is generated from one pin in arduino is not sufficient enough for both the sensor and the servo so I had to divide them in their own pins because when I tried it on the breadboard it did not work properly. The wire colors in the servo are brown (Ground/GND/-), red (5v/+) and orange (For the signal). To use the servo, you must import the Servo.h library to the arduino software.

#include <Servo.h>

Before I start, please take time to take a look at my older assignment so you will understand what is happening in this assignment because I will use recycled code from my older posts. 

Below I have posted the first phase of my testing with this servo, where the servo moves(degrees) based on the value it gets from the Ultrasonic sensor where the max value is 181 and lowest is 0 degrees. This test is for the later project where I will code the values for the robot walker/car. The code can include maneuvers in algorithmic order for example “if the object is too close –> STOP –> move slightly back –> STOP –> move right –> etc “. The logic how the robot will move is based on the components you will be using to change the direction of the robot. For example if you were to use wheels instead of legs, the code would be different. You can also load your robot with sensor so it would be safer if it was one of the bigger projects where the robot would be the size of industrial bots. Anyway, here is the code. Note that if you are new to programming languages, the compiler will ignore everything in one line after the // comment. This way you can take notes for yourself in the compiler itself. The same is with Java and C++.


#include <Servo.h> // Library

#define trigPin 13

#define echoPin 12
Servo servo; // The making of servo variable
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(11); // Attach variable to pin 11
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 181 || distance <= 0){
Serial.println(“Out of range”);
}
else {
Serial.print(distance);
Serial.println(” cm”);
servo.write(distance); // move the servo with distance

}
delay(2000); // delay 2 seconds
}

Here is some reference for the codes where attach() means

Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.

and write()

Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).

If you are new to arduino, then I suggest you to look a closer look at the syntax example at their own website with broader information. Below is the picture of the components that I used for the above code.

IMG_0208[1]

Here is the video

 

 

 

With the components of the above picture/video, I modified the code to make this servo to mimic a walking robot (You would of course need two servos). The code is below. You can see that the servo will stop completely when an object comes closer than 5 cm in front of it. We can make the servo to do anything for example in the STOP section we could have created a reference to another method where if the object comes too close, turn and run. There the only limit that you have is the hardware and the programming language.

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 5){
Serial.println(“STOP”);
}
else {
Serial.print(distance);
Serial.println(” cm”);
walk();

}
delay(500);
}

void walk()
{
servo.write(0);
delay(1000);
servo.write(180);
}

By the way, if you are completely new to arduino, I suggest you to take a look at the arduino example code library from the File –> Examples to get the basic idea of the logic behind every added component. You can also monitor the values and your own writings to the console from Serial monitor in the tools –> Serial monitor. I must also add that although this is not a programming class, it’s a good idea to always manage your code in smart way by not duplicating your code when you can create a method and reference that one method that has ten lines of code every time when you need the same functionality. The code in the assignment example has a method walk() and the way it works is explained below

void walk()
{ // Opening brackets
// All of the codes, must be inserted here
} // Closing brackets

and whenever you want to use this method you can reference in in other methods or loops by just adding

walk();

The method can have some functionality that does something, like in our case or to do some function with the values it receives inside the void walk(value){ }.Although I do not always follow my own advice’s they are known set of good programming standards to learn. You might also use operators on methods for example instead of using one method four times where the bot will walk few meters in every time a walk(); method is activated you can multiply the method by one activation but I am going to go in depth in the arithmetic functionality in another post. I can tell you this that the programming language in Arduino software is similiar to C with few exceptions.

I will update this assignment gradually.

Source:

Lectures on Tero Karvinen
Make: Arduino Bots and Gadgets by Kimmo and Tero Karvinen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s