Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created January 28, 2011 15:35
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/800403 to your computer and use it in GitHub Desktop.
Save laclefyoshi/800403 to your computer and use it in GitHub Desktop.
Arduino protothread + Metro
#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