// ======================================================================= // Using threads and thread controller, blink LEDs at different rates // (Not true threads or multi-tasking but maybe good enough) // ----------------------------------------------------------------------- // See: github.com/ivanseidel/ArduinoThread // ======================================================================= #include <Thread.h> #include <ThreadController.h> // ------------------------------------------------------------------------ // ---- definitions // ------------------------------------------------------------------------ // ---- LED pins #define LED1 5 #define LED2 6 #define LED3 7 // ---- seriaL port baud rate // ---- Use: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, // ---- 28800,38400, 57600, 115200 #define BAUDRATE 19200 // ------------------------------------------------------------------------ // ---- create threads and controller // ------------------------------------------------------------------------ // ---- create threads Thread led1Thread = Thread(); Thread led2Thread = Thread(); Thread led3Thread = Thread(); // ---- create a thread controller ThreadController controller = ThreadController(); // ------------------------------------------------------------------------ // ---- thread callback functions // ------------------------------------------------------------------------ void led1ThreadCallback() { static bool ledon = false; if (ledon) { digitalWrite(LED1, LOW); // turn the LED off ledon = false; } else { digitalWrite(LED1, HIGH); // turn the LED on ledon = true; } } void led2ThreadCallback() { static bool ledon = false; if (ledon) { digitalWrite(LED2, LOW); // turn the LED off ledon = false; } else { digitalWrite(LED2, HIGH); // turn the LED on ledon = true; } } void led3ThreadCallback() { static bool ledon = false; if (ledon) { digitalWrite(LED3, LOW); // turn the LED off ledon = false; } else { digitalWrite(LED3, HIGH); // turn the LED on ledon = true; } } // ------------------------------------------------------------------------ // ---- setup // ------------------------------------------------------------------------ void setup() { // ---- set pin mode pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); // ---- set thread name // ---- Thread Name is disabled by default, to use less memory) // ---- Enable it by definint USE_THREAD_NAMES on 'Thread.h') // led1Thread.ThreadName = "LED 1 Thread'); // led2Thread.ThreadName = "LED 2 Thread'); // led3Thread.ThreadName = "LED 3 Thread'); // ---- set default thread enabled flags led1Thread.enabled = true; led2Thread.enabled = true; led3Thread.enabled = true; // ---- set thread interval led1Thread.setInterval(100); // interval 100ms (1/10 second) led2Thread.setInterval(500); // interval 500ms (1/2 second) led3Thread.setInterval(800); // interval 800ms (4/5 second) // ---- set thread callback functions led1Thread.onRun(led1ThreadCallback); led2Thread.onRun(led2ThreadCallback); led3Thread.onRun(led3ThreadCallback); // ---- add Threads to the controller // ---- (Notice the '&' sign before the thread, IF it's not // ---- instantiated as a pointer. (For pointer "x = new Thread()")) controller.add(&led1Thread); controller.add(&led2Thread); controller.add(&led3Thread); // --- initialize serial port Serial.begin(BAUDRATE); // ---- report the number of threads Serial.print(controller.size()); Serial.println(" threads"); Serial.flush(); } // ------------------------------------------------------------------------ // ---- loop forever // ------------------------------------------------------------------------ void loop() { controller.run(); }