I'm trying to do a counter and use the time...for example, if i press 5 times a button...illuminate 5 seconds a LED
int ledPin = 40; // LED connected to digital pin 13
int ledValue = LOW; // previous value of the LED
long ledStartTime = 0; // will store last time LED was updated
long ledBlinkInterval = 1000; // interval at which to blink (milliseconds)
int buttonPin = 22; // an active-high momentary pushbutton on pin 2
int buttonValue = HIGH; // the current state of the button
long buttonPressTime = 0; // will store the time that the button was pressed
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
// Check to see if the button is pressed. If so, mark the time. When the
// button is released, calculate how long it was pressed, and make the led
// blink at the same rate.
buttonValue = digitalRead(buttonPin);
// button press
if (buttonValue==LOW && buttonPressTime==0) {
buttonPressTime = millis(); // capture the time of the press
}
// button release
if (buttonValue==HIGH && buttonPressTime!=0) {
ledBlinkInterval = millis() - buttonPressTime; // set the new flash interval
buttonPressTime = 0;
int ledPin = 40; // LED connected to digital pin 13
int ledValue = LOW; // previous value of the LED
long ledStartTime = 0; // will store last time LED was updated
long ledBlinkInterval = 3000; // interval at which to blink (milliseconds)
int buttonPin = 22; // an active-high momentary pushbutton on pin 2
int buttonValue = HIGH; // the current state of the button
long buttonPressTime = 0; // will store the time that the button was pressed
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
// Check to see if the button is pressed. If so, mark the time. When the
// button is released, calculate how long it was pressed, and make the led
// blink at the same rate.
buttonValue = digitalRead(buttonPin);
// button press
if (buttonValue==LOW && buttonPressTime==0) {
buttonPressTime = millis(); // capture the time of the press
}
// button release
if (buttonValue==HIGH && buttonPressTime!=0) {
ledBlinkInterval = millis() - buttonPressTime; // set the new flash interval
buttonPressTime = 0; // clear the button press time
}
// check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
if (millis() - ledStartTime > ledBlinkInterval) {
ledStartTime = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (ledValue == LOW)
ledValue = HIGH;
else
ledValue = LOW;
digitalWrite(ledPin, ledValue);
}
}
i was missing a part, sorry...yes compile and works, basically This code places intervals of time to blink, and I need is, if I press a button X number of times the LED stays lit X amount of time and turns off after the time expires
Am I correct to think that your existing program measures the time that a button is pressed and then uses that for the blink interval?
If you want to count button presses then you need code to make sure the button is released and pressed again - assuming the buttonValue shows LOW when pressed then the count would increment every time the buttonValue is LOW and previousButtonValue is HIGH
Do you also want the existing system to work alongside the counting system? If so that might be complicated.