Hi
I want to read a pushbutton (or 2 or....) and do something when one is pushed.
If none of them have been pushed on in 5seconds, the flow should go somewhere else.
How is this best done?
Thanxks
PS the C command sleep(), does it works also for AVR?
newbee timing question
-
- Valued Contributor
- Posts: 548
- Joined: Tue Jun 26, 2007 11:23 am
- Has thanked: 6 times
- Been thanked: 44 times
Re: newbee timing question
You can test the switches repeatedly from within a 'While' loop. The tome taken to test the switches will be very short, but difficult to predict.
Including a short delay within the loop (10ms) will make the loop time much more predictable, without missing any of the switch operations.
An integer variable, set to zero before starting the loop, and incremented on each cycle of the loop, can be used to count the number of 10ms cycles (500 loops = 5seconds).
If the loop counter variable is 'count' and the switch detection variable is 'switches' (0 = no switches pressed), the condition for the 'While' loop should be (count < 500) AND (switches = 0).
A timer interrupt is another (slightly more advanced) option. This uses one of the hardware timer peripherals to provide the timing function. Several interrupts would need to be counted to produce a 5 second delay (depending on the configuration of the timer in the interrupt properties). The main program would need to test the interrupt count, but would not require any delays in the switch testing loop.
The AVR devices support several 'sleep' related commands:
sleep_cpu(); - puts the chip in sleep mode.
sleep_enable(); and sleep_disable(); - turn the sleep option on and off respectively.
sleep mode(); - combines the sleep_enable(); sleep_cpu(); and sleep_disable(); operations in a single command.
set_sleep_mode(mode); sets the wake condition for sleep mode - refer to the individual device data sheets for the modes available.
Use of these C commands requires the supplied sleep.h file to be included in Flowcode by adding the following line to the top panel of the 'Edit -> Supplementary code' menu:
#include <avr/sleep.h>
Including a short delay within the loop (10ms) will make the loop time much more predictable, without missing any of the switch operations.
An integer variable, set to zero before starting the loop, and incremented on each cycle of the loop, can be used to count the number of 10ms cycles (500 loops = 5seconds).
If the loop counter variable is 'count' and the switch detection variable is 'switches' (0 = no switches pressed), the condition for the 'While' loop should be (count < 500) AND (switches = 0).
A timer interrupt is another (slightly more advanced) option. This uses one of the hardware timer peripherals to provide the timing function. Several interrupts would need to be counted to produce a 5 second delay (depending on the configuration of the timer in the interrupt properties). The main program would need to test the interrupt count, but would not require any delays in the switch testing loop.
The AVR devices support several 'sleep' related commands:
sleep_cpu(); - puts the chip in sleep mode.
sleep_enable(); and sleep_disable(); - turn the sleep option on and off respectively.
sleep mode(); - combines the sleep_enable(); sleep_cpu(); and sleep_disable(); operations in a single command.
set_sleep_mode(mode); sets the wake condition for sleep mode - refer to the individual device data sheets for the modes available.
Use of these C commands requires the supplied sleep.h file to be included in Flowcode by adding the following line to the top panel of the 'Edit -> Supplementary code' menu:
#include <avr/sleep.h>