Receiving Bluetooth data is bugging

Continuing the discussion from Problem of receiving bytes with Bluetooth:

Hello,
I had a problem of receiving data that was solved in the post above but that was when trying it alone in the loop function.

void loop() {
  
if(BT.available() > 0) {  
  bt_tag = BT.readStringUtil("|");  
  if(bt_tag == "m") {
    bt_data = BT.read();
    Serial.println("mode" + bt_data);
    mode = bt_data;
  } else if(bt_tag == "x" || bt_tag == "y") {
    if(mode == 0) {
      if(bt_tag == "x") {
        xAxis = BT.read();
      } else if(bt_tag =="y") {
        yAxis = BT.read();
      }
      Serial.print("Coordinates: ");
      Serial.print(xAxis);
      Serial.print(",");
      Serial.println(yAxis);
   }
}

After adding code to the loop function, the x y coordinates data (given by imageSprite when dragged and when touchup) started bugging again and giving zeroes, sending data by buttons works fine.
azeaze

That is probably because of the delay time caused by execution of other instructions in the loop and because the data sent by imageSprite when drag is continuous. so I am stuck the only ideas that I have are:
*Adding some kind of delay to make sure the arduino receives data correctly (tried in diffrent parts of the code but failed)
*Using some kind of software Interrupt
Is my diagnosis of the problem correct?
Are the ideas that I have good and should I continue trying them. if not what is better?

Resources are available in the previous post. The main one are:
the sending data blocks


capture2

the code after adding functions to the loop

void loop() {
  if(BT.available() > 0) {  
    String bt_tag = BT.readStringUntil('|');  
    bt_data = BT.readStringUntil('|').toInt();
    if(bt_tag == "m") {
      mode = bt_data;
      Serial.println("mode" + String(bt_data));
    } else 
       if(bt_tag == "x" || bt_tag == "y") {  // MANUAL MODE
        if(bt_tag == "x") {
          xAxis = bt_data;
        } 
        if(bt_tag =="y") {
          yAxis = bt_data;
        }
        Serial.print("Coordinates: ");
        Serial.print(xAxis);
        Serial.print(",");
        Serial.println(yAxis);
        delay(10);
        }  else
      if(bt_tag == "b") {
      button = bt_data;
      Serial.println("button" + String(bt_data)); }
    
      }
      switch (button) {
         case 1:
          lighton();  break;
         case 2:
          lightoff();break;
         case 3:
          rblinkeron();break;
         case 4:
          rblinkeroff();break;
         case 5:
          lblinkeron();break;         
         case 6:
          lblinkeroff();break;
         case 7:
          hornon();break;
         case 8:
          hornoff();break;
         default:
         break;
      }
                //************************* LINE FOLLOWER *******************************
  if ( mode == 2 || mode == 6 || mode == 4){Stop();}
  else if(mode == 1){
   if (xAxis > 125 && xAxis <155 && yAxis > 130 && yAxis <150){Stop();}
     else if (yAxis > 130 && yAxis <150){    
        if (xAxis < 130){turnLeft();
        motorSpeedA = map(xAxis, 130, 60, 0, 255);  
        motorSpeedB = map(xAxis, 130, 60, 0, 255);    
        }

        if (xAxis > 150) {turnRight();
        motorSpeedA = map(xAxis, 150, 220, 0, 255);
        motorSpeedB = map(xAxis, 150, 220, 0, 255); 
        }
   }
    else if (xAxis > 125 && xAxis <155){   

        if (yAxis < 130){forward();
        motorSpeedA = map(yAxis, 130, 60, 0, 255);
        motorSpeedB = map(yAxis, 130, 60, 0, 255); 
        }
        if (yAxis > 150){backward();
        motorSpeedA = map(yAxis, 150, 220, 0, 255);
        motorSpeedB = map(yAxis, 150, 220, 0, 255);}   
    }
    else{
        if (yAxis < 130){forward();}
        if (yAxis > 150){backward();}
        if (xAxis < 125){
        motorSpeedA = map(xAxis, 130, 60, 255, 70);
        motorSpeedB = 255; 
        }
        if (xAxis > 155){
        motorSpeedA = 255;
        motorSpeedB = map(xAxis, 150, 220, 255, 70); 
        }
    } 
    analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
    analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
      }

  else if(mode == 3){       
    if((digitalRead(RS) == 0)&&(digitalRead(LS) == 0)){forward();}  
    if((digitalRead(RS) == 1)&&(digitalRead(LS) == 0)){turnRight();}
    if((digitalRead(RS) == 0)&&(digitalRead(LS) == 1)){turnLeft();} 
    if((digitalRead(RS) == 1)&&(digitalRead(LS) == 1)){Stop();}   
    } 

            //********************** OBSTACLE AVOIDING ****************************
  
  else if(mode == 5){    
    distance_F = Ultrasonic_read();
    if (distance_F > set){forward();}
    else{Check_side();}
    }

  delay(10);
  } 

Remove any delays from the loop and then try.

It works after removing that one 10ms delay. sorry for wasting your time.

I think you can remove both. The best program is one that has no delays. If you want to do something periodically, you should use millis.

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