Disable push button for 30 sec after press arduino - button

I am working with Arduino as I am new need your help please Thanks!
I am turning on the LED light via push-button for 30 seconds all working fine but there is one issue after pushing the button light on start from 0 sec but if I push the button after 15 sec it will start again from 0 sec so, is there any way can I disable the push button also for 30 sec so it will run only 30 sec even I will push the button and button will work only when the light of after 30 sec.
int BUTTON = 2;
int BUTTONstate = 0;
int LED = 8;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
static unsigned long startTime = 0;
BUTTONstate = digitalRead(BUTTON);
if (BUTTONstate == HIGH){
if (millis() - startTime >= 30000)
digitalWrite(LED, LOW);
}
else{
digitalWrite(LED, HIGH);
startTime = millis();
}
}

I got the correct code working fine:
int pin1 = 13;
int LED = 8;
void setup()
{
pinMode(pin1, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
long offAt = 0;
void loop()
{
if ((digitalRead(LED) == LOW ) && (digitalRead(pin1) == LOW) ) //if LED is off and button is pressed [low because it has pullup resistor]
{
digitalWrite(LED, HIGH);
offAt = millis() + 30000; //store var of now + 5 seconds
}
if (digitalRead(LED) == HIGH) //if led is on
{
if (millis() >= offAt) //see if it's time to turn off LED
{
digitalWrite(LED, LOW); //it's time. this also re-enables the button
}
}
}

Related

Switching on the diode for some time, as well as execution of the next program in Arduino

The circuit made in tinkercad
I use the relay, because I have only 4 wires to led diode and this two switches.
int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) == HIGH and digitalRead(s2) == LOW)
{
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(s2) == HIGH and digitalRead(s1) == LOW)
{
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(k1) == HIGH && (millis() - startTime1 >= led1Duration))
{
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k2) == HIGH && (millis() - startTime2 >= led2Duration))
{
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k1) == HIGH and digitalRead(k2) == LOW)
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == LOW)
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == HIGH)
{
digitalWrite(led, HIGH);
}
}
Is there easier way to do that, cause when I assembled it the switches work very slow that blue led turn on after some time. Next I use the esp2866 but the relay for 3V doesn't turn on. How can I make it to work?
#Peter is true : avoid blocking delay code.
here is an example : I have a little simplified your code and added else if to make it more readable.
update1 :
int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
unsigned long t200 = 200;
unsigned long t500 = 500;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) && !digitalRead(s2)) {
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
blinkLed200();
} else if (!digitalRead(s1) && digitalRead(s2)) {
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
blinkLed500();
}
if (digitalRead(k1) && (millis() - startTime1 >= led1Duration)) {
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
if (digitalRead(k2) && (millis() - startTime2 >= led2Duration)) {
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
}
void blinkLed200() {
if (millis() - t200 > 200) { //tick every 200mS
t200 = millis();
digitalWrite(led, !digitalRead(led));
}
}
void blinkLed500() {
if (millis() - t500 > 500) { //tick every 500mS
t500 = millis();
digitalWrite(led, !digitalRead(led));
}
}
The 3V relays have a very low resistance and I am not sure that the digital outputs are powerful enough. Personally I always switch relays via a transistor.

Countdown once event is triggered

I'm just getting started with my Arduino and I want to operate a relay for a certain amount of time once I press a contact switch, but still be able to stop the it again at any time by pressing the button again. I've got the press the button to start and stop working Ok, but can't get the timer bit to work at all. Below is what I've got working so far.
TIA.
void setup()
{
Serial.begin(9600);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
Serial.print("Pin 7 ");
Serial.println(digitalRead(7));
if (digitalRead(6) == LOW && digitalRead(7) == HIGH) {
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(250); // Wait for 250 millisecond(s)
}
if (digitalRead(6) == HIGH && digitalRead(7) == HIGH) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(250); // Wait for 250 millisecond(s)
}
}
bool buttonState = 0, buttonStateBefore = 0;
bool toggle = 0;
unsigned long timeNow = 0;
buttonState = digitalRead(buttonPin);
if(buttonState > buttonStateBefore) {
toggle = !toogle; // negates the toogle
timeNow = millis(); // milliseconds to the time you press the button
}
buttonStateBefore = buttonState;
if(toggle && millis() < timeNow + delayTime) {
// relay on
}
else {
// relay off
}
Explanation:
When you press the button, buttonState beign now one, is bigger than the buttonStateBefore. So the if-statement is true. The variable toggle is now true and the time is set to the amount of milliseconds after starting the arduino. After the first if the buttonStateBefore is set to be the buttonState, to not toggle the toggle variable every circle. The second if-statement checks if the toggle variable is true and if the time has not exceeded. If one of the two things is not the case, the relay is turned off.
I hope this is helpful.
P.s. You can shorten your code if you use a for-loop for the digitalWrite
for(int i = 3; i < 7; i++) {
digitalWrite(i, true);
}

How do I make the buzzer stay on (LDR Alarm)?

I have a program in Arduino that checks an LDR sensor. If it goes over the set values it will trigger an alarm. How do I set it so once triggered it stays on until say a button push is detected to disarm it?
Code:
const int ledPin = 8;
const int buzzerPin = 4;
const int ldrPin = A0;
void setup () {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus >= 30) {
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
} else {
tone(buzzerPin, 100);
digitalWrite(ledPin, HIGH);
delay(100);
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
delay(100);
Serial.println("----------- ALARM ACTIVATED -----------");
}
}
You should use a FLAG to fire the alarm instead of using threshold directly.
if (ldrStatus >= 30) {
AlarmFlag = true; //Set alarm
}
...
if (digitalRead(pushButton) == LOW){
AlarmFlag = false; //Turn off alarm
}
...
if (AlarmFlag == true){
Serial.println("ALARM ON");
...
}

How can I set a ceratin time interval for reading input in my Arduino game?

I'm making a game with my Arduino Uno.
4 leds on the breadboards are displaying a random binary number.
The player needs to press the button to put in the number that he sees
(for example, if the leds are 0011, push the button 3 times).
If he guessed right, he wins (another led blinks). If not, he loses (led turns on and stays on).
But I want the player to automatically lose if he hasn't pressed a button for two seconds.
But I'm really a beginner so bear with me. Below I'll post what I've got so far. But it's not exactly working. When I turn it on, without me pressing the button, it just goes straight to "you win" and the next round. What am I doing wrong?
int led1 = 9;
int led2 = 6;
int led3 = 5;
int led4 = 3;
int ledResult = 13; //will blink when you won, stay on when you lost
int buttonPin = 2;
int val = 0; // variable for reading the pin status
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
long interval = 2000;
long randomNumber;
void setup() {
Serial.begin(9600); //starts serial communication
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (ledResult, OUTPUT);
pinMode (buttonPin, INPUT);
randomSeed(analogRead(A0)); //the pin is unconnected so it'll return something random (0-1023)
}
void loop() {
randomNumber = random(1, 16);
Serial.println("Random Numbers sequence"); //just for self-check
Serial.println(randomNumber);
if (randomNumber >= 8)
{
digitalWrite (led1, HIGH);
randomNumber - 8;
}
else
{
digitalWrite (led1, LOW);
}
if (randomNumber >= 4)
{
digitalWrite (led2, HIGH);
randomNumber - 4;
}
else
{
digitalWrite (led3, LOW);
}
if (randomNumber >= 2)
{
digitalWrite (led4, HIGH);
randomNumber - 2;
}
else
{
digitalWrite (led1, LOW);
}
if (randomNumber = 1)
{
digitalWrite (led2, HIGH);
}
else
{
digitalWrite (led1, LOW);
}
unsigned long currentMillis = millis();
if (currentMillis > interval) {
Serial.println("You lost.");
digitalWrite(ledResult, HIGH);
}else{
//READ BUTTON STATE
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
if (buttonState == HIGH)
{
buttonPushCounter++;
Serial.println("Button push counter:");
Serial.println(buttonPushCounter);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter = randomNumber) {
Serial.println("You won!");
digitalWrite(ledResult, HIGH);
delay(700);
digitalWrite(ledResult, LOW);
delay(700);
}
else
{
Serial.println("You lost.");
digitalWrite(ledResult, HIGH);
}
}
}
Essentially you'll want to have a main loop that probes/polls for user input, and if there's no input from the user on probe, then check the current millis time against when you called it for start. This will give you the elapsed time since no input was detected. If the elapsed time is greater than your threshold, you break out of the loop and check a timeout flag to see if you proceed with the button validation code or branch to the "lose" code.
Without writing the whole thing, here's a C pseudo-code mock up of the idea (commented to explain):
void loop() {
while (is_running) { // main loop
unsigned long start = millis(); // get starting probe time
bool timeout = false;
while ((buttonState = probe_input()) == no_input_val) {
/* probe_input function returned no input,
so check for timeout. If the probe_input function
did return a 'value', then this loop won't iterate
and you can check the buttonState below */
if ((millis() - start) >= interval) {
/* millis is monotonic time, so calling it, then
subtracting the value from the start value will give
us elapsed time since user did not give input. */
timeout = true;
break;
}
}
if (!timeout) {
/* button state/level code */
} else {
// lose code
}
}
}
Again, this is just pseudo-code and won't compile as you'd need to implement the probe_input function to poll for input, but it's more to illustrate what you'd need to do to get you moving in the direction you're wanting.
I hope that can help.

How to write a state change (edge detection) using analogRead() on Arduino?

I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0;
int led = 13;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
myservo.attach(6);
myservo.write(0);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue > 800) {
digitalWrite(led, HIGH);
myservo.write(180);
delay(100);
}
else{
digitalWrite(led, LOW);
myservo.write(0);
delay(100);
}
}
EDIT: I'm pretty close with this edited code. I added a variable and an if statement. It turns the LED on and it stays on, but when pressed again it doesn't turn off. So close...
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(500);
ledState == 1;
}
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(500);
ledState == 0;
}
}
OK. I think I solved it! Take a look. It now turns the LED on when the button is pressed and stays on until the button is pressed again. This is how to create a switch using an analog input reading.
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(100);
ledState = 1;
Serial.println(ledState);
delay(500);
}
else {
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(100);
ledState = 0;
Serial.println(ledState);
delay(500);
}
}
}

Resources