I have a below test code to test firing some arduino code at the send of a command instead of at the press of a button. Stuff is supposed to happen when I write '1' to serial, but I end up writing and sending many 1s and nothing happens, and then suddenly one stray time it does work. What's wrong?
int rnd;
unsigned long currentmillis;
bool SetCurrentMillis = false;
bool OperationStarted = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if ((Serial.available() && Serial.read() == '1') || (OperationStarted == true)) {
rnd = random(0, 1023);
OperationStarted = true;
if (SetCurrentMillis == false) {
currentmillis = millis();
SetCurrentMillis = true;
}
if ((millis() - currentmillis) <= 200) {
Serial.print(50);
Serial.print("\t");
Serial.print(100);
Serial.print("\t");
Serial.print(150);
Serial.print("\t");
Serial.print(200);
Serial.print("\t");
Serial.print(250);
Serial.print("\t");
Serial.print(300);
Serial.print("\t");
Serial.print(300);
Serial.print("\t");
Serial.print(rnd);
Serial.print("\t");
Serial.print(55);
Serial.print("\t");
Serial.print(110);
Serial.print("\t");
Serial.print(175);
Serial.print("\t");
Serial.print(225);
Serial.print("\t");
Serial.print(268);
Serial.print("\t");
Serial.print(364);
Serial.print("\t");
Serial.print(398);
Serial.print("\t");
Serial.println(rnd + 57);
}
} else if (Serial.available() && Serial.read() == '0') {
OperationStarted = false;
SetCurrentMillis = false;
}
}
the read() "eats up" the '0' (it's removed from the incoming buffer)... so your next test
won't see it
➜ if something is available on Serial, then read it in a variable and then decide what to do
organise your code as a state machine, it will make your life easier, your operationStarted bool was a good idea to decide what state you are in
click to see the code
unsigned long lastPrinting;
bool operationStarted = false;
void setup() {
Serial.begin(115200);
Serial.println(F("type 1 to start printing every 200ms and 0 to stop"));
}
void loop() {
int r = Serial.read(); // will be -1 if nothing is available to read
if (!operationStarted && r == '1') {
operationStarted = true;
lastPrinting = millis() - 200;
Serial.println(F("STARTING"));
}
else if (operationStarted && r == '0') {
Serial.println(F("STOPING"));
operationStarted = false;
}
if (operationStarted) {
if ((millis() - lastPrinting) >= 200) {
Serial.println("Tick");
lastPrinting = millis();
}
}
}
you only print when you get the '1' and more than 200ms had passed. Although it matches his original code (non working) behavior, I'am not sure it was OP's intent - would need clarification
My understanding of OP's expectations was that sending 1 would get the arduino to log stuff out every 200ms and sending 0 would stop the logging. I might have been wrong.
what I want to do is that when I press 1, for 200mS the code should print stuff. But if before the 200mS is passed I press 0, then the code should stop printing stuff.