#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
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...
[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.