Button release condition

HI ALL

PLEASE TELL ME HOW TO DO A CODING FOR WHEN BUTTON GET OFF OUTPUT (EX LED) GET BLINK 10 SECOND AND OFF. I TRY TO DO A CODE BUT ITS USELESS

SEE MY CODE BELOW.

// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Try: pinMode(buttonPin, INPUT_PULLUP);

How do you have your switch wired?

Read: http://www.gammon.com.au/forum/?id=11955

switch GND to pin 2

Your code as is should turn on the LED when HIGH and off when LOW. Does it?

yes correct i need to blink led 10 s and off

Try this very basic sketch:

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() 
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) 
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(10000);
    digitalWrite(ledPin, LOW);
  }

} //END of loop()

this is work but my purpose is different. think my door normally it closed. when someone come door reed switch open (open state) then LED ( or a beep) get blinked 10 s and off. but door still open. when he closed the door again arduino should reset and reed switch in closed mode. i want to add TX and RX for this too.

Are you saying when the door is open, the reed is open and pin 2 is HIGH?

You want the LED to turn on for 10 seconds "the moment" the door is open?

when door is open pin 2 on low ( i mean magnet is going away co contact)

below code work but no timer. led blink till the door is close.

// set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT_PULLUP); }

void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: //digitalWrite(ledPin, HIGH);

digitalWrite(13, HIGH); //set pin 13 high (+5V) delay(200); //wait 1000 ms = 1 second digitalWrite(13, LOW); //set pin 13 low (0V) delay(200); // wait 1 second

digitalWrite(ledPin, LOW); // }

} //END of loop()

I am not fully understanding what you need. Try this:

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

byte ledState = 0;
unsigned long ledTime;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() 
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if(buttonState == HIGH && ledState == 0) 
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    ledState = 1;
    ledTime = millis();
  }
  
  //if the led is on, is it time to turn it off?
  if(ledState == 1 && millis() - ledTime >= 10000UL)
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    //ledState = 0;
  }

  if(buttonState == LOW)
  {
    ledState = 0;
    digitalWrite(ledPin, LOW);
  }

} //END of loop()

i am sorry for bad english. simply here is my situation. i have placed reed switch in my room door. when door is closed. magnet near the reed switch and no alert its a standby. but when door open reed switch contact open (no magnet) then LED should blink 10 second and off. same time i want to send alert to other location via 433MHz tx.

there should be a timer for this right? reed switch open mean my system alerting.

now your code works well but no blinking if it blinking that is perfect.

thank for your kind support

You want the LED to flash on and off several times and do this for 10 seconds?

correct you got it

Try this:

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

byte ledFlag = 0;
byte enableFlag = 0; 
unsigned long ledTime;
unsigned long flashTime;
const unsigned long flashRate = 200UL;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() 
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() 
{
  buttonState = digitalRead(buttonPin);

  if(buttonState == HIGH && ledFlag == 0) 
  {
    digitalWrite(ledPin, HIGH);
    ledFlag = 1;
    enableFlag = 1;
    ledTime = millis();
    flashTime = millis();
  }
  
  if(enableFlag == 1 && millis() - ledTime <= 10000UL)
  {
    if(millis() - flashTime >= flashRate)
    {
      flashTime = millis();
      digitalWrite(ledPin, !digitalRead(ledPin));
    }
  }
  else
  {
    enableFlag = 0;
    digitalWrite(ledPin, LOW);
  }

  if(buttonState == LOW)
  {
    ledFlag = 0;
    enableFlag = 0;
    digitalWrite(ledPin, LOW);
  }

} //END of loop()

EDIT:
Updated code with enableFlag

Thank you Mr Larry your a genius

Do you understand what each line of code does in the sketch from post #14?

yes i understood the flag. and all. now im trying to burn this to tiny85. ok tiny85 has 8 pin now in my sketch there are one input and one output. how i burn boot loader.

In future i want to add TX and RX to this. when LED trigger TX send signal to RX so can i use tiny85 to this or a need to buy another chip. i mean 8 pin enough for t his??? thank for supporting