Computer and Arduino controlled car (1:18)
Thursday, February 18th, 2010So I decided to make something with RC-servos using the Arduino board and the sensor shield which I recently purchased.
I went ahead figuring out how to send arrow signals from my computer to Arduino using USB interface.
Using void setup() { Serial.begin(9600); } on Arduino and screen /dev/ttyUSB0 9600 on my computer, I managed to send commands back and forth. I hooked up the standard servo library and write some code before I mounted the Arduino board on my mini rock crawler. I now had a computer controlled car. Because of Arduino`s simple interface I had it all up and running around an hour. Check the small video and code below.
You can view and or use the code as you like below. (Sorry about the indention, WordPress messes it up).
// LIBRARY
//
#include <Servo.h>
//
// OBJ
//
Servo servo1;
Servo servo2;
// VARS
int readByte;
int servo1Angle = 90; //default servo angle
int servo2Angle = 90;
int minPulse = 700; // minimum servo position
int maxPulse = 2300; // maximum servo position
void setup()
{
servo1.attach(2, minPulse, maxPulse); //connect servo
servo2.attach(3, minPulse, maxPulse);
Serial.begin(9600); // start serial
Serial.println(“Ready\n”);
}
void loop()
{
if (Serial.available() > 1) // procced when two bytes is avaiable
{
readByte; = Serial.read(); //read first byte
if (readByte; == 91)
{
readByte; = Serial.read(); //read second byte to determine arrow type
if (readByte; == 65 && servo1Angle <= 180) //UP
{
servo1Angle += 5;
}
else if(readByte; == 66 && servo1Angle >= 0) //DOWN
{
servo1Angle -= 5;
}
else if(readByte; == 67 && servo2Angle <= 180) //RIGHT
{
servo2Angle += 5;
}
else if(readByte; == 68 && servo2Angle >= 0) //LEFT
{
servo2Angle -= 5;
}
}
}
// set servo positions
servo1.write(servo1Angle);
servo2.write(servo2Angle);
delay(15);
}







