I got the arduino rover kit (just the chassis, treads, and motor) and I hooked up an h bridge. It works fine.
I'm trying to make a robot where you can program a series of four actions (left, forward, right) by pushing buttons.
Something is going wrong with the button pressing.
Here's the code:
int en1 = 5;
int en2 = 9;
int s1log1 = 3;
int s1log2 = 4;
int s2log1 = 7;
int s2log2 = 8;
int button1 = 10;
int button2 = 11;
int button3 = 12;
int light = 2;
int pot = 0;
int act1 = 0;
int act2 = 0;
int act3 = 0;
int act4 = 0;
int calib = 1000;
int time1 = 0;
int time2 = 0;
int time3 = 0;
int time4 = 0;
boolean do180 = false;
void setup() {
pinMode(en1, OUTPUT);
pinMode(en2, OUTPUT);
pinMode(s1log1, OUTPUT);
pinMode(s1log2, OUTPUT);
pinMode(s2log1, OUTPUT);
pinMode(s2log2, OUTPUT);
pinMode(light, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
//==============================================
digitalWrite(light, HIGH);
while((digitalRead(button1) == LOW && digitalRead(button2) == LOW) && digitalRead(button3) == LOW) {
}
delay(10);
if(digitalRead(button1) == HIGH) {
act1 = 1;
}
else if(digitalRead(button2) == HIGH) {
act1 = 2;
}
else if(digitalRead(button3) == HIGH) {
act1 = 3;
}
int num = analogRead(pot);
time1 = map(num, 0, 1023, 100, 1000);
//================================================
digitalWrite(light, LOW);
delay(1000);
while((digitalRead(button1) == LOW && digitalRead(button2) == LOW) && digitalRead(button3) == LOW) {
}
delay(10);
if(digitalRead(button1) == HIGH) {
act2 = 1;
}
else if(digitalRead(button2) == HIGH) {
act2 = 2;
}
else if(digitalRead(button3) == HIGH) {
act2 = 3;
}
num = analogRead(pot);
time2 = map(num, 0, 1023, 100, 1000);
digitalWrite(light, LOW);
delay(1000);
//================================================
digitalWrite(light, LOW);
delay(1000);
while((digitalRead(button1) == LOW && digitalRead(button2) == LOW) && digitalRead(button3) == LOW) {
}
delay(10);
if(digitalRead(button1) == HIGH) {
act3 = 1;
}
else if(digitalRead(button2) == HIGH) {
act3 = 2;
}
else if(digitalRead(button3) == HIGH) {
act3 = 3;
}
num = analogRead(pot);
time3 = map(num, 0, 1023, 100, 1000);
digitalWrite(light, LOW);
delay(1000);
//================================================
digitalWrite(light, LOW);
delay(1000);
while((digitalRead(button1) == LOW && digitalRead(button2) == LOW) && digitalRead(button3) == LOW) {
}
delay(10);
if(digitalRead(button1) == HIGH) {
act4 = 1;
}
else if(digitalRead(button2) == HIGH) {
act4 = 2;
}
else if(digitalRead(button3) == HIGH) {
act4 = 3;
}
num = analogRead(pot);
time2 = map(num, 0, 1023, 100, 1000);
digitalWrite(light, LOW);
delay(1000);
//===================================================
digitalWrite(light, HIGH);
delay(250);
digitalWrite(light, LOW);
delay(250);
digitalWrite(light, HIGH);
delay(250);
digitalWrite(light, LOW);
delay(250);
digitalWrite(light, HIGH);
delay(250);
digitalWrite(light, LOW);
delay(250);
digitalWrite(light, HIGH);
while(digitalRead(button1) == LOW && digitalRead(button2) == LOW) {
}
delay(10);
if(digitalRead(button1) == HIGH) {
do180 = true;
}
else if(digitalRead(button2) == HIGH) {
do180 = false;
}
digitalWrite(light, LOW);
delay(1000);
}
void loop() {
switch (act1) {
case 1:
turnleft(time1);
break;
case 2:
gofwd(time1);
break;
case 3:
turnright(time1);
break;
}
switch (act2) {
case 1:
turnleft(time2);
break;
case 2:
gofwd(time2);
break;
case 3:
turnright(time2);
break;
}
switch (act3) {
case 1:
turnleft(time3);
break;
case 2:
gofwd(time3);
break;
case 3:
turnright(time3);
break;
}
switch (act4) {
case 1:
turnleft(time4);
break;
case 2:
gofwd(time4);
break;
case 3:
turnright(time4);
break;
}
if(do180 == true) {
turn180();
}
}
void gofwd(int time) {
digitalWrite(s1log2, LOW);
digitalWrite(s1log1,HIGH);
digitalWrite(s2log2, LOW);
digitalWrite(s2log1,HIGH);
digitalWrite(en1, HIGH);
digitalWrite(en2, HIGH);
delay(time);
stop();
}
void stop() {
digitalWrite(en1, LOW);
digitalWrite(en2, LOW);
}
void turnleft(int time) {
digitalWrite(s1log1, LOW);
digitalWrite(s1log2,HIGH);
digitalWrite(s2log2, LOW);
digitalWrite(s2log1,HIGH);
digitalWrite(en1, HIGH);
digitalWrite(en2, HIGH);
delay(time);
stop();
}
void turnright(int time) {
digitalWrite(s1log2, LOW);
digitalWrite(s1log1,HIGH);
digitalWrite(s2log1, LOW);
digitalWrite(s2log2,HIGH);
digitalWrite(en1, HIGH);
digitalWrite(en2, HIGH);
delay(time);
stop();
}
void turn180() {
digitalWrite(s1log1, LOW);
digitalWrite(s1log2,HIGH);
digitalWrite(s2log2, LOW);
digitalWrite(s2log1,HIGH);
digitalWrite(en1, HIGH);
digitalWrite(en2, HIGH);
delay(calib);
}