arduino led blinking mode

Hi guys,

I am pretty new on Arduino programming but I have experiences with other developing approaches. I kindly ask you to help me with my problem.

I have a led diode and a push button in my circuit. I would like to create a script wich will allow to users to select more then one mode of led blinking using the push button.

For example: After first pressed on the button led will turn on. Then second pressed will change mode and led will start blinking and after third push the led will turn off.

The problem is when the program comes to second mode (led blinking). There is one second delay between turn on and turn off and then when is the program in delay mode push button is not accessible. As I know arduino doesn't support multithreading. Is there any other options how to solve this?

I would like to led will blinking and after push button will be pressed the led will turn off.

Here is my code.

void loop() {
  
  buttonState = digitalRead(buttonPin);
  
  if(buttonState == HIGH && previous == LOW && millis() - time > debounce)
  {
    if(flag == 0)
    {
      Serial.print("mode -> ");
      Serial.println(mode);
      
      if(mode == 0)
      {
        stateLed = HIGH;
      }
      previousMode = mode;
      mode++;
    }
    flag = 1;
    time = millis();
  }
  else if(buttonState == LOW)
  {
      flag = 0;
  }
  
  if(previousMode == 0)
  {
    digitalWrite(led, stateLed);
  }
  else if(previousMode == 1)
  {
    digitalWrite(led, LOW);
    delay(1000);
    digitalWrite(led, HIGH);
    delay(1000);
  }
  else if(previousMode == 2)
  {
    digitalWrite(led, LOW);
    mode = 0;
  }
}

Many new people get caught up in the trap of using delay() The response to them is always the same, look at the blink without delay example.

I don't think i have ever seen someone who has a debounce timer for the button get caught by the delay trap.

First time for everything i suppose.

mango89: For example: After first pressed on the button led will turn on. Then second pressed will change mode and led will start blinking and after third push the led will turn off.

The problem is when the program comes to second mode (led blinking). There is one second delay between turn on and turn off and then when is the program in delay mode push button is not accessible. As I know arduino doesn't support multithreading. Is there any other options how to solve this?

Such problems can always be solved by strictly following the IPO programming model: - Input - Processing - Output

In an Arduino sketch your loop() function would look like that:

void loop()
{
  input();
  processing();
  output();
}

And it would be up to you to write three functions to make your program work in "cooperative multitasking".

input() function reads all the input processing() function does all the logical processing output() function sends all the logical output to the world outside the Arduino

The function "delay()" is a blocking function, which is totally forbidden in any cooperative multitasking application.

Need some example worked out?

jurs, LarryD thanks to both of you to directed me on the right way.

millis() function si very important at arduino developing. Am I right?

Yes very important. Also try to master the technique of using State Machines. http://www.gammon.com.au/forum/?id=12316 Also there is a thread about doing several things at once on this web site. http://forum.arduino.cc/index.php?topic=223286.0