i want to create a timer for my program. the program should look like this:
LED2=on
Maintimer
timerA
timerB
Mainloop {
Maintimer=start counting
if Maintimer=5minutes turn LED2=off
{
timerA=start counting
if timerA=30seconds set Maintimer=0
reset timerB
}
{
timerB=start counting
if timerB=30seconds set Maintimer=0
reset timerA
}
}
void loop() // run over and over again
{
*****
Maintimer=start counting
if Maintimer=5minutes turn LED2=off
*****
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
if (tog ==0){
tog++;}
else{
while ( tog == 1) {
cnt++;
*****
timerA=start counting
if timerA=30seconds set Maintimer=0
reset timerB
*****
}
}
}
else
{
digitalWrite(ledPin, HIGH); // turn LED ON
tog =0;
*****
timerB=start counting
if timerB=30seconds set Maintimer=0
reset timerA
*****
}
}
i dont know if i explained it well. Please help! Thanks in advance!
You don't need any timers. What you need to do is keep track of what state you are in (waiting for cycle to start, switch pressed to trigger cycle start, etc.) and when you got into or out of that state.
On each pass through loop, you need to see if it is time to change to another state, based on what state you are in and how long you have been in that state.
For now, add some comments at the top of your code where you list (and number) the states that you can be in. Create a global variable to keep track of what state you are in and when you entered that state. In loop(), check what state you are in, using if statements, or a switch statement with cases. In each block or case, determine whether it is time to transition to another state or if the necessary external event has happened (the start switch was pressed). If it is time to change state, make whatever happen that needs to happen (turn a pin on or off, for instance). record the time, and change the state value.
Look at the blink without delay example for ideas.
I'd offer more concrete suggestions, but your requirements are far too vague.
Thank you for the immediate reply. The logic mentioned above will be used in my little experiment. I have an exhaust fan and a LED switch that will be toggled ON and OFF. By default, the exhaust fan is turned ON. Now, I need to create a timer that will start to count if the LED switch is being toggled on and off. But if the LED switch remained in ON state for 30 seconds or it remained in OFF state for thirty seconds, the timer will reset to 0 (zero). For the output, I have an exhaust fan that will be shut off if the timer reached 5 minutes. This is the program I am currently using to read the LED switch
void loop() // run over and over again
{
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
if (tog ==0){
tog++;}
else{
while ( tog == 1) {
cnt++;
}
}
}
else
{
digitalWrite(ledPin, HIGH); // turn LED ON
tog =0;
}
}
It's very hard to read. Put each { on a new line. Put each } on a new line. Use Tools + Auto Format to fix the crappy indenting. Add some comments.
An LED switch doesn't make much sense. An LED is an output device. A switch is an input device.
Now, I need to create a timer
No, you don't. You need to use a clock or stopwatch. Neither is a timer. The Arduino knows how much time has elapsed since it started. You can use that information like a stopwatch.
You might explain what your little experiment is. Exhaust fans and LEDs hardly seem to go together.
Counting like you are doing does not happen at known intervals. You can't rely on cnt reaching some value to mean that the fan should be turned off.
Counting like you are doing does not happen at known intervals. You can't rely on cnt reaching some value to mean that the fan should be turned off.
The cnt is for other purpose, i am using cnt to know how many times the switch is being turned on. It is simply a counter for the switch. I still don't know what to put in the program for the timer if ever i need it.
An LED switch doesn't make much sense. An LED is an output device. A switch is an input device.
I mean it is a switch for an LED. The switch can be toggled on and off to turn the LED on and off. There are three states for toggling the switch. No.1 toggling the switch on and off continuously(wheter toggling it too fast or too slow), No.2 the switch is in ON state and No.3 the switch is in OFF state. While in state No.1 my exhaust fan is in ON state, if the switch is being toggled continuously for 5 minutes the exhaust should turn to OFF state. In state No.2, if the switch is in ON state for 30 seconds the timer 5mins should reset to 0. Because the switch is ON for 30 seconds I can conclude that the switch is not toggled on and off continuously. State No.3 is just the same for state No.2 the only difference is that the switch is in OFF state for 30 seconds.
The Arduino knows how much time has elapsed since it started
How will my Arduino know that the switch is being toggled continuously for 5 minutes so that it can shut off the exhaust automatically?
Last night I made a video explaining state management and the Blink Without Delay example. This might help you understand. You need to let the loop run over and over as fast as possible and wait for certain time points or other conditions. Then you change to the appropriate state and let the loop run again as soon as possible. If you use delay, everybody else has to wait, so you can't respond to a button while you delay. Do yourself a favor and write down your three state and then make little arrows for every possible state change, and what causes it. That should make the necessary logic perfectly clear.
I suspect that you're trying to use a light sensor to tell when a fan is running, but I think that's a logical mistake - can anything other than the fan running cause the light to flicker? When the fan is running fast, will you actually see the flickering? Wouldn't it be better to just test if the fan is actually on? Your sensor is not direct enough.
The variable current_state holds the current state. Each state has a handler class like this one button-minder/project.ino at master · zapta/button-minder · GitHub with two methods, one to enter the state and one to handle the loop call when in this state, as well as private variables that the state needs, if at all.
One key attribute of this programming style is making the loop() method doing something quick and returning, without EVER calling delay() or similar 'blocking' functions, nor doing your own wait loops.
The typical blinking led arduino example with calls to delay(), puts beginners on a dead end path, so some programming style change is required when moving to more complex programs. As a learning exercise, try to implement a blinking led with the passive timer instead of delay().
write down your three state and then make little arrows for every possible state change, and what causes it.
What do you mean by these? Can you give me an example on how to do this so that I can clearly explain my point. Thanks
I mean get out a paper and draw a picture with circles and arrows and a paragraph on the back of each one to be used as evidence against us...
Oh wait I got lost... I'm seriously saying, map out your states on a piece of paper and describe what happens to change the states, by drawing an arrow between each one, and writing what causes the transition. I think when you do this, you will find that your states are not what you think they are. You are getting confused between the state of your hardware and your sensor reading logic. Do one thing at a time.
I mean get out a paper and draw a picture with circles and arrows and a paragraph on the back of each one to be used as evidence against us... smiley-wink
Oh wait I got lost... I'm seriously saying, map out your states on a piece of paper and describe what happens to change the states, by drawing an arrow between each one, and writing what causes the transition. I think when you do this, you will find that your states are not what you think they are. You are getting confused between the state of your hardware and your sensor reading logic. Do one thing at a time.
The setup goes like this, i have an LED, push button switch and a fan, all connected to the arduino. The switch acts as an input for the LED. What I want to do now is to create a program that will toggle the LED on and off manually using the push button switch and count the number of press. Here’s the code I used for this matter.
#include <SPI.h>
// initialize the library with the numbers of the interface pins
int ledPin = 13; // LED connected to digital pin 13
int inPin = 11; // choose the input pin (for a pushbutton switch)
int fan = 12; //Fan connected to digital pin 12
int val = 0; // variable for reading the pin status
int cnt = 0; // var for counting the presses
int tog = 0; //bit used to toggle and keep track of when the button is pressd or not
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as LED output
pinMode(fan, OUTPUT); // sets the digital pin as fan output
pinMode(inPin, INPUT); // declare pushbutton as input
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() // run over and over again
{
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
if (tog ==0){
tog++;
}
else{
while ( tog == 1) {
cnt++;
Serial.print("Number of press:");
Serial.print(cnt)
Serial.print("\r\n");
}
}
}
else
{
digitalWrite(ledPin, HIGH); // turn LED ON
tog =0;
}
}
In the beginning of the program the fan should be turned on. Now I need to modify this program so that the fan will be turned off in two cases. First case: the push button switch is being toggled on and off continuously for 5 minutes, if this case is satisfied the fan should be turned off. Second case: the button is pressed OR released for thirty seconds, meaning the switch is not being toggled on or off right? I want to reset the timer “5minutes” to zero if the switch is idled for 30 seconds.
First case: the push button switch is being toggled on and off continuously for 5 minutes,
This is still too vague. What constitutes being toggled on and off continuously? That is, what interval defines a break in "continuously"? Does pressing the switch once a second mean continuously? Does a 1.1 second interval define a break? Or, does pressing the switch once every two minutes mean continuously? Or, does pressing the switch once every 10 milliseconds mean continuously, and a 12 millisecond gap constitutes a discontinuity?
This is still too vague. What constitutes being toggled on and off continuously? That is, what interval defines a break in "continuously"? Does pressing the switch once a second mean continuously? Does a 1.1 second interval define a break? Or, does pressing the switch once every two minutes mean continuously? Or, does pressing the switch once every 10 milliseconds mean continuously, and a 12 millisecond gap constitutes a discontinuity?
Yes, those examples are continuous, toggling the switch whether fast or slow. what i need is that if the push button switch is changing its state for 5 minutes i want the fan to be turned off.
what about if as soon as the program starts there is a timer for 5 minutes and then it will only reset if the switch is idle for 30 seconds ( switch state = HIGH for 30 seconds or LOW for 30 seconds) with this kind of program, i think i can only say that the switch is toggled continuously if the interval between toggling the switch is less than 30 seconds. if the switch is idle for 30 seconds i think we can conclude that the switch is not being pressed.
if the switch is idle for 30 seconds i think we can conclude that the switch is not being pressed.
Or: If the interval between presses is greater than 30 seconds, you change to another state.
What we've been trying to do is to get you to list all the states, and what needs to happen to change from one state to another, and what triggers a change of state.
If 30 seconds goes by after a switch press, which you determine by subtracting then (the last time a switch was pressed) from now, you need to do some things (transition from one state to another) and record the new state (and, most likely, when the transition occurred).
if the switch is idle for 30 seconds i think we can conclude that the switch is not being pressed.
Or: If the interval between presses is greater than 30 seconds, you change to another state.
What we've been trying to do is to get you to list all the states, and what needs to happen to change from one state to another, and what triggers a change of state.
Yeah this. I don't actually follow how this machine is supposed to work, like what is it supposed to do? I think we're getting bogged down in the details, and we're missing something big. I think this machine only has two states - fan on and fan off. From what I understand we're trying to use a light sensor to determine if the fan is running or not. I don't understand what the button does.
Can you take us through an example "user story" maybe? That is, how would a person use this machine? List every interaction between the person and the machine, and how the machine reacts. I still don't understand where the button comes in.
I think this machine only has two states - fan on and fan off.
No. It has many states.
For instance:
Fan on, switch on.
Fan on, switch off.
Fan on, switch pressed recently
Fan on, switch not pressed recently.
Fan off.
There are others, I'm sure. But, like you, I'm confused about the total project. It seams like there is either a language issue involved, or that the project is a homework assignment that has no real application. An overview of the project, and an explanation for the weird requirements, is definitely needed.
For instance:
Fan on, switch on.
Fan on, switch off.
Fan on, switch pressed recently
Fan on, switch not pressed recently.
Fan off.
There are others, I'm sure. But, like you, I'm confused about the total project. It seams like there is either a language issue involved, or that the project is a homework assignment that has no real application. An overview of the project, and an explanation for the weird requirements, is definitely needed.
Yes, your right about that instances and there are a lot of states for this project. Yes, it has something to do with experiment or assignment that is why i cant give an example of real application but instead i can give you the "weird requirements".
States for the Fan status On or Off. timerOne = millis(); //to initialize the timer and start counting
State 1: timerOne < 5 minutes
Fan is ON
State 2: timerOne >= 5minutes
Fan is OFF
timerOne should reset to zero if the Switch is Pressed or Released for 30 seconds
For instance:
State 1: Switch is HIGH (button released) < 30 seconds //button is released < 30 seconds
timerOne should continue counting until it reach 5 minutes
Check the state of the Fan status. (2 States stated above) //check timerOne if it is >= or < 5 minutes
State 2: Switch is HIGH (button released) >= 30 seconds //button is released >= 30 seconds
timerOne should reset to zero, timerOne will only initialize again if Switch == LOW
Check the state of the Fan status. //we can say that the Fan should be ON because timerOne was reset to zero in this State.
State 3: Switch is LOW < 30 seconds //button is pressed < 30 seconds
timerOne should continue counting until it reach 5 minutes
Check the state of the Fan status. (2 States stated above)
State 4: Switch is LOW >= 30 seconds button is pressed >= 30 seconds
timerOne should reset to zero, timerOne will only initialize again if Switch == HIGH
No, not really. You still have some fundamentally incorrect assumptions.
timerOne = millis(); //to initialize the timer and start counting
That is NOT what that statement does. Think of millis() like the odometer in your car. At the start of a long trip, you do not reset the odometer to 0. You simply record the starting mileage. You would KNOW that the starting mileage represented an event, not a place, not a time.
millis() returns how long the Arduino has been running, just like looking at the odometer returns how many miles the car has been driven.
State 1: timerOne < 5 minutes
This is like saying "while the miles on the car is less than 500", do something, and then assuming that the 500 refers to the mileage traveled on this trip. It doesn't, unless its a brand new car, so any action you take based on the current mileage will be wrong. Taking an action based on the current mileage minus the starting mileage would be correct.
Even if you fix that issue, the trigger to turn the fan off, if I understand your weird requirements is NOT just that it has been running for 5 minutes, so the state description is incomplete (or wrong).