Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Last active December 9, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fearthecowboy/4e8d9c00af64b89b2f1cbd33d5bae8cc to your computer and use it in GitHub Desktop.
Save fearthecowboy/4e8d9c00af64b89b2f1cbd33d5bae8cc to your computer and use it in GitHub Desktop.
Setup Arduino sketch
// Setup the sketch.
void setup() {
// we're going to spit some messages out to the serial port.
Serial.begin(19200);
// create the device instances
// blink on and off every two seconds.
bc = new Blinker(2000);
// dump a memory report every 5 seconds
fm = new FreeMemoryReport(5000);
// setup a switch on pin 10 of the arduino board.
sw1 = new Switch(10);
// a temperature sensing device
tc = new Thermocouple();
// 'when' is how we setup an event handler.
// in this programming model, app is implicitly defined when you
// declare the States and the individual states in the state machine
// are all basic events.
when(app->starting, {
// app is starting
Serial.println("The app starting");
// in this state, we'd like to trigger an event when our switch
// is closed:
when(sw1->is(closed), {
Serial.println("switch was closed");
// 'after' will schedule the following code to run after a given
// number of milliseconds has passed.
after(1000, {
Serial.println("was closed a second ago.");
switchTo(processing);
});
});
});
when(app->processing, {
Serial.println("ok, now we're in the processing stage");
// do some more sample code..
after(1000, {
Serial.println("after 1000 msec in processing");
after(2000, {
Serial.println("after 3000 msec or so in processing");
});
});
when( tc-> temperature.deceeds(200), {
// this event will fire once when the temperature is below 200C
// or if it went higher than 200C and then dropped down again.
// if the temperature goes below 200C,
// then we could turn on the heating element..
Serial.println('turning on the heating element');
// ie, heater->enable();
});
when( tc->temperature.exceeds(200), {
// when we hit 200C, we want to proceed to the final state
switchTo(cleanup);
});
});
// kick off the first state. (I may optimize this out entirely soon...
after(1, { switchTo(starting) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment