Created
January 28, 2011 15:35
-
-
Save laclefyoshi/800403 to your computer and use it in GitHub Desktop.
Arduino protothread + Metro
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> | |
#include <Metro.h> | |
#define LedPin1 12 | |
#define LedPin2 11 | |
#define LedPin3 10 | |
#define LedPin4 9 | |
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); | |
} | |
Metro ledMetro3 = Metro(1000); | |
Metro ledMetro4 = Metro(2000); | |
void setup() { | |
pinMode(LedPin1, OUTPUT); | |
pinMode(LedPin2, OUTPUT); | |
PT_INIT(&pt1); | |
PT_INIT(&pt2); | |
pinMode(LedPin3, OUTPUT); | |
pinMode(LedPin4, OUTPUT); | |
} | |
void loop() { | |
protothread1(&pt1); | |
protothread2(&pt2); | |
if (ledMetro3.check() == 1) { | |
digitalWrite(LedPin3, !digitalRead(LedPin3)); | |
} | |
if (ledMetro4.check() == 1) { | |
digitalWrite(LedPin4, !digitalRead(LedPin4)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment