I am having trouble getting my code below to only allow one character to be taken and read in to check the value. If multiple characters are entered like 'gh' it needs to be registered as invalid and any approach I've taken has not worked.
Here is my code:
void setup() {
Serial.begin(9600); // Initialize Serial Connection
pinMode(13, OUTPUT); // Making Pin 13 on board as OUTPUT
pinMode(12, OUTPUT);
Serial.println("Enter 'g' to start or 's' to stop LED's");
}
void loop(){
int user_input;
bool blinking; // Control variable to set blinking
if(Serial.available() > 0) // Continue to read while inputs valid
{
user_input = Serial.read();
if (user_input == 'g'){
blinking = true;
user_input = Serial.read();
}
else if (user_input == 's'){
blinking = false;
user_input = Serial.read();
}
else{
Serial.println("Invalid input..... Try again");
user_input = Serial.read();
}
if(blinking == true) // Handles case when 'g' was entered
{
Serial.println("You turned the LED's ON! Watch them blink");
do{
digitalWrite(13, HIGH); // Turn ON onboard LED
digitalWrite(12, LOW); // Keep external LED off to cycle
delay(2000); // Wait 2 sec
digitalWrite(13, LOW); // Turn OFF onboard LED
digitalWrite(12, HIGH); // Turn external LED on
delay(1000);
user_input = Serial.read(); // Continue to read input to cycle blinks
}while( user_input != 's');
}
else
{
digitalWrite(13, LOW); // 's' entered blinking stopped
digitalWrite(12, LOW);
user_input = Serial.read(); // Check for the input
}}}}
If multiple characters are entered like 'gh' it needs to be registered as invalid
So, establish that there is at least one character to be read, read it and act on it if appropriate. Now test whether any more data is available. If so print your "Invalid" message and read from Serial until there is nothing available and start the process over again.
At the start of loop() you could check whether more than one character is available but as serial data arrives very slowly this is unlikely to catch multiple character entry
One thing to look out for is what you have the line ending set to in the Serial monitor. Anything except No line ending will cause problems as more than one character will always be sent
Well i have tried setting user_input to both int and char, was just trying different things. and I only had the serial.read again in the else loop as a failsafe but it technically does not need to be there. But Like you mentioned, I am struggling creating the loop to check if only one character was input, everything I have done messes up the rest of my code
calvinc5:
Well i have tried setting user_input to both int and char, was just trying different things. and I only had the serial.read again in the else loop as a failsafe but it technically does not need to be there. But Like you mentioned, I am struggling creating the loop to check if only one character was input, everything I have done messes up the rest of my code
You only read one character, and make a decision. You MUST have some means, on the sending end, to include a character that means 'That's all, folks!". You need to then read all the data until that character arrives, and then determine how many characters you read and stored.
The Serial Monitor makes this easy, by giving you the option to send a carriage return or carriage return and line feed, after every that you want sent has been sent.