latching LED with flash on and off with momentary button.

Hello All,

I am new to the forum and very new to arduino. I am looking to control two independent flashing LED lights with momentary button to turn on and off. So one button would start flashing and stop flashing for one light and another button would start flashing and stop flashing for the other light.
Basically like a turn signal.

I am working through the tutorials and trying to get up to speed as fast as possible, but the above code is way above my head at the moment.

I am using an arduino UNO.

thanks for help in advance.

I am working through the tutorials and trying to get up to speed as fast as possible, but the above code is way above my head at the moment.

No, it isn't. The state change detection example and the blink without delay example have all the details you need.

I was working on a blink without delay and a debounce, but I will check out state change detection.

thanks

This is my first attempt, I merged (at least I think I did) state change and blink without delay. It loads fine but the LED starts out on, and only turns off and on with the button. I would like it to start with Led off and turn on with button and flash until button is pressed again.

/*
 State change detection with blink without delay


*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int ledState = LOW;          // ledState used to set the LED

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)



void setup() {
 // initialize the button pin as a input:
 pinMode(buttonPin, INPUT);
 // initialize the LED as an output:
 pinMode(ledPin, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
}


void loop() {
 // read the pushbutton input pin:
 buttonState = digitalRead(buttonPin);

 // compare the buttonState to its previous state
 if (buttonState != lastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     buttonPushCounter++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(buttonPushCounter);
   } else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 lastButtonState = buttonState;


 // turns on the LED every two button pushes by
 // checking the modulo of the button push counter.
 // the modulo function gives you the remainder of
 // the division of two numbers:
 if (buttonPushCounter % 2 == 0) {
   digitalWrite(ledPin, HIGH);
 } else {
   digitalWrite(ledPin, LOW);
 }


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);
 }
}


}

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...
You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

hope this helps aarg.

Starting with the LED off is easy - just set it off in setup().

sorry if this is painfull...

int ledState = LOW; // ledState used to set the LED

wouldn't this make it start off?

Hi,

int ledState = LOW; // ledState used to set the LED

wouldn't this make it start off?

No. You "Speak to hardware" in the setup() or loop() part of your sketch. Anything done before that is preparing things up. No action is made.

As aarg said, you have to explicitly do it in the setup()

I ran your sketch, and it does exactly what you ask it to do.

Each time you press the switch, the LED toggles from on to off.

// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

% 2 means: "change every 2 presses". Not four

I would like it to blink when turned on. other wise it works great

You wonder why it does not blink.

I can suggest a trick of the trade: Divide and conquer.

void setup() {
  Do your setup here
}
bool blinkModeOn() {
  Put your code that reads the button here, and return what "if (buttonPushCounter % 2 == 0)" says
}
void blink() {
  Put your blink code here.
}

void loop() {
  if (blinkModeOn()) {
    Start HIGH; init your chronometer; and
    blink();
  }
}

thanks, I will work on that jbellavance

Forget that line:Start HIGH; init your chronometer; andThe blink() routine should take care of that.

The loop() should read:

void loop() {
  if (blinkModeOn()) {
     blink();
  }
}

Desmo,

You can use Bounce2 library GitHub - thomasfredericks/Bounce2: Debouncing library for Arduino and Wiring
It take care all the debounce matters and very easy to use. You don't even need to use pull-up resistor on your button, just connect ur pushbutton to ground & I/O pin.

Hope it helps.

Regards,
Bronson
www.pt-altraman.com

Bronson_alex, do you have any examples using bounce2 library for reference?

thanks

Here is an example. Press button once to turn on led, press again to turn off led:

#include <Bounce2.h>

//PUSH BUTTON use direct connection (P7 and GND). No need for 10K resistor and 5V


int pb1 = 7;
int led1 = 13;
int state=0;
Bounce debounce1 = Bounce();


void setup() {
  Serial.begin(9600);
  pinMode(pb1,INPUT_PULLUP);
  pinMode(led1,OUTPUT);
  debounce1.attach(pb1);
  debounce1.interval(5); // interval in ms
  
}

void loop() {
  debounce1.update();
  if (debounce1.fell())   //if pressed (transition occurs)
    {
      if (state==0)
        {
          digitalWrite(led1,HIGH);
          state=1;
        }
       else
        {
          digitalWrite(led1,LOW);
          state=0;
        }
    }
}