This document contains code for an Arduino 2560 real-time operating system (RTOS) that implements a simple task scheduler. It defines several timed tasks like Blinker tasks to blink LEDs at different rates and an Echoer task to echo serial input. The main loop initializes these tasks and the scheduler, and runs the scheduler indefinitely to switch between tasks.
This document contains code for an Arduino 2560 real-time operating system (RTOS) that implements a simple task scheduler. It defines several timed tasks like Blinker tasks to blink LEDs at different rates and an Echoer task to echo serial input. The main loop initializes these tasks and the scheduler, and runs the scheduler indefinitely to switch between tasks.
This document contains code for an Arduino 2560 real-time operating system (RTOS) that implements a simple task scheduler. It defines several timed tasks like Blinker tasks to blink LEDs at different rates and an Echoer task to echo serial input. The main loop initializes these tasks and the scheduler, and runs the scheduler indefinitely to switch between tasks.
//Lab40 Arduino 2560 RTOS A105a #include <Task.h> #include <TaskScheduler.h> const int NUMBER_OF_FIELDS = 3; // how many comma seperated fields we expect int values[NUMBER_OF_FIELDS]; // array holding values for all the fields // Timed task to blink a LED. class Blinker : public TimedTask{ public: // Create a new blinker for the specified pin and rate. Blinker(uint8_t _pin, uint32_t _rate); virtual void run(uint32_t now); private: uint8_t pin; // LED pin. uint32_t rate; // Blink rate. bool on; // Current state of the LED.}; Blinker::Blinker(uint8_t _pin, uint32_t _rate) : TimedTask(millis()), pin(_pin), rate(_rate), on(false){ pinMode(pin, OUTPUT); // Set pin for output.} void Blinker::run(uint32_t now){ // If the LED is on, turn it off and remember the state. if (on) { digitalWrite(pin, LOW); on = false; // If the LED is off, turn it on and remember the state. } else { digitalWrite(pin, HIGH); on = true; } // Run again in the required number of milliseconds. incRunTime(rate);} // Task to echo serial input. class Echoer : public Task {public: Echoer(); virtual void run(uint32_t now); virtual bool canRun(uint32_t now); }; Echoer::Echoer() : Task(){ Serial.begin(4800);}
bool Echoer::canRun(uint32_t now) { return Serial.available() > 0;} void Echoer::run(uint32_t now){ while (Serial.available() > 0) { int byte = Serial.read(); Serial.print(byte, BYTE); if (byte == '\r') { Serial.print('\n', BYTE); } } } // Timed task 1 class Blinker1 : public TimedTask {public: // Create a new blinker for the specified pin and rate. Blinker1( uint32_t _rate); virtual void run(uint32_t now); private: uint32_t rate; // Blink rate. }; Blinker1::Blinker1(uint32_t _rate) : TimedTask(millis()), rate(_rate) { Serial1.begin(4800); clearLCD(); } void Blinker1::run(uint32_t now){ selectLineOne(); Serial1.print("Blinker1 "); Serial1.print(millis()); incRunTime(rate); } // Timed task 2 class Blinker2 : public TimedTask {public: // Create a new blinker for the specified pin and rate. Blinker2( uint32_t _rate); virtual void run(uint32_t now); private: uint32_t rate; // Blink rate. }; Blinker2::Blinker2( uint32_t _rate) : TimedTask(millis()), rate(_rate) { Serial1.begin(4800); } void Blinker2::run(uint32_t now) {selectLineTwo(); Serial1.print("Blinker2 "); Serial1.print(millis()); incRunTime(rate);} void setup() { Serial.begin(4800);} // Main program. void loop(){ Serial.println("Se afiseaza ecoul transmiterii unui caracter"); // Create the tasks. Blinker blinker(13, 25); Echoer echoer; Blinker1 blinker1(10000); Blinker2 blinker2(10000);
// Initialise the task list and scheduler. Task *tasks[] = { &blinker1, &blinker, &blinker2, &echoer}; TaskScheduler sched(tasks, NUM_TASKS(tasks)); // Run the scheduler - never returns. sched.run();} //SerialLCD Functions void selectLineOne(){ //puts the cursor at line 0 char 0. Serial1.print(0xFE, BYTE); //command flag Serial1.print(128, BYTE); //position} void selectLineTwo(){ //puts the cursor at line 2 char 0. Serial1.print(0xFE, BYTE); //command flag Serial1.print(192, BYTE); //position} void selectLineThree(){ //puts the cursor at line 3 char 0. Serial1.print(0xFE, BYTE); //command flag Serial1.print(148, BYTE); //position} void selectLineFour(){ //puts the cursor at line 4 char 0. Serial1.print(0xFE, BYTE); //command flag Serial1.print(212, BYTE); //position} void goTo(int position) { //position = line 1: 0-19, line 2: 20-39, etc, 79+ defaults back to 0 if (position<20){ Serial1.print(0xFE, BYTE); //command flag Serial1.print((position+128), BYTE); //position } else if (position<40){ Serial1.print(0xFE, BYTE); //command flag Serial1.print((position+128+64-20), BYTE); //position } else if (position<60){ Serial1.print(0xFE, BYTE); //command flag Serial1.print((position+128+20-40), BYTE); //position } else if (position<80){ Serial1.print(0xFE, BYTE); //command flag Serial1.print((position+128+84-60), BYTE); //position } else { goTo(0); } } void clearLCD(){ Serial1.print(0xFE, BYTE); //command flag Serial1.print(0x01, BYTE); //clear command.} void backlightOn(){ //turns on the backlight Serial1.print(0x7C, BYTE); //command flag for backlight stuff Serial1.print(157, BYTE); //light level.} void backlightOff(){ //turns off the backlight Serial1.print(0x7C, BYTE); //command flag for backlight stuff Serial1.print(128, BYTE); //light level for off.} void backlight50(){ //sets the backlight at 50% brightness Serial1.print(0x7C, BYTE); //command flag for backlight stuff Serial1.print(143, BYTE); //light level for off.} void serCommand(){ //a general function to call the command flag for issuing all other commands Serial1.print(0xFE, BYTE);}
#include <Task.h> #include <TaskScheduler.h> // Timed task to blink a LED. class Blinker : public TimedTask {public: // Create a new blinker for the specified pin and rate. Blinker(uint8_t _pin, uint32_t _rate); virtual void run(uint32_t now); private: uint8_t pin; // LED pin. uint32_t rate; // Blink rate. bool on; // Current state of the LED.};
Blinker::Blinker(uint8_t _pin, uint32_t _rate) : TimedTask(millis()), pin(_pin), rate(_rate), on(false){ pinMode(pin, OUTPUT); // Set pin for output.} void Blinker::run(uint32_t now){ // If the LED is on, turn it off and remember the state. if (on) { digitalWrite(pin, LOW); on = false; // If the LED is off, turn it on and remember the state. } else { digitalWrite(pin, HIGH); on = true; } // Run again in the required number of milliseconds. incRunTime(rate);} // Task to echo serial input. class Echoer : public Task{ public: Echoer(); virtual void run(uint32_t now); virtual bool canRun(uint32_t now);}; Echoer::Echoer() : Task(){ Serial.begin(9600);} bool Echoer::canRun(uint32_t now) { return Serial.available() > 0;} void Echoer::run(uint32_t now){ while (Serial.available() > 0) { int byte = Serial.read(); Serial.print(byte, BYTE); if (byte == '\r') { Serial.print('\n', BYTE); } } } void setup(){} // Main program. void loop() { // Create the tasks. Blinker blinker(13, 25); Echoer echoer; // Initialise the task list and scheduler. Task *tasks[] = { &blinker, &echoer }; TaskScheduler sched(tasks, NUM_TASKS(tasks)); // Run the scheduler - never returns. sched.run();}