Using a button as a relay?

I am trying to writea sketch that allows me to press a button,. have something happen for a specified amount of time, and then stop and wait for the next press of the button. Specifically in this sketch, I am simply trying to get the LED on pin 13 to turn on for 3 seconds at the press of the button and then stop, waiting for the next press. Any help would be greatly appreciated! Here is the current sketch and schematic:

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

int switchPin = 8;
int ledPin = 13;
int buttonPresses = 0;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()

{
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
}

boolean debounce(boolean last) //just a debounce function

{
boolean current = digitalRead(switchPin);
if (last != current){
delay(5);
current = digitalRead(switchPin);
}
return current;
}

void loop()
{
{currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH){
ledOn = !ledOn;

buttonPresses++;
lcd.clear();
lcd.print("Number of Button ");
lcd.setCursor(0,1);
lcd.print("Presses = ");
lcd.print(buttonPresses);
}

lastButton = currentButton;
digitalWrite(ledPin, ledOn);

}
}

So far this works with pressing the button once and turning the LED on, and displaying the button count and then pressing again and turning it off. Is there anyway to do what I am asking? Thank you!

For starters

boolean debounce(boolean last) //just a debounce function

should be

boolean debounce(boolean last) { //just a debounce function

Does that even compile?

Also this debounce function doesn't really debounce, if you read a != state all you do is wait 5mS, you don't test the state again.

Yeah like I mentioned it compiles and runs just fine, I just am looking for the button relay. As for the { that you say is missing it is literally right under the function name so yes, it works. I am not very good at programming which is why I am looking for help, so as for the debounce function as well do you possibly have any suggestions for that? Thanks again.

Have a look at the Debounce and StateChangeDetection example sketches, for handling switches.
Also have a look at BlinkWithoutDelay for time based actions.

What you want is detect when the button is pressed. Then set a flag, e.g. ledOn, instead of toggling it. Also store the lastPressedTime=millis(), and start whatever should happen now. Next check the flag, and if set check for the elapsed time millis()-lastPressedTime >= HoldTime, and if true stop the action and clear the flag.

A relay is an electrically operated switch. How could a button be a relay?

...R

#include <Bounce2.h>

const byte ButtonPin = 8;
const byte LedPin = 13;

unsigned long ledOnTimer = 0;

const unsigned int = ledPeriod = 3000;

Bounce button;

void setup(){
  button.attach(ButtonPin, INPUT_PULLUP);
  pinMode(LedPin, OUTPUT)
}

void loop(){
  //if led is on
  if(digitalRead(LedPin)){
    //time to turn it off?
    if(millis() - ledOnTimer => ledPeriod){
      digitalWrite(LedPin, LOW);
    }
  }
  //if led is off
  else{
    button.update();
    if(button.fell()){
      ledOnTimer = millis();
      digitalWrite(LedPin, HIGH);
    }
  }
}

Thank you so much for the responses. I apologize for some of the more naïve mistakes such as forgetting the code tags as you mentioned I am still getting the hang of all this. I am going to try now all the suggestions you made and I'll see if I can figure it out.

septillion:

#include <Bounce2.h>

const byte ButtonPin = 8;
const byte LedPin = 13;

unsigned long ledOnTimer = 0;

const unsigned int = ledPeriod = 3000;

Bounce button;

void setup(){
  button.attach(ButtonPin, INPUT_PULLUP);
  pinMode(LedPin, OUTPUT)
}

void loop(){
  //if led is on
  if(digitalRead(LedPin)){
    //time to turn it off?
    if(millis() - ledOnTimer => ledPeriod){
      digitalWrite(LedPin, LOW);
    }
  }
  //if led is off
  else{
    button.update();
    if(button.fell()){
      ledOnTimer = millis();
      digitalWrite(LedPin, HIGH);
    }
  }
}

Thank you so much Septillion. I was able to modify it a bit to fit my purposes and it worked. I really appreciate it!

As for the { that you say is missing it is literally right under the function name

Don't mind me, I must be blind.

jonathantalvarez:
I apologise for some of the more naïve mistakes such as forgetting the code tags

Don't apologise. :grinning: Go back and modify the post to put them in.