When Roamer's bumper is depressed, there are five possible "backup and turn" scenarios--the activation choice is random.
After 30 seconds of forward movement (stuck spinning wheels without depressing the bumper or long distance), she backs up and tries another direction.
unsigned long timestart;
unsigned long timeend;
unsigned long timetotal;
int a=95;//speed for left? motor, increased from 80
int b=85;
int c=650;//value for photoresistor day/night higher = less light to keep going
int d=820;//upper voltage value
int t1=1;
int volt=A1;//bumper input pin
int val=0;//initial bumper value
int val1=100;//initial value for photocell
int bumper=1;//setup bumper not pressed
long randNumber;
int x=1;//current read value to be determined in setup
int x1=0;//first of multiple reads
int x2=0;
int x3=0;
int x4=0;
int x5=0;
int y=0;//counts for overcurrent rather than spike
int z=0;//storage area for temporary current reads
const int pwmA=3;
const int pwmB=11;
const int brakeA=9;
const int brakeB=8;
const int dirA=12;
const int dirB=13;
void setup() {
pinMode (dirA,OUTPUT);
pinMode (brakeA,OUTPUT);
pinMode (dirB,OUTPUT);
pinMode (brakeB,OUTPUT);
pinMode (A1,INPUT);
pinMode (2,INPUT);
digitalWrite (2,HIGH);
randomSeed (analogRead(0));
digitalWrite(brakeA,LOW);
digitalWrite(brakeB,LOW);
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,0);//changed to reflect no overcurrent mode
analogWrite(pwmB,0);
//delay(2000);//get motors started
//x1=analogRead(A0);//set normal motor value
//delay(10);
//x2=analogRead(A0);
//delay(10);
//x3=analogRead(A0);
//delay(10);
//x4=analogRead(A0);
//delay(10);
//x5=analogRead(A0);
//x=((x1+x2+x3+x4+x5)/5);
x=500;//arbitray number
timestart=millis();
}
void loop() {
bumper=digitalRead(2);
if (bumper==0) {//bumper depressed
randNumber=random(1,5);//changed august 12 from 1,4
if(randNumber==1) {
analogWrite(pwmB,0);//check for power station
analogWrite(pwmA,0);
delay(500);//stop to read
val=analogRead(A2);
if (val>100) {
val1=analogRead(A3);
while ((val<d && val>100) || (val1>c)) {//was 815 instead of 600
val1=analogRead(A3);
val=analogRead(A2);
delay(100);
}}
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,120);//was 150
analogWrite(pwmB,70);//reverse power
delay(500);
analogWrite(pwmB,0);
delay(750);
analogWrite(pwmA,0);//stop
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
if (randNumber==2) {
analogWrite(pwmB,0);//check for power station
analogWrite(pwmA,0);
delay(500);//stop to read
val=analogRead(A2);
if (val>100) {
val1=analogRead(A3);
while ((val<d && val>100) ||(val1>c)) {
val1=analogRead(A3);
val=analogRead(A2);
delay(100);
}}
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,70);//was 150
analogWrite(pwmB,120);//reverse power
delay(600);
analogWrite(pwmA,0);
delay(1000);
analogWrite(pwmB,0);//stop
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
if (randNumber==3) {
analogWrite(pwmB,0);//check for power station
analogWrite(pwmA,0);
delay(500);//stop to read
val=analogRead(A2);
if (val>100) {
val1=analogRead(A3);
while ((val<d && val>100) || (val1>c)) {
val1=analogRead(A3);
val=analogRead(A2);
delay(100);
}}
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,a);//was 150
analogWrite(pwmB,b);//reverse power
delay(600);
digitalWrite(dirB,HIGH);
analogWrite(pwmB,150);
delay(800);
analogWrite(pwmA,0);//stop
analogWrite(pwmB,0);
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
if(randNumber==4) {
analogWrite(pwmB,0);//check for power station
analogWrite(pwmA,0);
delay(500);//stop to read
val=analogRead(A2);
if (val>100) {
val1=analogRead(A3);
while ((val<d && val>100) || (val1>c)) {//was 815 instead of 600
val1=analogRead(A3);
val=analogRead(A2);
delay(100);
}}
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,a);//was 150
analogWrite(pwmB,b);//reverse power
delay(500);
analogWrite(pwmB,0);
delay(1000);
analogWrite(pwmA,0);//stop
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
if(randNumber==5) {
analogWrite(pwmB,0);//check for power station
analogWrite(pwmA,0);
delay(500);//stop to read
val=analogRead(A2);
if (val>100) {
val1=analogRead(A3);
while ((val<d && val>100) || (val1>c)) {//was 815 instead of 600
val1=analogRead(A3);
val=analogRead(A2);
delay(100);
}}
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,a);//was 150
analogWrite(pwmB,b);//reverse power
delay(400);
analogWrite(pwmB,0);
delay(800);
analogWrite(pwmA,0);//stop
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
}
//main body of action
if (t1=0) {
timestart = millis();
t1=1;
}
if (t1=1) {
timeend = millis();
timetotal= (timeend-timestart);
}
if (timetotal>30000) {
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,a);//was 150
analogWrite(pwmB,b);//reverse power
delay(1000);
digitalWrite(dirB,HIGH);
analogWrite(pwmB,150);
delay(1000);
analogWrite(pwmA,0);//stop
analogWrite(pwmB,0);
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
digitalWrite(dirA,HIGH);
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
z=analogRead(A0);//read current
if (z>(x+210)) {//turn around and restart
digitalWrite(dirA,LOW);
digitalWrite(dirB,LOW);//reverse
analogWrite(pwmA,150);
analogWrite(pwmB,150);//reverse power
delay(1000);
digitalWrite(dirB,HIGH);
analogWrite(pwmB,150);
delay(600);
analogWrite(pwmA,0);//stop
analogWrite(pwmB,0);
digitalWrite(dirA,HIGH);//go forward one second
digitalWrite(dirB,HIGH);
analogWrite(pwmA,a);
analogWrite(pwmB,b);
delay(1000);
t1=0;
timestart=millis();
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.