From 09c59da3c295d0d30588602930ff9772c08d544e Mon Sep 17 00:00:00 2001 From: eaman Date: Tue, 10 Jan 2017 01:00:06 +0100 Subject: [PATCH] RTOS --- .gitignore | 1 + basic/pwm/pmw_5_coseno/pmw_5_coseno.ino | 35 ++++++ .../blink_10_freeRTOS/blink_10_freeRTOS.ino | 103 ++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 basic/pwm/pmw_5_coseno/pmw_5_coseno.ino create mode 100644 multitasking/blink_10_freeRTOS/blink_10_freeRTOS.ino diff --git a/.gitignore b/.gitignore index eaa5cc6..4dffdc7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ libraries/aero/ *Makefile *tags libraries/LedControl/ +libraries/FreeRTOS/ diff --git a/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino b/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino new file mode 100644 index 0000000..91f9214 --- /dev/null +++ b/basic/pwm/pmw_5_coseno/pmw_5_coseno.ino @@ -0,0 +1,35 @@ +/* + * PWM coseno + * + * PWM up & down utilizzando il coseno + * senza delay() + * + * Maggiore tempo con poca / massima luminosita'. + */ + +int value, value2 ; +int ledpin = 10; // light connected to digital pin 10 +int ledpin2 = 11; // light connected to digital pin 11 +long time=0; + +int periode = 2000; +int displace = 500; + +void setup() +{ + // nothing for setup +} + +void loop() +{ + time = millis(); + value = 128+127*cos(2*PI/periode*time); + value2 = 128+127*cos(2*PI/periode*(displace-time)); + analogWrite(ledpin, value); // sets the value (range from 0 to 255) + analogWrite(ledpin2, value2); // sets the value (range from 0 to 255) +} + +/* Esercizi: + 1.Fare un script analogo utilizzano la funzione map(). + 2.Fare un script analogo utilizzano millis e (float). + */ diff --git a/multitasking/blink_10_freeRTOS/blink_10_freeRTOS.ino b/multitasking/blink_10_freeRTOS/blink_10_freeRTOS.ino new file mode 100644 index 0000000..91a8f97 --- /dev/null +++ b/multitasking/blink_10_freeRTOS/blink_10_freeRTOS.ino @@ -0,0 +1,103 @@ +#include +/* E' necessario installare la libreria: Arduino_FreeRTOS +- https://github.com/feilipu/Arduino_FreeRTOS_Library +- https://www.hackster.io/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc +*/ +// define two tasks for Blink & AnalogRead +void TaskFirstBlink( void *pvParameters ); +void TaskSecondBlink( void *pvParameters ); + +// the setup function runs once when you press reset or power the board +void setup() { + + // initialize serial communication at 9600 bits per second: + Serial.begin(9600); + + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards. + } + + // Now set up two tasks to run independently. + xTaskCreate( + TaskFirstBlink + , (const portCHAR *)"First Blink" // A name just for humans + , 128 // This stack size can be checked & adjusted by reading the Stack Highwater + , NULL + , 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest. + , NULL ); + + xTaskCreate( + TaskSecondBlink + , (const portCHAR *) "Sec Blink" + , 128 // Stack size + , NULL + , 1 // Priority + , NULL ); + + // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started. +} + +void loop() +{ + // Empty. Things are done in Tasks. +} + +/*--------------------------------------------------*/ +/*---------------------- Tasks ---------------------*/ +/*--------------------------------------------------*/ + +void TaskFirstBlink(void *pvParameters) // This is a task. +{ + (void) pvParameters; + +/* + Blink + Turns on an LED on for one second, then off for one second, repeatedly. + + Most Arduinos have an on-board LED you can control. On the UNO, LEONARDO, MEGA, and ZERO + it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care + of use the correct LED pin whatever is the board used. + + The MICRO does not have a LED_BUILTIN available. For the MICRO board please substitute + the LED_BUILTIN definition with either LED_BUILTIN_RX or LED_BUILTIN_TX. + e.g. pinMode(LED_BUILTIN_RX, OUTPUT); etc. + + If you want to know what pin the on-board LED is connected to on your Arduino model, check + the Technical Specs of your board at https://www.arduino.cc/en/Main/Products + + This example code is in the public domain. + + modified 8 May 2014 + by Scott Fitzgerald + + modified 2 Sep 2016 + by Arturo Guadalupi +*/ + + // initialize digital LED_BUILTIN on pin 13 as an output. + pinMode(LED_BUILTIN, OUTPUT); +const int pausa = 500; + + for (;;) // A Task shall never return or exit. + { + digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) + vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second + digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW + vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second + } +} + +void TaskSecondBlink(void *pvParameters) // This is a task. +{ + (void) pvParameters; + +pinMode(3, OUTPUT); +const int pausa = 1000; + for (;;) // A Task shall never return or exit. + { + digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level) + vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second + digitalWrite(3, LOW); // turn the LED off by making the voltage LOW + vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second + } +} -- 2.39.2