Blinking LED From Serial Read [SOLVED]

I’d like to be able to control things from the serial monitor: to turn a blinking light on and to be able to turn it off (separate commands).

What’s happening is that I’m able to turn the blinking on from the serial monitor using the switch case you see below BUT the program fails to respond after that (and just continues blinking with no more serial input available to turn off or provide other instructions).

Any advice or suggestions would be great!
Thank you for your time

Here’s the code:

unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)
const int ledPin = 6;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
boolean state = false;

void setup() {
  // put your setup code here, to run once:
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    Serial.print("I received: ");
    Serial.println(inByte);
    switch (inByte) {
      case 48:
        digitalWrite(7, HIGH);
        Serial.println("Test1");
        state = true;
        break;
      case 49:
        digitalWrite(7, LOW);
        Serial.println("Test2");
        state = false;
        break;
    }
      while (state == true){
     Blink();
  }
    Serial.println(state);
  }
}

void Blink() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
    Serial.println("Test3");
  }
}

while (state == true) will block :wink:

Use an ‘if’ instead.

And you probably also want to move it out of the if (Serial.available() > 0) block.

Also there is a 50% chance that the LED will be on when state becomes false. so add an 'else' to that 'if'.

You guys are wicked!

This is part of why arduino is fun; community support.

Finished code:

unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
const int ledPin = 6;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
boolean state = false;

void setup() {
  // put your setup code here, to run once:
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    Serial.print("I received: ");
    Serial.println(inByte);
    switch (inByte) {
      case 48:
        digitalWrite(7, HIGH);
        Serial.println("Test1");
        state = true;
        break;
      case 49:
        digitalWrite(7, LOW);
        Serial.println("Test2");
        state = false;
        break;
    }
    Serial.println(state);
  }
  if (state == true) {
    Blink();
  } else {
    digitalWrite(6, LOW); // check to make sure it's off incase it was still on
  }
}

void Blink() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
    Serial.println("Test3");
  }
}