HELP me with a code for LED and BUTTON

 void setup(){
   pinMode(8, INPUT);
   pinMode(13,OUTPUT);
   
 }

 void loop(){


   if( digitalRead(8) == 1 ){
     digitalWrite(13, HIGH);
   }
   
   else {
   digitalWrite(13, LOW);

   }
 }

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..

As always, take a look at the blink without delay example.

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.

You’ll have to keep the pressed/released time somewhere then use it to turn the LED ON or OFF.

AWOL:
As always, take a look at the blink without delay example.

I can get this code to work…

int count =0;

 void setup(){
   pinMode(8, INPUT);
   pinMode(13,OUTPUT);
   
 }
 
 void loop(){
  
   count = millis();

   if( digitalRead(8) == 1 && count <= 3000 ){
     digitalWrite(13, HIGH);
   }
   
   else {
   digitalWrite(13, LOW);

   }
 }

But how do I reset the count, so that the next time I press the button the count starts from 0…??

see below code…

unsigned long int count =0;
unsigned long int prevTime = 0;
 void setup(){
   Serial.begin(9600);
   pinMode(8, INPUT);
   pinMode(13,OUTPUT);
   
 }
 
 void loop(){
  
   count = millis();

   if(/* digitalRead(8) == 1 &&*/ (count - prevTime <= 3000) ){   
     Serial.println("Less than 3 Sec");
     digitalWrite(13, HIGH);
   }
   
   else {
   prevTime = count;
   digitalWrite(13, LOW);
  Serial.println("More than 3 Sec");
    delay(1000); // to check only
   }
 }

Using the TIme library found here : http://www.pjrc.com/teensy/td_libs_Time.html
Use millis() if you don’t want to rely on a 3rd party lib.

#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
 }

you don’t need to use a library with your requirements… plus you should invest the time into learning BlinkWithoutDelay, as AWOL suggested.

try something like this…

int ledPin = 13;
int buttonPin = 8;
int lastState;
boolean ledState = false;
unsigned long ledStartTime;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin,OUTPUT);

}

void loop()
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState != lastState && ledState == false)
  {
    Serial.println(buttonState? "HIGH" : "LOW");
    ledStartTime = millis();
    ledState = true;
  }
  lastState = buttonState;
  if (ledState == true)
  {
    if (millis() - ledStartTime <= 3000UL)
    {
      digitalWrite(ledPin, HIGH);
    }
    else
    {
      digitalWrite(ledPin, LOW);
      ledState = false;
    } 
  }
}
{return(loop());}

WTF?

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?

      start = millis(); 
      delay(200);
      Serial.println("Started...");

Be sure that you deduct this wasted time from the reaction time.

if( digitalRead(buttonPin) == LOW )     
 {

That is NOT waiting for the pin to go LOW.

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!!!!

belinda:
.return loop doesn’t create confusion.

No, not much.

OK to avoid risks I have cancelled it now thanks