Exiting an IF loop based on App Inventor Data Received


i have developed set of scales use arduino uno take readings 4 loads cells , convert them weights.  these weights bluetoothed app inventor 2 (i still developing using companion android app).  app allows me either display weight measurements or enter screen allows scale calibration (writing calibration factors eeprom).

it works although experienced coders laugh @ aspects of coding!!

the specific issue have been trying fix in relation calibration coding.  during calibration process data bluetoothed arduino ai2 , vice versa.  arduino provides ai2 various 1 digit numbers ai2 uses prompt change on screen commands.  ai2 sends arduino either 1 digit numbers indicate ok button has been pressed or multiple numbers (when tells arduino scale number calibrate , kg weight).

ai2 has exit button want both return ai2 home screen , prompt arduino exit calibration section of code.  didn't feel necessary post actual code extremely long , not serve purpose below cut down version unnecessary bits removed;

code: [select]
//initiation stuff here

void setup() {
  // put setup code here, run once:

}

void loop() {
  if (btserial.available()){
    intfunction = btserial.read();

    if (intfunction = 1){
      //***do measurement stuff***
    }

    else if (intfunction = 2){
      //***enter calibration function***
     
     // nothing until send button pressed (scale# , weight entered)
     while (btserial.available() == 0) {
     }

      // loops around until data has been taken serial input. delay required ensure bt has enough time send everything
      while (btserial.available() > 0) { 
          strreceived += string(btserial.read());
          delay(300);
         
          if ((strreceived.length()) == 9) {  // once string of known length 9 received, split string , convert integers
            strpart1 = strreceived.substring(0, 3);
            strpart2 = strreceived.substring(3, 6);
            strpart3 = strreceived.substring(6);
            intscalenumber = strpart1.toint();
            int2 = strpart2.toint();
            int3 = strpart3.toint();
            intcalibrationweight = int2 + int3;
          } 
      }

      // ***use btserial.print() send command app inventor2***

      // nothing until ai2 ok button pressed
        while (btserial.available() == 0) {
        }
     
        // clear bt buffer ai2 ok button push not used else
        delay(1000);  // delay used having issues reliability...may remove
        // loops around until data has been taken serial input. delay required ensure bt has enough time send everything
        while (btserial.available() > 0) {
          btserial.read();
          delay(300);
        }

        //***do more stuff***

        //***use btserial.print() send command app inventor2***

        // nothing until ai2 ok button pressed
        while (btserial.available() == 0) {
        }

        // clear bt buffer ai2 ok button push not used else
        delay(1000);  // delay used having issues reliability...may remove
        // loops around until data has been taken serial input. delay required ensure bt has enough time send everything
        while (btserial.available() > 0) {
          btserial.read();
          delay(300);
        }

        //***do more stuff***
  }
}


when ai2 sends "2" arduino enters calibration coding.  arduino pauses until received in btserial @ time loops around extracting characters , creating 1 long string.  arduino sends character ai2 prompt update screen prompt waits reply (an ok button have been pressed in ai2).  arduino sends character prompt , waits again reply (ok button pressed again).  arduino finishes processing.

at anytime after arduino has entered calibration code - "else if (intfunction = 2){" - user press ai2 exit button sends "6" arduino.  life of me cannot work out how arduino exit start of loop() statement.  have tried goto without success.  thought perhaps everytime arduino pauses wait data ai2, check see if data "6" real difficulty approach comes when ai2 sending long string data.  long string may include number "6" not able differentiate between receiving data , receiving exit character.

i having few days off project clear head if has suggestions appreciated.

code: [select]
if (intfunction = 1)
i hope that's not problem


Arduino Forum > Using Arduino > Programming Questions > Exiting an IF loop based on App Inventor Data Received


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'