Code:
/*
PWM Pins
Nano: 3; 5; 6; 9; 10; 11
Uno: 3; 5; 6; 9; 10; 11
Due: 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13
Mega 2560: 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13
*/
int IN1 = 9;
int IN2 = 8;
int IN3 = 7;
int IN4 = 6;
int speedl1;
int speedl2;
int speedr1;
int speedr2;
int loopcount;
void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop()
{
// Vorwärts
for (int loopcount=0; loopcount <=255; loopcount++){
speedl1 = loopcount;
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=255; loopcount >=0; loopcount--){
speedl1 = loopcount;
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Rückwärts
for (int loopcount=0; loopcount <=255; loopcount++){
speedl1 = 0;
speedl2 = loopcount;
speedr1 = 0;
speedr2 = loopcount;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=255; loopcount >=0; loopcount--){
speedl1 = 0;
speedl2 = loopcount;
speedr1 = 0;
speedr2 = loopcount;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Links Drehen über Mittelpunkt
for (int loopcount=0; loopcount <=255; loopcount++){
speedl1 = loopcount;
speedl2 = 0;
speedr1 = (255 - loopcount);
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=255; loopcount >=0; loopcount--){
speedl1 = loopcount;
speedl2 = 0;
speedr1 = (255 - loopcount);
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Rechts Drehen über Mittelpunkt
for (int loopcount=0; loopcount <=255; loopcount++){
speedl1 = (255 - loopcount);
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=255; loopcount >=0; loopcount--){
speedl1 = (255 - loopcount);
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Links Drehen über linkes Rad
for (int loopcount=0; loopcount <=255; loopcount++){
speedl1 = loopcount;
speedl2 = 0;
speedr1 = 0;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=255; loopcount >=0; loopcount--){
speedl1 = loopcount;
speedl2 = 0;
speedr1 = 0;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Rechts Drehen über rechtes Rad
for (int loopcount=0; loopcount <=255; loopcount++){
speedl1 = 0;
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=255; loopcount >=0; loopcount--){
speedl1 = 0;
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Vorwärts Kurve Links Radius = Roboterbreite
for (int loopcount=0; loopcount <=127; loopcount++){
speedl1 = (loopcount * 2);
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=127; loopcount >=0; loopcount--){
speedl1 = (loopcount * 2);
speedl2 = 0;
speedr1 = loopcount;
speedr2 = 0;
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
// Rückwärts Kurve Rechts Radius = Roboterbreite
for (int loopcount=0; loopcount <=127; loopcount++){
speedl1 = 0;
speedl2 = loopcount;
speedr1 = 0;
speedr2 = (loopcount * 2);
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
for (int loopcount=127; loopcount >=0; loopcount--){
speedl1 = 0;
speedl2 = loopcount;
speedr1 = 0;
speedr2 = (loopcount * 2);
PWMDrive(speedl1, speedl2, speedr1, speedr2);
delay(10);
}
}
void PWMDrive(int speedl1,int speedl2,int speedr1,int speedr2){
analogWrite(IN1, speedl1);
analogWrite(IN2, speedl2);
analogWrite(IN3, speedr1);
analogWrite(IN4, speedr2);
}
Lesezeichen