here is some code i used for
here is some code i used for my iphone contrlled robot it uses the same thing beat_slayer talked about.
const int left1 = 9;
const int left2 = 10;
const int right1 = 4;
const int right2 = 5;
int enable1 = 11;
int enable2 = 6;
char topchan = ‘a’;
char midchan = ‘b’;
char downchan = ‘c’;
char rightchan = ‘d’;
char leftchan = ‘e’;
char sliderchan = ‘s’;
char serialchar = 0;
void setup()
{
pinMode(left1,OUTPUT);
pinMode(left2,OUTPUT);
pinMode(right1,OUTPUT);
pinMode(right2,OUTPUT);
pinMode(enable1,OUTPUT);
pinMode(enable2,OUTPUT);
Serial.begin(9600);
}
void loop(){
while(Serial.available() <=0); //Wait for a character on the serial port.
serialchar = Serial.read(); //Copy the character from the serial port to the variable
if(serialchar == topchan){ //Check to see if the character is the servo ID for the tilt servo
forward();
//while(Serial.available() <=0); //Wait for the second command byte from the serial port.
//analogWrite(left2,Serial.read());
}
if(serialchar == downchan){ //Check to see if the initial serial character was the servo ID for the pan servo.
back();
//while(Serial.available() <= 0); //Wait for the second command byte from the serial port.
//analogWrite(right1,Serial.read());
}
if(serialchar == rightchan)
{
right();
}
if(serialchar == leftchan)
{
left();
}
if(serialchar == midchan)
{
halt();
}
if(serialchar == sliderchan){ //Check to see if the initial serial character was the servo ID for the pan servo.
while(Serial.available() <= 0); //Wait for the second command byte from the serial port.
int i = Serial.read();
analogWrite(enable1,i);
analogWrite(enable2,i);
}
}
void forward()
{
digitalWrite(left1,LOW);
digitalWrite(left2,HIGH);
digitalWrite(right1,HIGH);
digitalWrite(right2,LOW);
digitalWrite(enable1,HIGH);
digitalWrite(enable2,HIGH);
}
void back()
{
digitalWrite(left1,HIGH);
digitalWrite(left2,LOW);
digitalWrite(right1,LOW);
digitalWrite(right2,HIGH);
digitalWrite(enable1,HIGH);
digitalWrite(enable2,HIGH);
}
void left()
{
digitalWrite(left1,HIGH);
digitalWrite(left2,LOW);
digitalWrite(right1,HIGH);
digitalWrite(right2,LOW);
digitalWrite(enable1,HIGH);
digitalWrite(enable2,HIGH);
}
void right()
{
digitalWrite(left1,LOW);
digitalWrite(left2,HIGH);
digitalWrite(right1,LOW);
digitalWrite(right2,HIGH);
digitalWrite(enable1,HIGH);
digitalWrite(enable2,HIGH);
}
void halt()
{
digitalWrite(left1,LOW);
digitalWrite(left2,LOW);
digitalWrite(right1,LOW);
digitalWrite(right2,LOW);
digitalWrite(enable1,LOW);
digitalWrite(enable2,LOW);
}