Ultrasonic sensor, ir remote, and motors interacting

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

Replace the partial code sample with the complete program.

This probably causes confusion while you program. "Not too close" to a human can mean "do not proceed (you are too close)" or "You are not too close, please, proceed" What about replacing that with something more descriptive like "object_distance?"

You may want to think about it in a completely different way...You could set it up as a state machine where the robot is always operating in one of the several states {forward, backward, left, right, and stop} and, while in those states, would periodically check if there's a reason it should transition to a different state. Then you could use the trick in Demonstration code for several things at the same time to read the sensor and check the ir remote.

Ok, I just spent the last hour or so reading all about state machines and realize this is the obvious way to go. I had a bunch of breaks in my code which was preventing things from happening. Im going to play around with this tomorrow.
The other thing I realized is that I need to stop using mixly and actually start coding. Mixly was actually confusing me because it’s easy to miss that things in a loop run top to bottom, over and over. With the visual aspect of mixly I had things spread all over the work area and no matter how I arraigned them it didn’t make any difference to the loop.
Now I have to try and remember all the programming classes I took over 20 years ago and haven’t used since…

1 Like

I think you can have Mixley produce cpp text.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.