I have an Arduino program that uses the Thread library to create two concurrent threads, where:
- Thread #1 blinks an LED every 500ms; and
- Thread #2 blinks another LED every 250ms
I wired things up, flashed the program to my Arduino Due, and it ran beautifully.
I am now trying to add a 3rd thread that sweeps a servo back and forth.
Here's my design:
And the schematic that Fritzing generated for me:
And an accompanying actual picture of the wire up:
If you can see it, I've rigged Pin 10 as an OUTPUT
, and attached the servo's signal line to Pin 9. Here's the code:
#include <Thread.h>
#include <ThreadController.h>
#include <Servo.h>
int SERVO_SIGNAL_PIN = 9;
int SERVO_PWR_PIN = 10;
int LED_1_PIN = 12;
int LED_2_PIN = 13;
Servo servo;
int pos = 0;
static bool LED_1_STATUS = false;
static bool LED_2_STATUS = false;
Thread blinkLED1Thread = Thread();
Thread blinkLED2Thread = Thread();
Thread servoThread = Thread();
ThreadController master = ThreadController();
void setup() {
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
pinMode(SERVO_PWR_PIN, OUTPUT);
servo.attach(SERVO_SIGNAL_PIN);
blinkLED1Thread.onRun(blinkLED1);
blinkLED1Thread.setInterval(250);
blinkLED2Thread.onRun(blinkLED2);
blinkLED2Thread.setInterval(500);
servoThread.onRun(runServo);
master.add(&blinkLED1Thread);
master.add(&blinkLED2Thread);
master.add(&servoThread);
}
void loop() {
master.run();
}
void runServo() {
// Give the servo a few seconds to reset to the starting position.
servo.write(0);
delay(5000);
while(true) {
for(pos = 0; pos < 180; pos += 1) {
servo.write(pos);
delay(15);
}
for(pos = 180; pos >=1; pos-=1) {
servo.write(pos);
delay(15);
}
}
}
void blinkLED1() {
LED_1_STATUS = !LED_1_STATUS;
digitalWrite(LED_1_PIN, LED_1_STATUS);
}
void blinkLED2() {
LED_2_STATUS = !LED_2_STATUS;
digitalWrite(LED_2_PIN, LED_2_STATUS);
}
When I flash this to the Due, two disappointing things happen:
- The LEDs, which previously blinked at the correct rates, now stay lit and do not blink at all; and
- The servo doesn't move
Now perhaps I have a bum servo, or perhaps I haven't wired it up correctly, but the most concerning thing to me is that the LEDs are no longer blinking. I have a feeling that once I get those blinking again, the servo will probably start sweeping as well.
Can anyone spot anything that jumps out at them as being wrong here, wiring and code alike?
delay()
. Do notdelay()
inside a thread!delay(...)
called in one "thread" would block/lock progress in other threads? Thanks again!