Blink With Out Delay

Need a little help.

I am working on a real simple sketch (Simple if I knew what I was doing that is).

I have small project, that consist of one LED and one Push Button momentary switch.

I want the LED to blink at 1000ms the first time I push the Button

Then blink at 100ms the second time I push the button.

Then turn off on the third.

I have looked at the Blink with Out delay example, but I am not getting how to work it in to my sketch.

Thanks in advanced for any help.

Read and understand: http://hacking.majenko.co.uk/finite-state-machine

Look at the variable “interval”.
Think about assigning different values to it.

For Got to Post the Code.

/*
 *  Night Lite, final version
 */

int switchPin = 3;              // IC leg 2 (PB3) 
int led1Pin = 2;                // IC leg 5 (PB0)

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  digitalWrite(switchPin, HIGH);  // set internal pull-up resistor
  pinMode(led1Pin, OUTPUT);


  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                             // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                     // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } 
        else {
          if (lightMode == 1) {        // if its on
            lightMode = 2;             // make it blink slow
          } 
          else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 0;           // make it stop!
            } 

          }
        }
      }
      buttonState = val;                 // save the new state in our variable
    }

    // Light is off
    if (lightMode == 0) { // all-off
      digitalWrite(led1Pin, LOW);

    }
      //Light blinks at 1 per sec
    if (lightMode == 1) { // Blue On
      digitalWrite(led1Pin, HIGH);
      delay(1000);
      digitalWrite(led1Pin, LOW);
      delay(1000);
    }
      //light light blinks 10 x per sec
    if (lightMode == 2) { // Blue off, Green On

      digitalWrite(led1Pin, HIGH);
      delay(100);
      digitalWrite(led1Pin, LOW);
      delay(100);
    }


  }
}

For Got to Post the Code.

I see lots of "delay()"s in that code.
I thought this was about blink without delay?

AWOL:

For Got to Post the Code.

I see lots of "delay()"s in that code.
I thought this was about blink without delay?

That's what I need help with. I not sure how to do it in my code.

kculm:
That's what I need help with. I not sure how to do it in my code.

Instead of changing the mode variable, change the interval

Arrch:

kculm:
That’s what I need help with. I not sure how to do it in my code.

Instead of changing the mode variable, change the interval

I really appreciate your help, but I have only been doing this a few weeks and I am not sure what you mean.

kculm:
I really appreciate your help, but I have only been doing this a few weeks and I am not sure what you mean.

Do you not understand how your code works?

Do you not understand how the Blink Without Delay code works?

Arrch:

kculm:
I really appreciate your help, but I have only been doing this a few weeks and I am not sure what you mean.

Do you not understand how your code works?

Do you not understand how the Blink Without Delay code works?

I under stand how my code works and I under stand how the blink with out delays works in the example code.

I am just lost when it comes to putting a button in the Mix and two speed for the blinking.

So then you understand how to manually change the blink speed in the blink without delay example by modifying the code?

Arrch:
So then you understand how to manually change the blink speed in the blink without delay example by modifying the code?

Yes

I think I may of asked the wrong question.

In the BWOD example It is only blinking the LED at a set Speed. I want to able to change the speed with a push of the button.

Now typing it out I think I know what you meant when you said change the interval.

Ok, I think I am one the right path. But now I am having a issue with the else statment.

More to the point I never know where to put the { } .

Also can someone tell me if I am on the right path.

Thanks.

const int ledPin =  2;      // the number of the LED pin
int switchPin = 3;          // number of switch Pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated


long interval = 0;           
int val;
int val2;
int buttonState;
int mode = 0;


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);  
  Serial.begin(9600);  
 
  buttonState = digitalRead(switchPin);
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds 
  val2 = digitalRead(switchPin);


  if (val == val2){
    if (val != buttonState)
      if (val == LOW){
        if (mode == 0) {
          mode = 1;
          interval = 100;
        }    
      }
       else
     {
       interval = 3000;
     }  
      buttonState = val;                 // 
}
  
  Serial.println(interval);

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

kculm:
More to the point I never know where to put the { } .

Put each curly brace on its own line and use the Tools > Auto Format to fix the indenting.

Ok, I am so close I can taste it.

I got it working some what.

when The sketch starts the LED is on. (Not what I want)

When I press the button it starts blinking at 1000 ms (That part is right)

but I have to hold the button down to get it to blink at 100ms.

I know it has to do with how I am storing the mode its in, but I don't know what.

if (val != buttonState)
{
  if (val == LOW)
  {
    if (mode == 0)
    {
      mode = 1;
      interval = 100;
    }    
  }
  else
  {
    interval = 3000;
  }  
}

After fixing your cod indenting, it should become evident that your else is incorrect. What you are saying here is "when the switch goes from HIGH to LOW set mode to 1 and interval to 100. When the switch goes from LOW to HIGH, set the interval to 3000. Your else should probably be nested within the signal edge detection if statement.

In the interest of introducing some "best (well, better) practices", try breaking your problem down into smaller parts that are more easily understood:

void loop() {
   read_button_and_adjust_time();
   maybe_flash_led();
}

OK so far, right? That was pretty trivial. But we've already made things easier; the led timing and the button timing will no longer be "mixed up."
The maybe_flash_led() comes pretty straight from blink_without_delay(), except it has code for "off"

int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)
void maybe_flash_led() {
  unsigned long currentMillis = millis();
  if (interval == 0) {  // turn off?
    if (ledState == HIGH) {
       ledState = LOW;
       digitalWrite(ledPin, ledState);
    }
    return;  // don't do the rest
  }
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}

(most of that was copy&paste from the blinkwithoutDelay sketch. No delay() calls at all) Now you have to write the button code. Debouncing without delays might be a bit tricky, so ... let's just defer that for now.

void read_button_and_adjust_time() {
  if (read_button_w_debounce()) {
     // The button had been pressed.  Adjust our time interval.  1000ms, 100ms, off.
     if (interval == 1000) {
       interval = 100;
     } else if (interval == 100) {
       interval = 0;   // off
     } else if (interval == 0) {
       interval = 1000;
     } else {
       // just to be safe, make any unknown interval go to 1000ms
     }
  }
}

That was straightforward as well, right? The trick is to make each little piece of the program be easy to understand. Reasonably easy, anyway. Now we have reading the button itself. the way the code has been arranged so far, we only want the function to return TRUE when the button changes state, not continually when it is IN a particular state. Now, WHICH state transition you want to sense is up to you. You have UP, debouncing on the way down, DOWN, and debouncing on the way up again. You can act at either the start or the end of the debounce time, as long as you only act once!

#define BUT_UP 0
#define BUT_DBOUNCE 1
#define BUT_DOWN 2 
#define BUT_UBOUNCE 3
char button_state = BUT_UP;
boolean read_button_w_debounce() {
  static unsigned long changetime;  // when the last state change occurred.
  boolean pin_state = digitalRead(buttonpin);  // (pin state of 0 means pressed)
  if (button_state == BUT_UP) {
     // Button was not pressed.  See if it's pressed now
     if (pin_state == LOW) {  // Pressed?
       button_state = BUT_DBOUNCE;  // now we're debouncing the press
       changetime = millis();
     }
  } else if (button_state == BUT_DBOUNCE) {
    // Button is being pressed but might be bouncing.
    if (pin_state == LOW  && (millis() > (changetime + 10))) {
      pin_state = BUT_DOWN;
      return true;  //  this is what counts as a button press!
    } else if (pin_state == HIGH) { // it looks like the pin is bouncing
      changetime = millis(); // so extend our debounce time.
    }
  } else if (button_state == BUT_DOWN) {
    // Button is held down, and has been.  Check for release.
    if (pin_state == HIGH) { // Released?
      button_state = BUT_UBOUNCE;
      changetime = millis();
    }
  } else if (button_state == BUT_UBOUNCE) {
     // exercise for the reader :-)
  }
  return false;  //any state or transition that hasn't returned true, returns false
}

This is an example of what's known as "top down" program design; you start with your big problem, and break it apart into smaller pieces until each piece is "easy." The other common technique is "bottom up" design, where you start with pieces that are easy enough to write, and combine them to make bigger programs.

With Help, This is what I ended up with.

Thanks for all the help.

const int ledPin =  1;      // the number of the LED pin
int switchPin = 3;          // number of switch Pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated


long interval = 0;          
int val;
int val2;
int buttonState;
int mode = 0;


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH); 
  //Serial.begin(9600); 

  buttonState = digitalRead(switchPin);
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds
  val2 = digitalRead(switchPin);

  if (val == val2){
    if (val != buttonState){
      if (val == LOW){

        if (mode == 0){
          interval = 1000;
          mode = 1;
        }
        else if (mode == 1){
          interval = 500;
          mode = 2;
        }
        else if (mode == 2){
          mode = 0;
        }
      }
      buttonState = val;
    }
  }

  // Serial.println(interval);
  unsigned long currentMillis = millis();
  if (mode == 1 || mode == 2){
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
     // Serial.println(mode);
      previousMillis = currentMillis;  

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
}