I know I use weird topic titles.
What I'm trying to do is have my robot move forward until either the ultrasonic sensor triggers as being too close to something or the ir remote tells it to stop. I'm not having much success. I can get the robot to move forward and stop with the remote and I can get it to move forward and stop when the sensor triggers but I can't get both at the same time. I"m betting it's a coding issue of some sort but since I'm learning as I'm doing I can't figure it out.
I'm thinking it has something to do with my while loop but I can't figure it out or if I'm even looking at the right thing. I don't want to stick all my code here so I'm just putting in the part that has the loop that I think is the issue. Like I said, Im new at this so my code could be over complicated. I've stuck a few functions in there to print text on the monitor so I can see where things are happening. I'm also using mixly visual coding so who knows if this is how it's supposed to work.
Ive set it up so there are functions for forward, backward, left, right, and stop. each one of these can be called by the ir remote. The function in the code is called when I hit "forward" on the remote. what I want the loop to do is use the ultrasonic sensor to get the distance then start moving forward. This is the part I'm having problems with. If the sensor gets tripped the robot stops but it's not reading the ir remote to stop also. I'm thinking maybe there is not enough time in the loop to read the remote. Like I said, I have no real clue what I'm doing.
The code is below is for the forward movement loop.
void UltrasonicForward() {
// Set speed
Car_Speed = 175;
// creates loop variable
// creates distance variable
// sets loop variable to true
Not_Too_Close = true;
Serial.println(Not_Too_Close);
// get IR input
if (irrecv_A1.decode(&results_A1)) {
ir_rec=results_A1.value;
String type="UNKNOWN";
String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
if(results_A1.decode_type>=1&&results_A1.decode_type<=13){
type=typelist[results_A1.decode_type];
}
Serial.print("IR TYPE:"+type+" ");
// sets variable as output from sensor
Forward_Distance = checkdistance_12_13();
Serial.print(Forward_Distance);
Serial.println("cm");
while (Not_Too_Close) {
// compare distance to min distance
if (Forward_Distance <= D_min) {
Serial.println("Too close!");
// sets loop variable to false
Not_Too_Close = false;
// backup
Move_Backward();
// stop
STOP();
// center button on remote
} else if (0xFF38C7 == ir_rec) {
// sets loop variable to false
Not_Too_Close = false;
// stop
STOP();
} else {
// move forward
Move_Forward();
}
}
irrecv_A1.resume();
} else {
}
}
I'm pretty sure I'm calling the ir input too much, I call it in every function that uses the IR. I'm not sure if I can call it once globally and be picked up anywhere in the program. I'm also sure this is way overcomplicated for what I'm trying to do