Closely related to To know the state of USB (Serial) connection (connected or not connected) To know the state of USB (Serial) connection (connected or not connected)
I would not be using Serial.readString()
personally. How do you know where the string starts and ends? Just check for three "A" in a row.
Like this:
const unsigned long MESSAGE_TIMEOUT = 10UL * 1000; // 10 seconds
const byte LED = 7;
void setup ()
{
Serial.begin (115200);
while (!Serial) ; // wait for Serial to become active
Serial.println ("Starting");
pinMode (LED, OUTPUT);
} // end of setup
unsigned long lastMessage;
unsigned int countOfA;
void processInput ()
{
while (Serial.available ())
{
char c = Serial.read ();
if (c == 'A')
countOfA++;
else
countOfA = 0;
// do something with the data
} // end of while loop
} // end of processInput
void loop ()
{
if (Serial.available ())
{
lastMessage = millis (); // remember when we last got input
processInput (); // now handle the input
}
if (millis () - lastMessage >= MESSAGE_TIMEOUT)
{
countOfA = 0;
}
// warn if no keepalive received
if (countOfA >= 3)
digitalWrite(LED, LOW);
else
digitalWrite(LED, HIGH);
} // end of loop
Note that this will fail (ie. it will light the LED) if it gets something other than "A" which would include newlines, etc.
What it does it count the number of times it gets the letter "A". If it gets something else it resets the count. If 10 seconds elapse without receiving something, the count resets.
Closely related to To know the state of USB (Serial) connection (connected or not connected)
I would not be using Serial.readString()
personally. How do you know where the string starts and ends? Just check for three "A" in a row.
Like this:
const unsigned long MESSAGE_TIMEOUT = 10UL * 1000; // 10 seconds
const byte LED = 7;
void setup ()
{
Serial.begin (115200);
while (!Serial) ; // wait for Serial to become active
Serial.println ("Starting");
pinMode (LED, OUTPUT);
} // end of setup
unsigned long lastMessage;
unsigned int countOfA;
void processInput ()
{
while (Serial.available ())
{
char c = Serial.read ();
if (c == 'A')
countOfA++;
else
countOfA = 0;
// do something with the data
} // end of while loop
} // end of processInput
void loop ()
{
if (Serial.available ())
{
lastMessage = millis (); // remember when we last got input
processInput (); // now handle the input
}
if (millis () - lastMessage >= MESSAGE_TIMEOUT)
{
countOfA = 0;
}
// warn if no keepalive received
if (countOfA >= 3)
digitalWrite(LED, LOW);
else
digitalWrite(LED, HIGH);
} // end of loop
Note that this will fail (ie. it will light the LED) if it gets something other than "A" which would include newlines, etc.
What it does it count the number of times it gets the letter "A". If it gets something else it resets the count. If 10 seconds elapse without receiving something, the count resets.
Closely related to To know the state of USB (Serial) connection (connected or not connected)
I would not be using Serial.readString()
personally. How do you know where the string starts and ends? Just check for three "A" in a row.
Like this:
const unsigned long MESSAGE_TIMEOUT = 10UL * 1000; // 10 seconds
const byte LED = 7;
void setup ()
{
Serial.begin (115200);
while (!Serial) ; // wait for Serial to become active
Serial.println ("Starting");
pinMode (LED, OUTPUT);
} // end of setup
unsigned long lastMessage;
unsigned int countOfA;
void processInput ()
{
while (Serial.available ())
{
char c = Serial.read ();
if (c == 'A')
countOfA++;
else
countOfA = 0;
// do something with the data
} // end of while loop
} // end of processInput
void loop ()
{
if (Serial.available ())
{
lastMessage = millis (); // remember when we last got input
processInput (); // now handle the input
}
if (millis () - lastMessage >= MESSAGE_TIMEOUT)
{
countOfA = 0;
}
// warn if no keepalive received
if (countOfA >= 3)
digitalWrite(LED, LOW);
else
digitalWrite(LED, HIGH);
} // end of loop
Note that this will fail (ie. it will light the LED) if it gets something other than "A" which would include newlines, etc.
What it does it count the number of times it gets the letter "A". If it gets something else it resets the count. If 10 seconds elapse without receiving something, the count resets.
Closely related to To know the state of USB (Serial) connection (connected or not connected)
I would not be using Serial.readString()
personally. How do you know where the string starts and ends? Just check for three "A" in a row.
Like this:
const unsigned long MESSAGE_TIMEOUT = 10UL * 1000; // 10 seconds
const byte LED = 7;
void setup ()
{
Serial.begin (115200);
while (!Serial) ; // wait for Serial to become active
Serial.println ("Starting");
pinMode (LED, OUTPUT);
} // end of setup
unsigned long lastMessage;
unsigned int countOfA;
void processInput ()
{
while (Serial.available ())
{
char c = Serial.read ();
if (c == 'A')
countOfA++;
else
countOfA = 0;
// do something with the data
} // end of while loop
} // end of processInput
void loop ()
{
if (Serial.available ())
{
lastMessage = millis (); // remember when we last got input
processInput (); // now handle the input
}
if (millis () - lastMessage >= MESSAGE_TIMEOUT)
{
countOfA = 0;
}
// warn if no keepalive received
if (countOfA >= 3)
digitalWrite(LED, LOW);
else
digitalWrite(LED, HIGH);
} // end of loop
Note that this will fail (ie. it will light the LED) if it gets something other than "A" which would include newlines, etc.
What it does it count the number of times it gets the letter "A". If it gets something else it resets the count. If 10 seconds elapse without receiving something, the count resets.