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);
}
}
// 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.
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
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.
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