Skip to content

Instantly share code, notes, and snippets.

@vitiral
Last active January 15, 2016 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vitiral/10012257 to your computer and use it in GitHub Desktop.
Save vitiral/10012257 to your computer and use it in GitHub Desktop.
usertools ui_led_example4 explained -- part1
#include <ui.h> // include protothread library
#define LEDPIN 13 // LEDPIN is a constant
uint16_t sub_time = 0; // subtracts time from LED periods
void toggleLED() {
boolean ledstate = digitalRead(LEDPIN); // get LED state
ledstate ^= 1; // toggle LED state using xor
digitalWrite(LEDPIN, ledstate); // write inversed state back
}
/* This function toggles the LED after 'interval' ms passed */
PT_THREAD blinky_thread(pthread *pt) {
// Notice that this function doesn't require any static integers. Everything
// is stored inside of pt.
PT_BEGIN(pt);
while(1) {
//* Use PT_WAIT_MS instead of the timestamp, etc.
PT_WAIT_MS(pt, pt->get_int_input(0) - sub_time); // macro to do "wait for milliseconds"
toggleLED();
}
PT_END(pt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment