I'm trying to use the Arduino serial monitor to receive input, and do something if it is a certain integer

1.4k Views Asked by At

I'm trying to make a simple program to turn a laser on and off. It was working fine until I tried to be able to make it blink by inputting a 2 to the serial monitor. It blinks, but only once. What I'm trying to make it do is blink forever, while also waiting for the user to input something to be able to stop blinking. Here is my code.

int laserPin = 3; 
int laserState; 

void setup()
{
  pinMode(laserPin, OUTPUT);  // set pin10 as output pin
  digitalWrite(laserPin, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
  Serial.println("     Commands");
  Serial.println("--------------------");
  Serial.println("2- makes laser blink");
  Serial.println("1- turns laser on");
  Serial.println("0- turns laser off");
}
void loop()
{
  Serial.println("Set laser state to: "); //Prompt User for Input
  while (Serial.available() == 0) {
    // Wait for User to Input Data
  }
  laserState = Serial.parseInt(); //Read the data the user has input
  Serial.println(laserState);
  if(laserState == 1){
    Serial.println("Turning laser on");
    digitalWrite(laserPin, HIGH);
    }
   else if(laserState == 0){
      Serial.println("Turning laser off");
      digitalWrite(laserPin, LOW);
   }
   else if(laserState == 2){
      Serial.println("Making laser blink");
      while(laserState == 2)
      {
        digitalWrite(laserPin, HIGH);
        delay(1000);
        digitalWrite(laserPin, LOW);
        Serial.println("Set laser state to: "); //Prompt User for Input
        laserState = Serial.parseInt(); //Read the data the user has input
      }
   }
}

Here is the code I made to make the laser blink:

   else if(laserState == 2){
  Serial.println("Making laser blink");
  while(laserState == 2)
  {
    digitalWrite(laserPin, HIGH);
    delay(1000);
    digitalWrite(laserPin, LOW);
    Serial.println("Set laser state to: "); //Prompt User for Input
    laserState = Serial.parseInt(); //Read the data the user has input
  }

}

1

There are 1 best solutions below

0
On

If you want to make the arduino do more than one thing at a time, you need to let the loop keep running and keep track of the state - rather than holding it up with while loops.

I've taken your code and modified it slightly to allow the serial to be read, and the laser to be flashed.

In this version laserState is normally set to -1 - which means the loop won't do anything apart from check the serial input each time.

When you input a serial int - this will change laserState and the appropriate action will be performed.

Immediately after switching on and off the laser, laserState is set back to -1 so no further actions occur.

However - if laserState is set to 2, it will stay at that value for each loop iteration and the laser will continue to flash until another serial input is received to change the state.

There's also a flag to determine when to display the prompt (although it will display everytime the led flashes).

This could be made more responsive by removing the delays in the flash and having a timer using millis() to determine when to switch the laser on and off - so you don't have to wait for a flash cycle to read the next serial input - but that would be a bit more complicated.

int laserPin = 3; 
int laserState = -1; 
bool showPrompt = true ;

void setup()
{
  pinMode(laserPin, OUTPUT);  // set pin10 as output pin
  digitalWrite(laserPin, LOW); // set the pin value on low at the begin
  Serial.begin(9600);
  Serial.println("     Commands");
  Serial.println("--------------------");
  Serial.println("2- makes laser blink");
  Serial.println("1- turns laser on");
  Serial.println("0- turns laser off");
}
void loop()
{
  if ( showPrompt ) {
    Serial.println("Set laser state to: "); //Prompt User for Input
    showPrompt = false ;
  }
  if (Serial.available()) {
    // Read the User Input Data
    laserState = Serial.parseInt(); //Read the data the user has input
    Serial.println(laserState);
  }

  if(laserState == 1){
    Serial.println("Turning laser on");
    digitalWrite(laserPin, HIGH);
    laserState = -1 ; 
    showPrompt = true ;
  }
  else if(laserState == 0){
    Serial.println("Turning laser off");
    digitalWrite(laserPin, LOW);
    laserState = -1 ; 
    showPrompt = true ;
   }
   else if(laserState == 2){
     Serial.println("Making laser blink");
     digitalWrite(laserPin, HIGH);
     delay(1000);
     digitalWrite(laserPin, LOW);
     delay(1000);
     showPrompt = true ;
   }
}