The hardware
When building a robot, it is necessary to have a hadware where all the code will be inserted, so later on, this hardware will be responsible of the control of the robot. In our case, we choose the arduino uno, a signgle-board mocrocontroller which can be programmed with all the computers. The arduino also has other interesting features, such as the cappability of applying the canges of the code instantaneusly while connected to the computer.
Finally, we chose this type of arduino because, once the microchip is programmed, it is possible to unplug it from the board and attach it on the pcb or wherever you need.
The software
Arduino company has also it's own sofware to programm the board. It's language is really similar as the C language, so if you know a little bit of programmation, it will be easy for you to understand most of the code we wrote down to programm the robot
The code
The code used to programm the arduino is the following:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
int a=0; // variable to make the leds blink
int on=0; // variable that tells if the robot is on or off
int b=1; // variable set randomly between 1 or 4 to make the robot dance
int n=0; // variable that reads the command from the bluetooth
int flag = 0; // make sure that you return the state only once
void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(12); //the pin for the servo control
servo1.attach(11); //the pin for the motor1
servo2.attach(10); //the pin for the motor2
Serial.println("First Test:"); // so I can keep track of what is loaded
}
void loop() {
//if some data is sent, read it and save it in the n variable
if(Serial.available() > 0){
n = Serial.read();
flag=0;
}
//if the data recieved is 1, it will set the variable on to 1 and will turn on the leds, and will place
the main motor on the on position
if (n == '1')
{
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
myservo.write(15);
delay (500);
on=1;
if(flag == 0){
Serial.println("Charging mechanisms");
flag = 1;
}
}
//if the data recieved is 0, it will set the variable on to 0 and will turn off everything
else if (n == '0')
{
if(flag == 0){
Serial.println("Shutting down robot");
flag = 1;
myservo.write(180);
}
digitalWrite(8, LOW);
digitalWrite(9, LOW);
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1500);
delay (500);
on=0;
}
//if the data recieved is 2, it will make the motors go ahead and blink the leds in on and off
else if ((n=='2') && (on==1))
{
servo1.writeMicroseconds(1300);
servo2.writeMicroseconds(1700);
if(flag == 0){
Serial.println("Go ahead");
flag = 1;
}
if (a==0){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay (100);
a=1;
}
else if (a==1){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay (100);
a=0;
}
}
//if the data recieved is 3, it will make the motors go back and blink the leds in on and off
else if ((n=='3') && (on==1))
{
servo1.writeMicroseconds(1700);
servo2.writeMicroseconds(1300);
if(flag == 0){
Serial.println("Go back");
flag = 1;
}
if (a==0){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay (400);
a=1;
}
else if (a==1){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay (400);
a=0;
}
}
//if the data recieved is 4, it will make therobot go right
else if ((n=='4') && (on==1))
{
servo1.writeMicroseconds(1550);
servo2.writeMicroseconds(1500);
if(flag == 0){
Serial.println("Go right");
flag = 1;
}
if (a==0){
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay (500);
a=1;
}
else if (a==1){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay (500);
a=0;
}
}
//if the data recieved is 5, it will make the robot go left
else if ((n=='5') && (on==1))
{
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1550);
if(flag == 0){
Serial.println("Go left");
flag = 1;
}
if (a==0){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay (500);
a=1;
}
else if (a==1){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay (500);
a=0;
}
}
//if the data recieved is 6, it will make the motors stop
else if ((n=='6') && (on==1))
{
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1500);
if(flag == 0){
Serial.println("Stop");
flag = 1;
}
if (a==0){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay (300);
a=1;
}
else if (a==1){
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay (300);
a=0;
}
}
//if the data recieved is 7, it will make turn randomly in 4 different positions and the same for the leds
else if ((n=='7') && (on==1))
{
if(flag == 0){
Serial.println("Dance");
flag = 1;
}
if (b==0){
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1550);
myservo.write(90);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay (random (600));
b=(random (3));
}
if (b==1){
servo1.writeMicroseconds(1450);
servo2.writeMicroseconds(1550);
myservo.write(60);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delay (random (600));
b=(random (3));
}
if (b==2){
servo1.writeMicroseconds(1550);
servo2.writeMicroseconds(1500);
myservo.write(40);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay (random (600));
b=(random (4));
}
if (b==3){
servo1.writeMicroseconds(1450);
servo2.writeMicroseconds(1450);
myservo.write(15);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay (random (600));
b=(random (3));
}
}
}