Hi guys,
yes noob here.
I'm currently stuck over something that's generally pretty simple- integrating code for a push button in my two-motor + 1 ultrasonic sensor setup to navigate an "S" shaped maze.
Yes, it's not good code! However, I'm supposed to only work with 1 ultrasonic sensor mounted at the head, and two motors & wheels (with a fixed castor in back) navigating an "S" maze. It currently works without a push button. But I'm supposed to have one to execute the rest of the code.
My question is, what can I do to make my code for the maze start when the button is pushed down and then released? I understand that the 'if' statement is already used for the beginning of the bulk of the code and hasn't worked for the button. I've tried using a while loop instead for the button, to encompass the program. I've used INPUT_PULLUP to change the status of the button to read when released to no avail... please halp me. What can I do to get this button to execute the rest of this code?
My craptastic code:
const int motor_lA = 11; //AIN1
const int motor_lB = 8; //BIN1
const int motor_enableA = 10; //PWMA
const int motor_rA = 12; //AIN2
const int motor_rB = 7; //BIN2
const int motor_enableB = 9; //PWMB
const int trigger_front = A0;
const int echo_front = A1;
const int button = 13;
int turnLeft = 0;
int turnRight = 0;
void setup()
{
pinMode(motor_lA,OUTPUT); //left motors forward
pinMode(motor_lB,OUTPUT); //left motors reverse
pinMode(motor_enableA, OUTPUT);
pinMode(motor_rA,OUTPUT); //right motors forward
pinMode(motor_rB,OUTPUT); //rignt motors reverse
pinMode(motor_enableB, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode(trigger_front,OUTPUT);
pinMode(echo_front,INPUT);
}
void loop()
{
Serial.begin(9600);
long duration_front, distance_front;
digitalWrite(trigger_front, LOW);
delayMicroseconds(2);
digitalWrite(trigger_front, HIGH);
delayMicroseconds(10);
digitalWrite(trigger_front, LOW);
duration_front = pulseIn(echo_front, HIGH);
distance_front= duration_front*0.034/2;
//Serial.print(distance_front);
//Serial.println(" = distance_front");
//delay(200);
while (button == LOW){
if(distance_front > 10)
{
digitalWrite(motor_lA, 0);//forward
digitalWrite(motor_lB, 0);
digitalWrite(motor_rA, 1);
digitalWrite(motor_rB, 1);
analogWrite(motor_enableA, 50);
analogWrite(motor_enableB, 50);
delay(10);
}
else if ((distance_front <= 10) && (turnLeft < 2))
{
turnLeft = turnLeft + 1;
//Serial.print(turnLeft);
//Serial.println(" left turns");
digitalWrite(motor_lA, 0);//stop
digitalWrite(motor_lB, 0);
digitalWrite(motor_rA, 0);
digitalWrite(motor_rB, 0);
analogWrite(motor_enableA, 0);
analogWrite(motor_enableB, 0);
delay(300);
digitalWrite(motor_lA, 0);//turn LEFT
digitalWrite(motor_lB, 1);
digitalWrite(motor_rA, 1);
digitalWrite(motor_rB, 0);
analogWrite(motor_enableA, 180);
analogWrite(motor_enableB, 180);
delay(500);
digitalWrite(motor_lA, 0);//Stop
digitalWrite(motor_lB, 0);
digitalWrite(motor_rA, 0);
digitalWrite(motor_rB, 0);
analogWrite(motor_enableA, 0);
analogWrite(motor_enableB, 0);
delay(300);
}
else if ((distance_front <= 10) && (turnRight < 2))
{
digitalWrite(motor_lA, 0);//stop
digitalWrite(motor_lB, 0);
digitalWrite(motor_rA, 0);
digitalWrite(motor_rB, 0);
analogWrite(motor_enableA, 0);
analogWrite(motor_enableB, 0);
delay(300);
turnRight = turnRight + 1;
digitalWrite(motor_lA, 1);//turn RIGHT
digitalWrite(motor_lB, 0);
digitalWrite(motor_rA, 0);
digitalWrite(motor_rB, 1);
analogWrite(motor_enableA, 180);
analogWrite(motor_enableB, 180);
delay(550);
digitalWrite(motor_lA, 0);//Stop
digitalWrite(motor_lB, 0);
digitalWrite(motor_rA, 0);
digitalWrite(motor_rB, 0);
analogWrite(motor_enableA, 0);
analogWrite(motor_enableB, 0);
delay(300);
}
}
}