coding a button switch

I'm trying to create a button switch that when pressed it turns a light on briefly and then turns the light back off until the button is pressed again. I'm looking for the light to shut back off regardless as to whether the button is still being pressed.

I created a button which does turn the relay on. I set an "on" delay as well as an "off" delay... this kinda replicates what I'm after however I don't want the light to be able to come back on until after the button has first been released and then pressed the second time around.

Also when using the delay in my code I end up having to wait till the delay time expires before I can re-trigger the button (light)... which I don't want. I simply would like to toggle this light for a brief moment (50 ms) as fast or slow as I like and not be restricted by any "off time" delay.

Thanks a million for any assistance!!!

if (digitalRead(btnPin2) == HIGH)
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin, LOW); 

You need to use the principles of the BlinkWithoutDelay example in the IDE.
Note the time that something happens (the button becomes pressed) and turn on the light, then check frequently (each time through loop()) whether it is time to do something else (turn off the light)

The other thing to do is to change from detecting when the button IS pressed to detecting when the button BECOMES pressed. The StateChangeDetection example in the IDE will show you how to do this.

To give you another clue, a button BECOMES pressed when you detect the transition from not pressed to pressed. This means that you need to remember the previous state of the switch.

Thanks for the replies!!! I definitely see the characteristics of the two your saying I need to implement into my code. I have set up each of them with my Arduino UNO to see exactly how they work which is very cool!

Ive only got about a week or so of doing this under my belt! I will let this smoke my brain for a while and see if I can figure out how to work this all together!!!! I may need to use a lifeline on this one!! lol! But thanks so far for getting me in the right direction!

OK yes this has been smoking my brain! lol! I'm just not familiar enough with the codes in simplest form yet alone being able to combine much of this in to one sketch and actually have it work! I'm sure this is sooo messed up but I'll attached where I thought to go with this. I am having difficulty integrating a button into the "blink w/out delay" sketch. I'm also unable to get the code to count the blinks of the led.... I'm thinking that's what you were referring to by having me look into the "state change" sketch.. Again I'm just not sure on how to integrate these all together...

Just to recap I'm trying to make an led blink once at 50ms at push of button and then have it shut off (regardless if the button is still being pressed). I don't want the led to blink again until the button has been released and then pressed again.

Thanks a million for any further instruction!!..

const int  buttonPin = 8;    // 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
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

int ledCounter = 0;
int ledState = 0;
int lastLedState = 0;

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

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  ledState = digitalRead(ledPin);
  unsigned long currentMillis = millis();

  // compare the buttonState to its previous state
  //if (buttonState != lastButtonState) {
  if (ledState != lastLedState) {
    // if the state has changed, increment the counter
    if (ledState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      Serial.print("number of blinks:  ");
    else {
      // if the current state is LOW then the button
      // wend from on to off:
  // save the current state as the last state,
  //for next time through the loop
  lastLedState = ledState;

  // 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 (ledCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  ledState = digitalRead(ledPin);

It’s better to remember the state that you write to the pin.

  //if (buttonState != lastButtonState) {
  if (ledState != lastLedState) {

Its the current versus previous state of the switch, not the LED, that drives what to do now.

Just to throw something else in mix for you - are you debouncing the switches in hardware? Because you are not doing this in software.

Mechanical switches will 'bounce' between on and off states while they make a transition, due to the nature of the mechanical contacts. At a human perception level you can ignore this, but with a microcontroller, you can be checking the switch thousands of times in a second and you will see the on/off sequence as it transitions. One common technique is to wait a few milliseconds (20 to 40, say) with the switch in the final position before deciding that is is actually switched. Debouncing can be done in hardware as well, which simplifies the software but can be more expensive as more components are needed.

Bounce may make your modulo 4 counter not work as expected.

I have a library for digital switches in my code library (link below) you can use as an example of code that works if you get stuck figuring it out for yourself.

Something like this perhaps?

buttonState = digitalRead(buttonPin);
if (ledState == LOW && buttonState == LOW) { // assumes active low
   ledState = HIGH;
   ledStartMillis = millis();
if (millis() - ledStartMillis >= ledOnMillis) {
   ledState = LOW;
digitalWrite(ledPin, ledState);


Am I adding your examples into the code I already have started???? Is your examples to replace coding I already have started??? Not sure where your examples are to go...

I don't believe there is any kind of debounce factored into the coding. No, I was not planning to do that with hardware. Id like the debounce factored into the coding. Ive seen another source showing the debounce and I actually had it working with a test sketch I did, but I don't know how to incorporate that into my present project. I'm trying to find your library now in hopes of finding a script that will be a more close and full example of how to do this.

Thanks for the tips!

The following code is the blink w/o delay code. So this is NOT the entire code for my actual project. But for start I was refered to this code as to eliminate the "delay" coding so I can eventually get this to function as Im after.

One and only thing I added (as I was able to find my problem and resolve) was to add a button to activate the blinking vs just having the board blink on its own.

So I added:
int btnPin = 8; (added at top of sketch)


if (digitalRead(btnPin) == HIGH){ (added just after opening the loop)


} (added towards bottom of the sketch)

So to sum this up, this coding blinks the light when I press the button I set up. However, I noticed that while I let off the button and the light is on, then the light will stay on and not shut off. If let the button while light is off, it stays off. I would of thought the light would shut off any time the button was released.

But from where Im at right here with this coding can anyone elaborate in a bit more detail as to what of this coding I need to specifically change to make the light blink only once at the press of a button? Again the light needs to shut off regardless as to whether or not the button is still being pressed.

I think when I resolve this part I can easily add this to the rest of my project!! Thanks a million!!!

//blink led without delay()

int ledPin =  13;
int btnPin =  8;

int ledState = LOW;//current state of the LED
unsigned long timeOfLastLedEvent = 0;//the last time the LED was updated

int intervalON = 1000;//how long we want the LED to stay on
int intervalOFF = 500;//how long we want the LED to stay off

void setup() {
  pinMode(ledPin, OUTPUT);  
  digitalWrite(ledPin, ledState); 
  pinMode(btnPin, INPUT); 

void loop() {
  if (digitalRead(btnPin) == HIGH){

  unsigned long currentMillis = millis();
  if (ledState == LOW){//if the LED is already off
    if (currentMillis - timeOfLastLedEvent > intervalOFF){//and enough time has passed
      digitalWrite(ledPin, HIGH);//turn it on
      ledState = HIGH;//store its current state
      timeOfLastLedEvent = currentMillis;//update the time of this new event
  } else {//if the LED is already on
    if (currentMillis - timeOfLastLedEvent > intervalON){
      digitalWrite(ledPin, LOW);
      ledState = LOW;
      timeOfLastLedEvent = currentMillis;

Id like the debounce factored into the coding.

Don't waste time writing debounce code until you see if you need it.

The code in planning and implementing a program does not need any. And I don't believe the code I posted in Reply #7 will need any. There is a simple system in the code in several things at a time.