I want to hold a button for 3 seconds and after that it turns on an led for 5 seconds then off... Its not working though, right now i load the code into my uno, the led flashes 3 times fast turns off for a few seconds then turns on and stays on... here is my code, what is wrong?
const int button1Pin = 2; // pushbutton 1 pin
const int ledPin = 13; // LED pin
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
int button1State; // variables to hold the pushbutton states
int count; //variables to count the time button is held
button1State = digitalRead(button1Pin);
if (button1State == HIGH)
{
count = count + 1;
}
if (count > 3000);
{
digitalWrite(ledPin, HIGH); // turn the LED on
delay (5000);
digitalWrite(ledPin, LOW); // turn the LED off
}
}
This is simply the Blink Without Delay example but with the State feature.
State 0 = waiting for button to be pressed
State 1= button pressed and waiting for time to be elapsed
State 2= led on and waiting for time to be elapsed
If the button is released in the state 1 and the time is not elapsed it come back to state 0.
ok that makes much more sense, I actually didnt see your first posts, just the one at the bottom with the code, I like the idea of not delaying other code with the delay function, see what you are saying there.
So i put on a Relay shield and modified some of the pins and constant names. Im going to use the button to hook up to one that is unused on my Range Rover steering wheel, then when you hold it down for 1.5 seconds it triggers the relay which opens the exhaust dump valves, giving a more aggressive exhaust tone for that V8.
Here is the next bit i have to add: Hold down the button for another 1.5 seconds to close the dumps... or maybe click the button twice to open and twice to close?
i want to hold the button down for 3 seconds to turn the led on and then hold button down for 3 seconds again to turn the led off ,please help me with the code.
i know that debounce code in examples of arduino can do what i want but i wnat a different code
#include <mechButton.h>
#include <timeObj.h>
#define DUMP_PIN 13
#define BTN_PIN 2
timeObj buttonTimer(1500); // Button dump timer for 1.5 sec.
mechButton myButton(BTN_PIN); // The button, debounced and everything.
bool dumpOpen;
void setup(void) {
dumpOpen = false;
pinMode(DUMP_PIN,OUTPUT);
digitalWrite(DUMP_PIN,LOW);
myButton.setCallback(buttonCliked);
}
void buttonCliked(void) {
if (!myButton.trueFalse()) {
buttonTimer.start();
}
}
void doDump(void) {
if (dumpOpen) {
// Close dump
digitalWrite(DUMP_PIN,LOW);
} else {
// Open dump
digitalWrite(DUMP_PIN,HIGH);
}
dumpOpen = !dumpOpen;
}
void loop(void) {
idle(); // Run background things, like the button stuff.
if (!myButton.trueFalse()) { // If the button is being held down..
if (buttonTimer.ding()) { // If the button timer has expired (**ding!**)..
doDump(); // Switch the dump.
buttonTimer.reset(); // And we are done with this button press for now.
}
}
}
You'll need to install LC_baseTools from the library manager to compile this but I think it'll do what you want.