blinking program need to catch invalid inputs

I am having trouble getting an invalid argument to catch. When a letter other than g or s is entered i need it to kick back invalid. I am not able to get it to catch

proj1_487.ino (1.19 KB)

Why don't you simply test the character received against 'g' or 's', and if it isn't either of those, print an error message?

Hmmm, I think you should read this:
Serial Input Basics
Then start over. :slight_smile:

I tried testing against g or s and it creates an endless print of the invalid

calvinc5:
I tried testing against g or s and it creates an endless print of the invalid

OK. You realise that we can’t see your code that does this? (Or maybe not. Which seems more likely)

I only have the code that I have done at the moment, did not save the comparison when it did not work. I also tried it in a switch statement but still not working

The code the OP could have just posted himself if he would bother to read the forum instructions:

/*  Project 1 Blink Program
 *  Spring 2018 Semster
 *  Febuary 8th
 *  Calvin Clark
 *  11398870
 */

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() {
char input_str;                   // Initialize input value to compare
input_str = Serial.read();
if(input_str == 'g')
{
  do{
    digitalWrite(13, HIGH);       // turn ON LED
    digitalWrite(12, LOW);
    delay(2000);                  // wait 2 sec
    digitalWrite(13, LOW);        // turn OFF LED
    digitalWrite(12, HIGH); 
    delay(1000);                  // wait 1 sec

    input_str = Serial.read();    //check value to repeat blinking
  } while(input_str != 's');
}
else if(input_str == 's')
{
  input_str = Serial.read();
  do{
    digitalWrite(13, LOW);        // 's' entered blinking stopped
    digitalWrite(12, LOW);

    input_str = Serial.read();
  } while(input_str != 'g');
}
else if (Serial.available() > 0)
{
  do{
    Serial.println("You have entered an Invalid input.... Try again");
  }while(input_str != 'g' || input_str != 's');
}
}

Im sorry, I thought it posted when I uploaded the file

You're going about this all wrong. Instead of trapping the code in while loops to wait for something to happen, just set a variable that tells which state you are in and react to that variable. Separate the code taht reads from serial from the code that reacts to it.

Take this for example let's think about what it says to do:

if(input_str == 'g')
{
  do{
    digitalWrite(13, HIGH);       // turn ON LED
    digitalWrite(12, LOW);
    delay(2000);                  // wait 2 sec
    digitalWrite(13, LOW);        // turn OFF LED
    digitalWrite(12, HIGH); 
    delay(1000);                  // wait 1 sec

    input_str = Serial.read();    //check value to repeat blinking
  } while(input_str != 's');

When you pres a g, it says to keep repeating this code as long as the char it reads from serial is not an 's'. It doesn't say anything about doing anything for any letter other than 's'.

else if (Serial.available() > 0)
{
  do{
    Serial.println("You have entered an Invalid input.... Try again");
  }while(input_str != 'g' || input_str != 's');
}

This says, if serial is available to read, then print the error message as long as the char in that variable isn't a 'g' or an 's'. But it never says to read anything from Serial in this loop, so if that variable is not a 'g' or an 's' when it starts that loop it will never ever get to be one and this loop becomes infinite.

Your code should look more like this pseudocode:

input_str = Serial.read()

if input_str == 'g' then blinking = true;
else if input_str == 's' then blinking = false;
else print error message for invalid input

if blinking == true
  // blink here, blink without delay style timing
else 
  // turn your leds off

Thank you and I am trying that method but it does not seem to be working. Not sure if I set it up wrong

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 input_str; // Initialize input value to compare
input_str = Serial.read();
bool blinking;
while (Serial.available() > 0){
if (input_str == 'g'){
blinking = true;
input_str = Serial.read();
}
else if (input_str == 's'){
blinking = false;
input_str = Serial.read();
}
else {
Serial.println("You have entered an Invalid input.... Try again");
}

switch (blinking){
case true:
digitalWrite(13, HIGH); // turn ON LED
digitalWrite(12, LOW);
delay(2000); // wait 2 sec
digitalWrite(13, LOW); // turn OFF LED
digitalWrite(12, HIGH);
delay(1000); // wait 1 sec

input_str = Serial.read(); //check value to repeat blinking
case false:
digitalWrite(13, LOW); // 's' entered blinking stopped
digitalWrite(12, LOW);

input_str = Serial.read();
}
}
}

No, if you write like the pseudocode I wrote then there won’t be any while loops in your code.

I also said you should do your blinking without delay. That delay call is going to make it unresponsive.

But I am trying to achieve a constant delay when on

I'm not sure exactly what you mean there but I'm positive you don't need the delay function to make it happen. It's time to google "Blink Without Delay" and start reading tutorials on timing with millis until it makes sense. It's like reading a clock. You already know how to do it. Don't let writing it into code confuse you on the concept.

Please edit you post #9 and put
** **[code]** **
before your code and
** **[/code]** **
after your code.

Your approach is wrong :wink: You read a character and next do something if there is another character and nothing if there no other character; this would imply that you have to type two characters to get started.

The below would be a bit better

void loop()
{
  // character received from serial port; initialised with 0 to indicated nothing received
  char c = 0;
  // variable to indicate if one should blink or not
  bool blinking;

  if(Serial.available() > 0)
  {
    c = Serial.read();
    if (c == 'g')
    {
      blinking = true;
    }
    else if (c == 's')
    {
      blinking = false;
    }
    else
    {
      Serial.println("Invalid choice");
    }
  }
  
  if(blinking == true)
  {
  }
  else
  {
  }
}

Now you have the problem (as already indicated) that your blinking takes 3 seconds. So the next time that you read a character from the serial port while blinking is true is after 3 seconds. As said, study the blink-without-delay example, understand it and apply the principles in your code; Using millis() for timing. A beginners guide might be of help in understanding.

When you apply the blink-without-delay principles, you might have to change the 'blinking' variable to either global (declare before setup) or to a static variable so it's remembered between consecutive
calls to loop(). For the latter

void loop()
{
  // character received from serial port; initialised with 0 to indicated nothing received
  char c = 0;
  // variable to indicate if one should blink or not
  static bool blinking = false;

  ...
  ...
}

You can read Serial Input Basics - updated for more understanding (and examples) on serial communication.