Robot car in a grid system (diagonal driving)
hello,
i want build arduino robot car drives in grid (1m x 1m).
the code wrote includes diagonal driving well.
the car gets information via bluetooth.
does code make sense because not possible try out @ moment?
are there other big mistakes?
(sorry, of comments in german.)
// front und als boolsche operatoren
bool front = true;
bool = false;
// deklarieren der einzelnen pins des arduinos
int en1 = 6;
int en2 = 5; //roboduino motor shield uses pin 9
int in1 = 7;
int in2 = 4; //latest version use pin 4 instead of pin 8
// funktion motor 1 - zugriff auf motor 1
void motor1(int pwm, boolean reverse)
{
analogwrite(en1,pwm); //set pwm control, 0 stop, , 255 maximum speed
if(reverse)
{
digitalwrite(in1,high);
}
else
{
digitalwrite(in1,low);
}
}
// funktion motor 2 - zugriff auf motor 2
void motor2(int pwm, boolean reverse)
{
analogwrite(en2,pwm);
if(reverse)
{
digitalwrite(in2,high);
}
else
{
digitalwrite(in2,low);
}
}
// setup-funktion
void setup()
{
int i;
// for(i=6;i<=9;i++) //for roboduino motor shield
// pinmode(i, output); //set pin 6,7,8,9 output mode
for(i=5;i<=8;i++) //for arduino motor shield
pinmode(i, output); //set pin 4,5,6,7 output mode
serial.begin(9600);
}
// loop-funktion
void loop()
{
int x, delay_en;
char val;
if (serial.available() > 0) {
while(1)
{
val = serial.read();
if(val!=-1)
{
switch(val)
{
case 'f':// front - gerade aus
motor1(255,front); // geschwindigkeit laesst sich aendern - 255 austauschen
motor2(255,front);
delay(500);
break;
case 'b':// - nach hinten
motor1(255,back);
motor2(255,back);
delay(500);
break;
case 'r':// right - nach rechts
// erstmal nach rechts drehen
motor1(255,front);
motor2(100,back);
// dann gerade aus fahren
motor1(255,front);
motor2(255,back);
// dann position wieder einnehmen (links drehen)
motor1(100,back);
motor2(255,front);
delay(500);
break;
case 'l':// left - nach links
// erstmal nach links drehen
motor1(255,back);
motor2(100,front);
// dann gerade aus fahren
motor1(255,front);
motor2(255,front);
// dann position wieder einnehmen (rechts drehen)
motor1(100,front);
motor2(255,back);
delay(500);
break;
case 's':// stopp - kein fahren mehr
motor1(0,back);
motor2(0,back);
delay(500);
break;
case 'fr':// front&right - fahrzeug fährt schraeg nach vorne/rechts
// erstmal nach 45° rechts drehen
motor1(128,front);
motor2(50,back);
// dann gerade aus fahren - aber 1,41mal laenger!
motor1(255,front);
motor2(255,front);
// dann position wieder einnehmen (links drehen)
motor1(50,back);
motor2(128,front);
delay(500);
break;
case 'fl':// front&left - fahrzeug fährt schraeg nach vorne/links
// erstmal nach 45° links drehen
motor1(128,back);
motor2(50,front);
// dann gerade aus fahren - aber 1,41mal laenger!
motor1(255,front);
motor2(255,front);
// dann position wieder einnehmen (rechts drehen)
motor1(50,front);
motor2(128,back);
delay(500);
break;
case 'br':// back&right - fahrzeug fährt schraeg nach hinten/rechts
// erstmal nach 45° links drehen
motor1(128,back);
motor2(50,front);
// dann nach fahren - aber 1,41mal laenger!
motor1(255,back);
motor2(255,back);
// dann position wieder einnehmen (rechts drehen)
motor1(50,front);
motor2(128,back);
delay(500);
break;
case 'bl':// front&right - fahrzeug fährt schraeg nach hinten/links
// erstmal nach 45° rechts drehen
motor1(128,front);
motor2(50,back);
// dann nach hinten fahren- aber 1,41mal laenger!
motor1(255,back);
motor2(255,back);
// dann position wieder einnehmen (links drehen)
motor1(50,back);
motor2(128,front);
delay(500);
break;
}
}
}
}
}
i want build arduino robot car drives in grid (1m x 1m).
the code wrote includes diagonal driving well.
the car gets information via bluetooth.
does code make sense because not possible try out @ moment?
are there other big mistakes?
(sorry, of comments in german.)
// front und als boolsche operatoren
bool front = true;
bool = false;
// deklarieren der einzelnen pins des arduinos
int en1 = 6;
int en2 = 5; //roboduino motor shield uses pin 9
int in1 = 7;
int in2 = 4; //latest version use pin 4 instead of pin 8
// funktion motor 1 - zugriff auf motor 1
void motor1(int pwm, boolean reverse)
{
analogwrite(en1,pwm); //set pwm control, 0 stop, , 255 maximum speed
if(reverse)
{
digitalwrite(in1,high);
}
else
{
digitalwrite(in1,low);
}
}
// funktion motor 2 - zugriff auf motor 2
void motor2(int pwm, boolean reverse)
{
analogwrite(en2,pwm);
if(reverse)
{
digitalwrite(in2,high);
}
else
{
digitalwrite(in2,low);
}
}
// setup-funktion
void setup()
{
int i;
// for(i=6;i<=9;i++) //for roboduino motor shield
// pinmode(i, output); //set pin 6,7,8,9 output mode
for(i=5;i<=8;i++) //for arduino motor shield
pinmode(i, output); //set pin 4,5,6,7 output mode
serial.begin(9600);
}
// loop-funktion
void loop()
{
int x, delay_en;
char val;
if (serial.available() > 0) {
while(1)
{
val = serial.read();
if(val!=-1)
{
switch(val)
{
case 'f':// front - gerade aus
motor1(255,front); // geschwindigkeit laesst sich aendern - 255 austauschen
motor2(255,front);
delay(500);
break;
case 'b':// - nach hinten
motor1(255,back);
motor2(255,back);
delay(500);
break;
case 'r':// right - nach rechts
// erstmal nach rechts drehen
motor1(255,front);
motor2(100,back);
// dann gerade aus fahren
motor1(255,front);
motor2(255,back);
// dann position wieder einnehmen (links drehen)
motor1(100,back);
motor2(255,front);
delay(500);
break;
case 'l':// left - nach links
// erstmal nach links drehen
motor1(255,back);
motor2(100,front);
// dann gerade aus fahren
motor1(255,front);
motor2(255,front);
// dann position wieder einnehmen (rechts drehen)
motor1(100,front);
motor2(255,back);
delay(500);
break;
case 's':// stopp - kein fahren mehr
motor1(0,back);
motor2(0,back);
delay(500);
break;
case 'fr':// front&right - fahrzeug fährt schraeg nach vorne/rechts
// erstmal nach 45° rechts drehen
motor1(128,front);
motor2(50,back);
// dann gerade aus fahren - aber 1,41mal laenger!
motor1(255,front);
motor2(255,front);
// dann position wieder einnehmen (links drehen)
motor1(50,back);
motor2(128,front);
delay(500);
break;
case 'fl':// front&left - fahrzeug fährt schraeg nach vorne/links
// erstmal nach 45° links drehen
motor1(128,back);
motor2(50,front);
// dann gerade aus fahren - aber 1,41mal laenger!
motor1(255,front);
motor2(255,front);
// dann position wieder einnehmen (rechts drehen)
motor1(50,front);
motor2(128,back);
delay(500);
break;
case 'br':// back&right - fahrzeug fährt schraeg nach hinten/rechts
// erstmal nach 45° links drehen
motor1(128,back);
motor2(50,front);
// dann nach fahren - aber 1,41mal laenger!
motor1(255,back);
motor2(255,back);
// dann position wieder einnehmen (rechts drehen)
motor1(50,front);
motor2(128,back);
delay(500);
break;
case 'bl':// front&right - fahrzeug fährt schraeg nach hinten/links
// erstmal nach 45° rechts drehen
motor1(128,front);
motor2(50,back);
// dann nach hinten fahren- aber 1,41mal laenger!
motor1(255,back);
motor2(255,back);
// dann position wieder einnehmen (links drehen)
motor1(50,back);
motor2(128,front);
delay(500);
break;
}
}
}
}
}
1) fix indentation (ctrl + t)
2) not use infinite whole loop read serial data without checking if there in fact new data read
3) not use delay() function
2) not use infinite whole loop read serial data without checking if there in fact new data read
3) not use delay() function
Arduino Forum > Using Arduino > Programming Questions > Robot car in a grid system (diagonal driving)
arduino
Comments
Post a Comment