The code above switches the LED on pin 13 ON when the button on pin 8 is pressed, and goes OFF when the button is released..
I want the button to be kept pressed for a long time, but the LED should go OFF after say 3 seconds though the button is kept pressed.
The same thing should happen every time the button is kept pressed for a long time..
#include <Time.h>
#define DEBUG
byte inPin = 8;
byte outPin = 13;
int pressed_time = 0;
int released_time= 0;
int timeout = 3; // Timeout after 3seconds
void setup(){
pinMode(inPin, INPUT);
pinMode(outPin,OUTPUT);
#ifdef DEBUG
Serial.begin(9600);
#endif
}
void loop(){
if(digitalRead(inPin) == HIGH) // is our input pin HIGH ?
{
pressed_time = now(); // Store the pressed time if HIGH
#ifdef DEBUG
Serial.println("Pressed");
#endif
digitalWrite(outPin, HIGH); // HIGH the LED pin
}
else // Our pin is LOW
{
released_time = now(); // Store the released time
if((pressed_time + timeout) > released_time) // Checks if the pressed time + 3second is more than the released time; Don't do anything to the LED if it is.
{
#ifdef DEBUG
int dif = (pressed_time + timeout) - released_time;
Serial.print("Not Pressed but we keep running for:");
Serial.println(dif);
#endif
}
else // It is not anymore, turn the LED pin LOW
{
#ifdef DEBUG
Serial.println("Not Pressed and timed out.");
#endif
digitalWrite(outPin, LOW);
}
}
#ifdef DEBUG
delay(100); // debug
#endif
}
The loop() function does not return a value. The function that this statement is in does not return a value.
You should NOT be calling loop() from this function.
for a random period of time (between 3 & 7 seconds)
delay (7000);
Nothing random about that delay!
digitalWrite(ledPin, HIGH);
if (digitalRead(ledPin)==HIGH )
You just made the pin HIGH. How is the if statement EVER going to not be true, unless you've toasted the hardware? If you have, does the rest of the code matter?
Hi Paul from Seattle thanks a lot for answering...
Here in this forum seems to be lost in the ocean...and nobody can save you...anyway the problem should be solved with while () ; (waiting for the button to be pushed) and without if...I am writing from my phone so it is difficult to explain...return loop doesn't create confusion...bye cheers!!!!