Insect Robot

(UPDATED 5.4.2013)

This is my insect robot project. I am actually writing this as I am building my robot from the spare parts since its always best to document while doing something so you can have the accurate information from memory that is fresh. When I started this project I was (and still am) incredibly broke like every other student so I was not sure how I was going to make/start my project let alone finish but I got the spare parts for this project from our course mentor Tero Karvinen.

The parts that I am going to use for this project are

The Arduino Uno MicroController
Ultrasound sensor (HC-SR04)
– Two servos (both of them are HC-311 models)
– iPhone 5 box (R.I.P. Steve Jobs, sorry for making fun of your phones)
– Jumper wires
– Roll of metal
– BreadBoard
– Ducktape (lol)
– Glue

The aim of the project is to create and program an insect robot that would walk and stop if and object comes in front of it and if I have time to program it to change direction if object is in front of it. The hardest part of this project was and still is that I don’t have sufficient amounts of money to buy decent hardware, tools or anything else for that matter to make anything smart even though I am staunch enemy of making excuses but in this example this is the unfortunate reality but I did manage to get a good project all in all.

What I did was that I put the Arduino microcontroller along side with all the necessary hardware inside the iPhone box and I programmed the Arduino to give signals to the servos so that the servos would move the metal pieces that I glued to them, so the whole thing would look like it was walking. The problem I encountered in this project was that how to program this thing so it would move forward.

The problem arises with the servos. How am I to program this thing so it would walk forward when the legs are made of static metal/copper/tin bits not to mention to move direction. Because the project is low budget, the legs are made of solid metal, meaning that this Frankenstein monster does not have knees to lift up, so I had to program this thing to somehow push itself forward. I did found some tips from the book “Make: Arduino and bot gadgets” but I thought to make this thing look more like it was built by me so I created something that is not pretty to watch but it works (on some level).  Below is the picture I took from the side of the robot

side

As you can see in the picture, that I have put some tape under the insects metal legs so it would be able to push itself forward. Without the tape this thing would slip all over the floor and not go anywhere because the metal would not be able to push on solid wood floor. I also was not able to have batteries on board this insect because the legs are very fragile and the legs would just collapse under the pressure. The best method would have been to buy some solid tin or copper tubes but then again this was supposed to be a personal project so the solution was not bad. I only regret that I did not buy a soldering gun  to solder the legs to the servos. Anyway the functionality of this robot is just to walk and stop for 5 seconds if something comes in front of it (more than 20 cm closer) and continue to walk forward if the object has moved that was in front of it. Yes I know it is a bit silly, because if a wall is in front of it, it would just stand in front of it. Below is another picture from the front side of it.


IMG_0250

You can see from the pictures that I duck taped the breadboard on top the the box and attached the ultrasound sensor on top of it. If you want to have more info about both the functionality of the servos and sensors please look for my previous assignments or go to the top of this post for the datasheets. Below is the code

#include <Servo.h> // Library
#define trigPin 8

#define echoPin 9
Servo servo1;
Servo servo2; // The making of servo variable
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo2.attach(12);
servo1.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 <= 10){
delay(5000);
}else {
servo1.write(20); // move the servo with distance
servo2.write(120); // move the servo with distance
delay(1000);
servo1.write(10);
servo2.write(50);
delay(1000);
servo1.write(30);
servo2.write(120);
}
}

Unfortunately as I am writing this and going forward with my project, the legs of the robot have collapsed. I just glued it together so I am not sure how it will turn out later. Below is the video from my previous advancement few hours ago before I broke the robot.

I promise to upload the new video if I get my robot to walk again. By the way, something that I forgot to add in the beginning of my course posts was the link to the existing other peer links in my same class. Here is the link to my course mentors site

http://terokarvinen.com/2012/aikataulu-prototyypin-rakentaminen-bus4tn007-3-kevat-2013#comment-19365

Scroll down to see other people with their own projects in this course. I also did not manage to get the insect up and running since the glue simply is not enough for the legs like that, because I did not attach the legs properly around the servo wings. I will try to make a better project later with more ambitious plans with better equipment.

References:

Make: Bots and Gadgets by Tero Karvinen and Kimmo Karvinen

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

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

In this weeks assignment I had HC-SR04 Ultrasonic sensor that measures distance (Pictures below). I am sorry for the quality of the photos but it is something that we have to live with since I try to use as much as my own material as possible instead of using outside sources because a lot of the background information that I use in my assignments are from online sources and the coursebook that I have mentioned in the sources.

Anyway lets start with the assignment. Again I encourage you to see my older assignments for some background information. So when I started this assignment the very first problem that I encountered again was not being able to find the datasheet for this particular sensor but in the end I did manage to find the datasheet. Click this link to read the datasheet.

IMG_0190[1]

Note in the above picture that there is 4 pins and above the pins there is descriptions that mean the following:

VCC  = 5 volt supply
trig    = Trigger pulse input
echo = Echo pulse output
GND = Ground

IMG_0191[1]

IMG_0192[1]

IMG_0196[1]

So this sensor is all about using ultrasound to measure the distance of the object. The mechanism is close to how bats locate their food, other bats and objects. Basically it sends signal and the input comes when it bounces back from nearby objects. This way the sensor is able to calculate how far the object is. Please do note that it’s not easy to explain how sound moves from someone like me but I do know (Thanks to the internet) some basic things about it.The speed of sound = 343 seconds per meter or c = 343 m/s depending on the temperature of the air among other things. The sensor thus sends ultrasound (The speed depends) “where the measuring angle is 15 degrees”. Other things you should note are

– You should not worry about any health effects for small projects like this

– The sensor sends pulses (Look below and click on it to go to the original source)

pulse

Before I start with the assignment I must admit that I heavily borrowed code from other people because the test code looks pure evil but here is the link. I think that you can calculate the distance of the object by taking humidity into account also, but that is a bit more complicated code and I do not have time to go into that area.

The example code that I will be using is not mine so here is the link to the authors page, because why to invent something that was already invented. I just modified it a little bit, where Arduini will switch on the red LED when the object/s is out of range and when to switch the green LED on when the object is not out of range. The serial monitor print would be something like in the picture below.

serialMonitor

Where the object for example your hand is moving either closer or farther away from the sensor. You can also place an object like cardboard box in front of the sensor and move it slowly away from it. Here is the code bit to calculate the distance of the object.

distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
  Serial.println(“Out of range”);
  digitalWrite(greenPin, LOW);
  digitalWrite(redPin, HIGH);
}
else {
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);
  Serial.print(distance);
  Serial.println(” cm”);
}

Here is few pictures of the wiring itself and again I did the wiring like it is for testing purposes so it was meant to be messy and sorry for the quality of the photo.

IMG_0203[1]

IMG_0202[1]

IMG_0198[1]

And lastly here is the code. I will probably try to decipher the test code and come up with my own code or just make my own functions later but for now this is the loaned code to show the functionality of the HC-SR04 sensor.

#define trigPin 12
#define echoPin 13
const int redPin = 7;
const int greenPin = 8;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}

void loop() {
int duration, distance; // creating variables
digitalWrite(trigPin, HIGH); // pin ON
delayMicroseconds(1000); //delay
digitalWrite(trigPin, LOW); // pin OFF
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
Serial.print(distance);
Serial.println(” cm”);

}
delay(500);
}

In the setup method we start the serial port for displaying the status of the sensor. We also define the bits per second.

“Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates – for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no parity, one stop bit.”

Where the sensor pins are given constant values with defined names.

"#define is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in arduino don’t take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.

This can have some unwanted side effects though, if for example, a constant name that had been #defined is included in some other constant or variable name. In that case the text would be replaced by the #defined number (or text).

In general, the const keyword is preferred for defining constants and should be used instead of #define.”

pinIn() means according to arduino website.

“Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.”

With the digitalWrite() we either give the pin values HIGH (On) or LOW (Off).

sources:

http://winkleink.blogspot.fi/2012/05/arduino-hc-sr04-ultrasonic-distance.html

http://blogs.helsinki.fi/kemma-keskus/

http://en.wikipedia.org/wiki/Ultrasound

https://docs.google.com/document/d/1Y-yZnNhMYy7rwhAgyL_pfa39RsB-x2qR4vP8saG73rE/edit?pli=1

http://www.satistronics.com/myfiles/file/Module/About%20UltracsonicModule.pdf

http://www.ebay.com.au/itm/Arduino-Ultrasonic-Ranging-Module-HC-SR04-Detector-/320588905524#ht_3014wt_1163

Make: Arduino Bots and Gadgets by Tero and Kimmo Karvinen

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

GROUP ASSIGNMENT

This is the assignment two where the objective was to install a sensor that we loaned from university. There was 6-7 different sensors available from distance sensors to Parallax 27401 that is a infrared receiver and emitter that we had as our group assignment where this particular sensor recognized the dimmer lighting. The sensor then outputs values depending on the lighting on the surface of it’s receivers. Unfortunately I did not have the opportunity to take a picture of the installed sensor but I will post it on later time, If will ever get my hands on it. The sensors are easy to use in the beginning so you can try it yourself, with the same materials as I have. Look for more information about the sensor that my group used from the web pages below.

LINK 1


LINK 2

Like I said, that we had many other sensors as well. The below link basically has some of the sensors that we used in the lab. If I remember correctly we had other sensors as well.

LINK 3

So to the assignment itself that we had to work on. Unfortunately, because I did not buy my own sensors, I had to loan from my university because of these reasons and huge number of students that also loaned the sensors, my group had to work on the sensors that were available in the lab namely the switch or button if you will.
We also had few LED’s and jump wires and switch board. In the first picture as you can see, there is no need for codes for it to work. The switch takes power from the Arduino board and basically, it will distribute the electricity to the LED when someone pushes the button.

IMG_0095

In the picture below a group member tries the switch button and as you can see, it works. Now again I have to emphasize that the wiring is not perfect.

IMG_1511

Now in the previous example we did not really have any code to take input and output but in the next picture as you can see, the led is connected to Arduino through the pin directly where to control it, you would need some sort automated software like I had in the previous assignment or through user interaction like switch that is connected to Arduino. Arduino then recognizes, in what state the switch is by taking input from the switch as values, and determines what to do with the LED that is connected through the code.

Its nothing more than if, else if statement machine.
I think that no matter what the action is that we are supposed to achieved with this Arduino is all about booleans meaning if something is true then do something and if the value is not what we wanted, do something else. By the way I thought it would be good to post some switchboard logic that I learned from a classmate if someone still has some problems with it.

IMG_0100

As you can see in the post above, I have drawn green lines. When you connect the jumper wires the logic works in a way that the red jumper wire in my case brings the electricity in meaning that from the third pin onward it will power the line and the switch and from the last leg of the switch the electricity will flow when the button is pushed through the whole line where the black jumper wire is connected. This way you can connect the GROUND to one line and connect many grounds on one line if needed.

 

I am going to post the code below but it is not without error’s but building prototype’s never was about creating fully working products right? here is the code without further delay

const int buttonPin = 12;
const int ledPin = 13;
int buttonState = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}void loop(){

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}

The source that I used to help me connect the Arduino is below

http://arduino.cc/en/tutorial/button

Last but not least heres the link to the person to my classmates blog with whom I worked with.

p.s. I will update the blog post later with more content.

Prototyypin rakentaminen / BUS4TN007-3 / Tehtava1 (assignment 1)

Hello,

This is my first post/blog in the WordPress site. I am currently studying at Haaga-Helia University of Applied Sciences. My major is Information Technology
(Degree Programme In Information Technology), and this course is about trying to build small prototypes (or bigger ones if you are up to it). Here is the outcome for of my assignment. Please excuse me the quality and angle of the photo.

IMG_0093[1]

IMG_0092[1]

The first assignment was to build a small code for Arduino to control the LED’s/LED.
The Arduino that is used for this assignment is Arduino Mega 2560.
The book that was assigned as our reading material is called
“Make: Arduino Bots and Gadgets” by Kimmo Karvinen & Tero Karvinen.

For the first assignment I tried the code and software for my Windows 7. The first hour I encountered my first  problem namely the grayed out ‘Serial Port’,

where choosing the port for Arduino was crucial to make the code work. I did manage to find help for my first problem from the source given below

http://emayssat.wordpress.com/2012/03/29/installing-windows-7-on-the-arduino-ide-or-vice-versas/ 

When I first started the led project I had no Idea, what I should do my project about so I thought to make Morse-code of my own name. In no way does the program follow the International Morse Code standards but you will get the idea when you implement the code. The code itself is ridiculous, but just for the sake of the assignment and practice I thought to start it easy. Here is a Wikipedia source for more information about the Morse Code

http://en.wikipedia.org/wiki/Morse_code

Last but not least, here is the link to my code. I have posted the whole code below this blog also.

http://www.2shared.com/file/gv2vagPF/Morse_Code_KUROSH.html

The compiler is version 1.5.1, and you can download it from their main page

http://arduino.cc/en/main/software

My next project will be a robot car. The sensor that would be good for this project is
direction sensor module, but I still have to think more about the project and the components that I will/have money to use in it but here is the link for starters.

http://dx.com/p/robox-arduino-compatible-ir-direction-sensor-module-145084

Here is the code for the Arduino board (UPDATED VERSION)

Updated 26.1.2013

.// The integers below are connected to Digital Pins

int greenPin = 13;     // LED that is connected to pin 13
int redPin = 12;          // LED that is connected to pin 12

void setup()
{
pinMode(greenPin, OUTPUT); // sets the digital pin as output
pinMode(redPin, OUTPUT);
}

void small()
{
digitalWrite(greenPin, HIGH); // sets the LED on
delay(500); // waits for a second
digitalWrite(greenPin, LOW); // sets the LED off
delay(1000);
}

void big()
{
digitalWrite(greenPin, HIGH);
delay(3000);
digitalWrite(greenPin, LOW);
delay(1000);
}

void red()
{
digitalWrite(redPin, HIGH);
delay(2000);
digitalWrite(redPin, LOW);
delay(1000);
}

void k()
{
big();
small();
big();
red();
}

void u()
{
small();
small();
big();
red();
}

void r()
{
small();
big();
small();
red();
}

void o()
{
big();
big();
big();

red();
}
void s()
{
small();
small();
small();
red();
}

void h()
{
small();
small();
small();
small();
red();
}

void loop()
{
k();
u();
r();
o();
s();
h();
}

Where the red LED signifies the change of the alphabet. Now before you start to do anything, you must never add components to your Arduino when it is plugged to your computer because it might damage the Arduino or the components that you are using. This should be common sense. Another thing that I must warn you about is that you should always do a good background research of any electronic component you want to install be it a laptop keyboard or Arduino sensor because you might do something stupid and short circuit the whole thing.