Created
January 28, 2011 15:30
-
-
Save laclefyoshi/800391 to your computer and use it in GitHub Desktop.
Arduino protothread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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