How to run a statement once in void loop

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x20,16,2);
SoftwareSerial BTserial(3, 2); // RX | TX  pinMode(RVPA,OUTPUT)
char s [80];
int idx = 0;
char junk;
int Hasrun = 0;
String inputString="";
String RECV;
void loop() 
{ 
  // serial recieve code
do {
 if (BTserial.available() > 0){ 
        char  c = BTserial.read ();
        s [idx++] = c;
    if ('\n' == c)  { // Remember in the code you have to append newline
       s [idx-1] = '\0';
       idx = 0;
       char *str;
       char *p = s;
      for (p = s; (str = strtok (p, ",")); p = NULL) {
         inputString += ",";
         inputString += str;}
   }
    RECV = inputString;
    Serial.println(RECV);   // Print the data stored in RECV
    delay(prowait);}     // delay time
    while (BTserial.available() > 0) // clear the serial buffer 
    { junk = BTserial.read() ; }
    Hasrun = 1; 
  }   
  while (Hasrun < 1); 
    // Main looper
}

i am trying to make this loop to receive serial data once and then print it on the serial monitor, then never re-run, but i tried, and it isn't working, can i get some help, the while loop checks if the variable Hasrun is less than one, and if it is, it executes the statement, then if the statement is executed, Hasrun becomes 1, and then it can't run, but i don't know why it isn't working

The most logical explanation is that BTserial.available() never returns >0 in your particular case. Have you verified this?

I hate to be petty here, but you have no

void setup()

I cannot test the code.

Make hasRun global variable or leave it local, but static.

Move int hasRun outside the loop function. Global.

or

static  int hasRun = 0;

where it was, local, but make it static.

Sry, Hasrun.

HTH

a7

not really, though, BTserial, is a seperate serial port i created using software serial library

i made Hasrun to be such that it changes when the loop is run, why should i change it to a static int

i haven't verified it

I'd do that first; make sure your soft serial / BT interface is actually working before adding more code to it.
Btw you could make Hasrun a boolean instead of an int. Saves a byte of memory. More importantly, it makes your code more logical which can be a bonus if you revisit it later on for updates/modifications.

Yes, you can not use a do-while because it will always run once no matter what the conditional statement evaluates to. Just use an 'if' with a boolean 'hasrun' initially set to 'false' then set to 'true' inside the 'if'.

Or put all the code in setup() where it will also run just once...

Never mind, just a bit of the old vertical dyslexia. :wink:

a7

thanks, just did this, it worked

void loop() 
{ 
  // serial recieve code
  if (Hasrun <1){
 if (BTserial.available ()) {
        char  c = BTserial.read ();
        s [idx++] = c;

        if ('\n' == c)  {
            s [idx-1] = '\0';
            idx = 0;

            char *str;
            char *p = s;

            for (p = s; (str = strtok (p, ",")); p = NULL) {
                Serial.print   ("Location ");
                Serial.println (str);
                inputString += ",";
                inputString += str;
            }
            RECV = inputString;
            Serial.print(RECV);
            delay(prowait);
            Hasrun++;
            Serial.println(Hasrun);
        }
    }
  }
  else{
    //main loop
    }
}

i did it in such a way that it runs the code first then does the main loop as the else statement

i actually prefer the non - boolean form, so i can run any desired number of times

Yes, that's the advantage of not using a boolean.

[quote="anuforopeter345, post:12, topic:873809, full:true"]
You should at least rename it, then. For example, 'timesRun'. 'Has run' is a true or false proposition.

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