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!