Hello everyone
I would appreciate your help in planning a code of timer. When you press a button (Button1), LED 13 is "on" for 45 minutes and when you press another button (Button2) during the 45 minutes, the LED turns off. (the trigger should be "HIGH")
Thanks.
Can you make this work?
...and then extend it to two buttons? One for on and another for off?
Thanks...but is no timer in this code. i need Button1 to start a 45 minuits of timer (Led13 is on for 45 min). and button2 is interrupt button that should stop the timer and shutdown Led13.
what is uninterrupted end of counting time?
yes...stop the timer and turn led13 off
void setup() {
// put your setup code here
}
void loop() {
while (button1 == notActive);
uint32_t Millis = millis();
digitalWrite(Led, light);
while (millis() - Millis < 45 * 60 * 1000) {
if (button2 == Active)break;
}
digitalWrite(Led, down);
}
its not work...too much errors. can you write a full code with button1 is D2 and button2 is D3 on nano arduino?
something like this ?
const int sPin1 = 8;
const int sPin2 = 9;
#define OFF LOW
#define ON HIGH
unsigned long msecPeriod;
unsigned long msec0;
void loop ()
{
unsigned long msec = millis ();
// check timer
if (msecPeriod && msec - msec0 >= msecPeriod) {
msecPeriod = 0; // disable timer
digitalWrite (LED_BUILTIN, OFF);
}
// check for start
if (LOW == digitalRead (sPin2)) {
msec0 = msec;
msecPeriod = 5000;
digitalWrite (LED_BUILTIN, ON);
}
// check to reset
if (LOW == digitalRead (sPin1)) {
msecPeriod = 0; // disable timer
digitalWrite (LED_BUILTIN, OFF);
}
}
void setup ()
{
Serial.begin (9600);
pinMode (sPin1, INPUT);
pinMode (sPin2, INPUT);
pinMode (LED_BUILTIN, OUTPUT);
digitalWrite (LED_BUILTIN, OFF);
}
why don't you give the button-pins self-explaining names?
Your code behaves strange.
I made a wokwi simulation
sometimes there is a reaction to button-presses sometimes not
Test yourself
works like described
button pins weren't configured with pull-up.
but it worked for me using a multi-function shield
Do you want to use hardware timer (like TCNT1) or software timer (like millisCounter) in your coding/sketch?
Its works like charm... Thanks for your time....You are the best!!!
I'm sorry, I thoght you wanted help with planning the software. I meant that if you could connect two actual buttons to actual pins on your actual device, and make them work on-off, like:
/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int secondButtonPin = 3; // 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
int secondButtonState = 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);
pinMode(secondButtonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
secondButtonState = digitalRead(secondButtonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// do nothing
}
if (secondButtonState == HIGH) {
// turn LED off:
digitalWrite(ledPin, LOW);
} else {
// do nothing
}
}
... then, once the hardware and buttons are working properly with your setup, it is easy to add in a timer by 1) storing a timestamp when you do the turning-on part, and 2) adding a clause for turning off the LED:
...
unsigned long timestamp;
...
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
timestamp = millis(); // <<< save the turn-on-time
}
...
// use the turn-on-time and millis() to turn it off
if(millis() - timestamp > 2700000) { // 45*60*1000
// turn LED off:
digitalWrite(ledPin, LOW);
}
All together, this gives:
/*
Button -- modified from https://docs.arduino.cc/built-in-examples/digital/Button
for
https://forum.arduino.cc/t/help-with-coding-a-timer/1211201
Turns on a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
Turns off the light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 3.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- pushbutton attached to pin 3 from +5V
- 10K resistor attached to pin 3 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int secondButtonPin = 3; // 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
int secondButtonState = 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);
pinMode(secondButtonPin, INPUT);
}
unsigned long timestamp=0;
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
secondButtonState = digitalRead(secondButtonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
timestamp = millis(); // <<< save the turn-on-time
} else {
// do nothing
}
if (secondButtonState == HIGH) {
// turn LED off:
digitalWrite(ledPin, LOW);
} else {
// do nothing
}
// use the turn-on-time and millis() to turn it off
if(millis() - timestamp > 2700000) { // 45*60*1000
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
If you plan and build the hardware, it's easy to adapt the software.
Your code is not using your ON/OFF so you can't switch from active-low to active-high with the obvious simple edit.
Nice design -- your code is designed around a timer, lighting the LED when the timer is activated, and using the buttons to start and kill the timer.
doesn't above use OFF?
Oops. I misunderstood. You are correct. I had thought it was for configuring the HIGH/LOW state of the buttons per the OP's
presumably this part.
but this could be recognized when the button is released in stead of when pressed
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.