LED control via serial port

i am trying to light up an LED for 3 seconds when there is a value "1" in the serial port and turn it off afterward. when there is no value, then the LED is off. i've made this code but it only turns the LED on constantly.

please help.:slight_smile:

  const int ledPin = 13;      // the pin that the LED is attached to
  long previousMillis=0;  //previous millisecond
  unsigned long currentMillis = 0;//current millisecond
  int SerialValue=0;
  
  
  
void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  SerialValue=0;
  previousMillis =0;
  currentMillis =0;
  if (Serial.available()) {
    SerialValue = Serial.read();
    if (SerialValue == '1') //if SerialValue = 1, turn LED on for 3 seconds
    {
      previousMillis = millis(); 
        currentMillis = millis();
    while(currentMillis - previousMillis <= 3000) 
        {
        digitalWrite(ledPin,HIGH);  
        currentMillis = millis();
        }
        digitalWrite(ledPin, LOW);//turn LED off after 3 seconds
    }
    else //if SerialValue is not = 1, LED off
    {
      digitalWrite(ledPin, LOW);
    }
  }
}

All your code is blocking, so you may as well just use "delay()"

what do you mean by blocking? i have read that i shouldn't use delay because it cannot sense if there is a switch pressed.i was planning on putting a switch to halt the lighting of the LED. so that i can immediately turn off the LED even if three seconds have not passed yet.

Look at the way that you reset variables each time through "loop" - that isn't going to help

do you mean this?
SerialValue=0;
previousMillis =0;
currentMillis =0;

but even if i remove them it still won't work. :frowning:

How would you turn the LED on and off manually, using nothing but a watch (millis()), a pencil and a piece of paper?

Turn the LED on. Write down the time. Periodically, check the time.

If enough time has passed, turn the LED off, and write down the time.

You don't need to just stand there doing nothing in between turning the LED on or off.

But, that is what you have your code doing. Turn the LED on. Do nothing until it is time to turn the LED odd. Turn it off. Do nothing until it is time to turn the LED on again. You might as well be using the "do-nothing-until" function, also known as delay.

Look at, and understand, the blink without delay example.

yes, i have looked at the blink without delay function. but it only does the blinking without specified time of how long to turn an LED on. i want that the LED be on for 3 seconds.

yes, i have looked at the blink without delay function. but it only does the blinking without specified time of how long to turn an LED on.

Well, go have another look, then. It most definitely has a specified on time and a specified off time (they are the same).

you're right but that is not what i meant. i mean that, like in the code i posted, i need that the LED will be on for 3 seconds and then off. the LED will only light up again if there is another "1" in the serial port. but the example in the blink without delay, it is in a continuous loop having 1 second interval for the on and off times.

Below is some test code I tweeked that might work for you.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
	Serial.begin(9600);
        pinMode(13, OUTPUT);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(1);  
    	if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
     
    if (readString == "1") {
	digitalWrite(13, HIGH);
        Serial.println("Led On");
        delay(3000);
        digitalWrite(13, LOW);
        Serial.println("Led Off");
    }
           readString="";
   } 
}

if i use delay(), i won't be able to use a switch to halt the LED from lighting up. i am planning on using a switch.:slight_smile:

if i use delay(), i won't be able to use a switch to halt the LED from lighting up.

Sure you will. It's just that the switch will be unresponsive. Using you replacement for the delay function will have the same problem.