Skip to main content
1 of 2
user avatar
user avatar

Arduino: how can I stop while() when bluetooth send a data?

I have learned Arduino for a month, and I want to bulid a car with 2 modes. Remote and Auto, but Auto mode need to keep while(), so it can receives data from sensors. When I choose Auto mode via bluetooth, it keep loop and it won't stop when I choose the Remote mode. Here's the part code:

char mode;

void loop() {

  while(serial1.available()) {
    char control = serial.read();
    if(control == 'r') mode = 'r';     // r stand for Remote Mode
    if(control == 'a') mode = 'a';     // a stand for Auto Mode

    if(mode == 'r') remoteMode(control);  

    while(mode = 'a') {                 
      int IR = analogRead(A10);

      float U1cmMsec,U2cmMsec;
      long U1microsec = u1.timing();
      long U2microsec = u2.timing();
      U1cmMsec = u1.convert(U1microsec, Ultrasonic::CM);
      U2cmMsec = u2.convert(U2microsec, Ultrasonic::CM);

      robot(U1cmMsec, U2cmMsec,IR);
      
      /*
      Here is the code to stop while() that I don't know how to program.
      I have tried:
      
      if(serial1.read() != 'a') break; 
      
      But it will stop while(), because it will waits for bluetooth send data to arduino board. 
      And if I take this line off, it will keeps while() and nothing can stop it.
      so, what I suppose to do?
      */
}

} }

徐理芳