-
Notifications
You must be signed in to change notification settings - Fork 63
The led machine
Control a led via a digital pin. Control blink speed, pause duration and number of repeats. Also very useful for controlling other types of hardware asynchronously, like pulsing buzzers or relays.
- begin()
- blink()
- pause()
- fade()
- wait()
- repeat()
- onFinish()
- EVT_ON
- EVT_OFF
- EVT_BLINK
- EVT_START
- EVT_TOGGLE
- EVT_TOGGLE_BLINK
- trace()
#include <Automaton.h>
Atm_led led1, led2;
void setup() {
led1.begin( 4 ).blink( 40, 250 ); // Setup blinking
led2.begin( 5 ).blink( 40, 50 );
led1.trigger( led1.EVT_BLINK ); // Start blinking
led2.trigger( led2.EVT_BLINK );
}
void loop() {
automaton.run();
}
Attaches a digital I/O pin to the Atm_led machine. The pin will be placed in OUTPUT mode. Set activeLow to true if you need pull the pin low to activate your led.
This is probably the shortest Automaton sketch (that actually does anything) possible.
#include <Automaton.h>
Atm_led led;
void setup() {
led.begin( 4 ).trigger( led.EVT_BLINK );
}
void loop() {
}
If you try it you'll see that the led turns on, but it does not blink. For that to happen the led machine must be constantly cycled:
#include <Automaton.h>
Atm_led led;
void setup() {
led.begin( 4 ).trigger( led.EVT_BLINK );
}
void loop() {
led.cycle();
}
Please note that the Atm_led machine starts up in state IDLE. You can turn the led on by sending a EVT_ON message or start it blinking with a EVT_BLINK message. EVT_TOGGLE toggles the led on and off, EVT_TOGGLE_BLINK toggles blinking on and off. These are the available external events:
led1.trigger( led1.EVT_ON );
led1.trigger( led1.EVT_BLINK );
led1.trigger( led1.EVT_OFF );
led1.trigger( led1.EVT_TOGGLE );
led1.trigger( led1.EVT_TOGGLE_BLINK );
The begin() method places the machine in the following blink configuration:
blink( 500, 500, -1 ); // Blink forever at 1 Herz
Alternatively: Atm_fade & blink( uint32_t duration, uint32_t pause_duration, uint16_t repeat_count = ATM_COUNTER_OFF )
Sets the time that the led is fully ON during a cycle in milliseconds. The three argument version sets the time the led is fully ON, the time the led is fully OFF and the number of repeats.
void setup() {
led1.begin( 4 );
led1.blink( 40 );
led1.blink( 40, 100, 10 ); // 40ms on, 100ms off, repeat 10 times
...
}
Sets the time that the led is fully OFF during a cycle in milliseconds.
void setup() {
led1.begin( 4 );
led1.blink( 40 );
led1.pause( 100 );
...
}
This is a dummy method for interface compatibility with the Atm_fade machine. It does nothing here.
Inserts a waiting period before start() or on() becomes active.
#include <Automaton.h>
// When the button is pressed, wait for 500 ms and then blink 3 times
Atm_led led;
Atm_button button;
void setup() {
led.begin( 4 )
.wait( 500 )
.blink( 200, 200, 3 );
button.begin( 2 )
.onPress( led, led.EVT_START );
}
void loop() {
automaton.run();
}
Sets how many times the blink pattern should repeat. Default is ATM_COUNTER_OFF (-1) which means it will blink indefinitely. Use 1 to blink once, etc...
void setup() {
led1.begin( 4 ).blink( 40, 100 ).repeat( 1 ).trigger( led1.EVT_BLINK );
...
}
The example above gives off a single 40 millisecond pulse and then goes back to sleep (state IDLE).
This method is used to trigger another machine when the current machine's blinking sequence has finished. This can be used to create sequences of blink patterns, but you can also trigger different types of machines in this manner.
led1.begin( 4 ).blink( 500, 500, 3 ).onFinish( led2, led.EVT_BLINK );
led2.begin( 4 ).blink( 50, 50, 10 );
led1.trigger( led1.EVT_BLINK );
The example above will blink a led slowly 3 times and then blink the same led quickly 10 times.
Turns the led on.
led.on();
led.trigger( led.EVT_ON );
Turns the led off.
led.trigger( led.EVT_OFF );
Starts the led blinking.
led.begin( 5 ).blink( 200 );
led.trigger( led.EVT_BLINK );
Starts the led blinking. (same as EVT_BLINK)
Toggle the led on and off.
led.begin( 5 );
led.trigger( led.EVT_TOGGLE );
Toggle the blinking on and off.
led.begin( 5 ).blink( 100, 900 );
led.trigger( led.EVT_TOGGLE_BLINK );
To monitor the behavior of this machine you may connect a monitoring function with the Machine::trace() method.
Serial.begin( 9600 );
led.trace( Serial );