Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created January 28, 2011 15:30
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 laclefyoshi/800391 to your computer and use it in GitHub Desktop.
Save laclefyoshi/800391 to your computer and use it in GitHub Desktop.
Arduino protothread
#include <pt.h>
#define LedPin1 12
#define LedPin2 11
static struct pt pt1, pt2;
static int protothread1(struct pt *pt) {
static unsigned long timestamp = 0;
PT_BEGIN(pt);
while(1) {
PT_WAIT_UNTIL(pt, millis() - timestamp > 1000);
timestamp = millis();
digitalWrite(LedPin1, !digitalRead(LedPin1));
}
PT_END(pt);
}
static int protothread2(struct pt *pt) {
static unsigned long timestamp = 0;
PT_BEGIN(pt);
while(1) {
PT_WAIT_UNTIL(pt, millis() - timestamp > 2000);
timestamp = millis();
digitalWrite(LedPin2, !digitalRead(LedPin2));
}
PT_END(pt);
}
void setup() {
pinMode(LedPin1, OUTPUT);
pinMode(LedPin2, OUTPUT);
PT_INIT(&pt1);
PT_INIT(&pt2);
}
void loop() {
protothread1(&pt1);
protothread2(&pt2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment