When running two threads, one for blinking led once every second, one for sweeping the servo 180 degrees. The servo just vibrates.
When I tested adding a serial printout in the run servo for loops for the pos variable it worked but a bit jerky. Any idea what could be the problem?
#include <Arduino.h>
#include <pt.h>
#include <Servo.h>
// put function declarations here:
static struct pt pt1;
static struct pt pt2;
Servo myservo;
int pos = 0;
static int protothreadBlinkLED(struct pt *pt) {
static unsigned long lastTimeBlink = 0;
PT_BEGIN(pt);
while(1) {
lastTimeBlink = millis();
PT_WAIT_UNTIL(pt, millis() - lastTimeBlink > 1000);
digitalWrite(LED_BUILTIN, HIGH);
lastTimeBlink = millis();
PT_WAIT_UNTIL(pt, millis() - lastTimeBlink > 1000);
digitalWrite(LED_BUILTIN, LOW);
}
PT_END(pt);
}
static int protoThreadRunServo(struct pt *pt) {
static unsigned long lastServoUpdate = 0;
PT_BEGIN(pt);
while(1) {
lastServoUpdate = millis();
for(pos = 0; pos < 180; pos += 1) {
PT_WAIT_UNTIL(pt, millis() - lastServoUpdate > 150);
myservo.write(pos);
}
for(pos = 180; pos >= 1; pos -= 1) {
PT_WAIT_UNTIL(pt, millis() - lastServoUpdate > 150);
myservo.write(pos);
}
}
PT_END(pt);
}
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
myservo.attach(9);
PT_INIT(&pt1);
PT_INIT(&pt2);
}
void loop() {
// put your main code here, to run repeatedly:
protothreadBlinkLED(&pt1);
protoThreadRunServo(&pt2);
}