изменила форматирование. Добавила автовыбор ножек в зависимости от выбранной платы

This commit is contained in:
KLASSENTS 2025-03-27 13:55:05 +07:00
parent 78ef98a74d
commit f3df27f5fc
12 changed files with 261 additions and 268 deletions

View File

@ -14,38 +14,33 @@ void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.println("Hello");
while (!Serial) {
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
}
// Semaphores are useful to stop a Task proceeding, where it should be paused to wait,
// because it is sharing a resource, such as the Serial port.
// Semaphores should only be used whilst the scheduler is running, but we can set it up here.
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
{
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
if ( ( xSerialSemaphore ) != NULL )
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
}
// Now set up two Tasks to run independently.
xTaskCreate(
TaskDigitalRead
, "DigitalRead" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL //Parameters for the task
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL ); //Task Handle
TaskDigitalRead,
"DigitalRead", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL, // Parameters for the task
2, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL); // Task Handle
xTaskCreate(
TaskAnalogRead
, "AnalogRead" // A name just for humans
, 128 // Stack size
, NULL //Parameters for the task
, 1 // Priority
, NULL ); //Task Handle
TaskAnalogRead,
"AnalogRead", // A name just for humans
128, // Stack size
NULL, // Parameters for the task
1, // Priority
NULL); // Task Handle
// Now the Task scheduler, which takes over control of scheduling individual Tasks, is automatically started.
}
@ -53,7 +48,6 @@ void setup() {
void loop()
{
// Empty. Things are done in Tasks.
Serial.println("ping");
}
/*--------------------------------------------------*/
@ -64,13 +58,17 @@ void TaskDigitalRead( void *pvParameters __attribute__((unused)) ) // This is a
{
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
Reads a digital input on pin pushButton, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
uint8_t pushButton = 2;
#ifdef BTN_BUILTIN
uint8_t pushButton = BTN_BUILTIN;
#elif (defined(ARDUINO_ELSOMIK))
uint8_t pushButton = P0_0;
#else
uint8_t pushButton = 2;
#endif
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
@ -93,13 +91,12 @@ void TaskDigitalRead( void *pvParameters __attribute__((unused)) ) // This is a
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
vTaskDelay(1); // one tick delay (10ms) in between reads for stability
}
}
void TaskAnalogRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
{
for (;;)
{
// read the input on analog pin 0:
@ -118,6 +115,6 @@ void TaskAnalogRead( void *pvParameters __attribute__((unused)) ) // This is a
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
}
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
vTaskDelay(1); // one tick delay (10ms) in between reads for stability
}
}

View File

@ -12,6 +12,14 @@
// Define a Array
int pinReadArray[4]={0,0,0,0};
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
//Function Declaration
void TaskBlink(void *pvParameters);
void TaskAnalogReadPin0(void *pvParameters);
@ -30,40 +38,39 @@ void setup() {
* Create a queue.
* https://www.freertos.org/a00116.html
*/
arrayQueue=xQueueCreate(10, //Queue length
sizeof(int)); //Queue item size
arrayQueue=xQueueCreate(10, // Queue length
sizeof(int)); // Queue item size
if(arrayQueue!=NULL){
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial,// Task function
"PrintSerial",// Task name
128,// Stack size
xTaskCreate(TaskSerial, // Task function
"PrintSerial", // Task name
128, // Stack size
NULL,
2,// Priority
2, // Priority
NULL);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin0, // Task function
"AnalogRead1",// Task name
128,// Stack size
"AnalogRead1", // Task name
128, // Stack size
NULL,
1,// Priority
1, // Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin1,// Task function
"AnalogRead2",// Task name
128,// Stack size
xTaskCreate(TaskAnalogReadPin1, // Task function
"AnalogRead2", // Task name
128, // Stack size
NULL,
1,// Priority
1, // Priority
NULL);
xTaskCreate(TaskBlink,// Task function
"Blink", // Task name
128,// Stack size
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0,// Priority
0, // Priority
NULL);
}
@ -86,7 +93,7 @@ void TaskAnalogReadPin0(void *pvParameters){
* https://www.freertos.org/a00117.html
*/
xQueueSend(arrayQueue,&pinReadArray,portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
@ -106,7 +113,7 @@ void TaskAnalogReadPin1(void *pvParameters){
* https://www.freertos.org/a00117.html
*/
xQueueSend(arrayQueue,&pinReadArray,portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
@ -122,11 +129,6 @@ void TaskSerial(void *pvParameters){
// Init Arduino serial
Serial.begin(9600);
// Wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
while (!Serial) {
vTaskDelay(1);
}
for (;;){
if(xQueueReceive(arrayQueue,&pinReadArray,portMAX_DELAY) == pdPASS ){
@ -149,12 +151,12 @@ void TaskSerial(void *pvParameters){
*/
void TaskBlink(void *pvParameters){
(void) pvParameters;
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,LOW);
pinMode(blink_pin,OUTPUT);
digitalWrite(blink_pin,LOW);
for (;;){
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(blink_pin,HIGH);
vTaskDelay(250/portTICK_PERIOD_MS);
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(blink_pin,LOW);
vTaskDelay(250/portTICK_PERIOD_MS);
}
}

View File

@ -1,24 +0,0 @@
/*
* Example of FreeRTOS configASSERT macro
* https://www.freertos.org/a00110.html#configASSERT
*/
#include <Arduino_FreeRTOS.h>
const bool valueToAssert = true;
// The setup function runs once when you press reset or power the board
void setup() {
// Assert value is true, execution doesn't stop.
configASSERT(valueToAssert == true);
// Assert value is false, FreeRTOS execution stops and start to blink main led two times with 4 second cycle.
configASSERT(valueToAssert == false);
}
void loop()
{
// Empty. Things are done in Tasks.
}

View File

@ -4,32 +4,36 @@
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
// 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(
TaskBlink
, "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 );
TaskBlink,
"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(
TaskAnalogRead
, "AnalogRead"
, 128 // Stack size
, NULL
, 1 // Priority
, NULL );
TaskAnalogRead,
"AnalogRead",
128, // Stack size
NULL,
1, // Priority
NULL);
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
@ -51,35 +55,19 @@ void TaskBlink(void *pvParameters) // This is a task.
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
Most boards have an on-board LED you can control. You can use it via LED_BUILTIN macro.
If there is no led on board, you can set different pin to blink_pin variable.
*/
// initialize digital LED_BUILTIN on pin 13 as an output.
pinMode(LED_BUILTIN, OUTPUT);
// initialize blink_pin as an output.
pinMode(blink_pin, OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(blink_pin, HIGH); // turn the pin on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(blink_pin, LOW); // turn the pin off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}
@ -91,7 +79,7 @@ void TaskAnalogRead(void *pvParameters) // This is a task.
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +3V3 and ground.
This example code is in the public domain.
*/

View File

@ -15,45 +15,51 @@
*/
QueueHandle_t integerQueue;
void setup() {
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
integerQueue = xQueueCreate(10, // Queue length
integerQueue = xQueueCreate(10, // Queue length
sizeof(int) // Queue item size
);
if (integerQueue != NULL) {
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"Serial", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
xTaskCreate(TaskSerial, // Task function
"Serial", // 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.
2, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogRead, // Task function
"AnalogRead", // Task name
128, // Stack size
"AnalogRead", // Task name
128, // Stack size
NULL,
1, // Priority
1, // Priority
NULL);
}
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
0, // Priority
NULL );
}
void loop() {}
@ -79,7 +85,7 @@ void TaskAnalogRead(void *pvParameters)
*/
xQueueSend(integerQueue, &sensorValue, portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
@ -94,11 +100,6 @@ void TaskSerial(void * pvParameters) {
// Init Arduino serial
Serial.begin(9600);
// Wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
while (!Serial) {
vTaskDelay(1);
}
int valueFromQueue = 0;
for (;;)
@ -122,13 +123,13 @@ void TaskBlink(void *pvParameters)
{
(void) pvParameters;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(blink_pin, OUTPUT);
for (;;)
{
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(blink_pin, HIGH);
vTaskDelay( 250 / portTICK_PERIOD_MS );
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(blink_pin, LOW);
vTaskDelay( 250 / portTICK_PERIOD_MS );
}
}

View File

@ -16,14 +16,31 @@
*/
SemaphoreHandle_t interruptSemaphore;
void setup() {
// set interrupt pin
#ifdef BTN_BUILTIN
uint8_t int_pin = BTN_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t int_pin = P0_8;
#else
uint8_t int_pin = 2;
#endif
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 3;
#endif
void setup() {
// Create task for Arduino led
xTaskCreate(TaskLed, // Task function
"Led", // Task name
128, // Stack size
xTaskCreate(TaskLed, // Task function
"Led", // Task name
128, // Stack size
NULL,
0, // Priority
0, // Priority
NULL );
/**
@ -33,10 +50,8 @@ void setup() {
interruptSemaphore = xSemaphoreCreateBinary();
if (interruptSemaphore != NULL) {
// Attach interrupt for Arduino digital pin
attachInterrupt(digitalPinToInterrupt(BTN_BUILTIN), interruptHandler, RISING);
}
attachInterrupt(digitalPinToInterrupt(int_pin), interruptHandler, RISING);
}
}
void loop() {}
@ -59,7 +74,7 @@ void TaskLed(void *pvParameters)
{
(void) pvParameters;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(blink_pin, OUTPUT);
for (;;) {
@ -68,7 +83,7 @@ void TaskLed(void *pvParameters)
* https://www.freertos.org/a00122.html
*/
if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
digitalWrite(blink_pin, !digitalRead(blink_pin));
}
vTaskDelay(10);
}

View File

@ -21,7 +21,6 @@ int task1Delay = 1250;
int task2Delay = 1000;
void setup() {
Serial.begin(9600);
/**
@ -36,15 +35,14 @@ void setup() {
/**
Create tasks
*/
xTaskCreate(TaskMutex, // Task function
"Task1", // Task name for humans
xTaskCreate(TaskMutex, // Task function
"Task1", // Task name for humans
128,
&task1Delay, // Task parameter
1, // Task priority
&task1Delay, // Task parameter
1, // Task priority
NULL);
xTaskCreate(TaskMutex, "Task2", 128, &task2Delay, 1, NULL);
}
void loop() {}

View File

@ -11,15 +11,23 @@
*/
TaskHandle_t taskNotificationHandler;
void setup() {
// set interrupt pin
#ifdef BTN_BUILTIN
uint8_t int_pin = BTN_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t int_pin = P0_8;
#else
uint8_t int_pin = 2;
#endif
void setup() {
// Create task for FreeRTOS notification
xTaskCreate(TaskNotification, // Task function
"Notification", // Task name
128, // Stack size
xTaskCreate(TaskNotification, // Task function
"Notification", // Task name
128, // Stack size
NULL,
3, // Priority
&taskNotificationHandler ); // TaskHandle
3, // Priority
&taskNotificationHandler); // TaskHandle
}
void loop() {
@ -33,18 +41,15 @@ void TaskNotification(void *pvParameters)
{
(void) pvParameters;
int digitalPin = BTN_BUILTIN;
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(digitalPin), digitalPinInterruptHandler, RISING);
attachInterrupt(digitalPinToInterrupt(int_pin), digitalPinInterruptHandler, RISING);
for (;;) {
if (ulTaskNotifyTake(pdTRUE, portMAX_DELAY)) {
Serial.println("Notification received");
}
}
}
@ -53,5 +58,4 @@ void digitalPinInterruptHandler() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(taskNotificationHandler, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

View File

@ -15,6 +15,15 @@ struct Arduino{
int ReadValue[2];
};
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
//Function Declaration
void Blink(void *pvParameters);
void POT(void *pvParameters);
@ -32,35 +41,33 @@ void setup() {
* Create a queue.
* https://www.freertos.org/a00116.html
*/
structArrayQueue=xQueueCreate(10, //Queue length
sizeof(struct Arduino)); //Queue item size
if(structArrayQueue!=NULL){
structArrayQueue=xQueueCreate(10, // Queue length
sizeof(struct Arduino)); // Queue item size
if(structArrayQueue!=NULL){
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
NULL);
xTaskCreate(TaskBlink, // Task function
"Blink",// Task name
128,// Stack size
NULL,
0,// Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(POT,// Task function
"AnalogRead",// Task name
128, // Stack size
NULL,
2,// Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(POT, // Task function
"AnalogRead", // Task name
128, // Stack size
NULL,
2, // Priority
NULL);
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial,// Task function
"PrintSerial",// A name just for humans
128,// This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
1, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
}
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"PrintSerial",// A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
1, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
}
}
void loop() {}
@ -72,15 +79,13 @@ void loop() {}
void TaskBlink(void *pvParameters){
(void) pvParameters;
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,LOW);
pinMode(blink_pin,OUTPUT);
for(;;)
{
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(blink_pin,HIGH);
vTaskDelay(250/portTICK_PERIOD_MS);
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(blink_pin,LOW);
vTaskDelay(250/portTICK_PERIOD_MS);
}
}
@ -109,7 +114,7 @@ void POT(void *pvParameters){
*/
xQueueSend(structArrayQueue,&currentVariable,portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
@ -123,14 +128,8 @@ void TaskSerial(void *pvParameters){
// Init Arduino serial
Serial.begin(9600);
// Wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
while (!Serial) {
vTaskDelay(1);
}
for (;;){
struct Arduino currentVariable;
/**
@ -138,7 +137,7 @@ void TaskSerial(void *pvParameters){
* https://www.freertos.org/a00118.html
*/
if(xQueueReceive(structArrayQueue,&currentVariable,portMAX_DELAY) == pdPASS ){
for(int i=0;i<2;i++){
for(int i=0; i<2; i++){
Serial.print("PIN:");
Serial.println(currentVariable.pin[i]);
Serial.print("value:");

View File

@ -21,43 +21,51 @@ struct pinRead {
*/
QueueHandle_t structQueue;
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
void setup() {
/**
* Create a queue.
* https://www.freertos.org/a00116.html
*/
structQueue = xQueueCreate(10, // Queue length
sizeof(struct pinRead) // Queue item size
);
structQueue = xQueueCreate(10, // Queue length
sizeof(struct pinRead) // Queue item size
);
if (structQueue != NULL) {
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"Serial", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
"Serial", // 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.
2, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin0, // Task function
"AnalogReadPin0", // Task name
128, // Stack size
"AnalogReadPin0", // Task name
128, // Stack size
NULL,
1, // Priority
1, // Priority
NULL);
// Create other task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogReadPin1, // Task function
"AnalogReadPin1", // Task name
128, // Stack size
"AnalogReadPin1", // Task name
128, // Stack size
NULL,
1, // Priority
1, // Priority
NULL);
}
@ -95,7 +103,7 @@ void TaskAnalogReadPin0(void *pvParameters)
*/
xQueueSend(structQueue, &currentPinRead, portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
@ -123,7 +131,7 @@ void TaskAnalogReadPin1(void *pvParameters)
*/
xQueueSend(structQueue, &currentPinRead, portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
// One tick delay (10ms) in between reads for stability
vTaskDelay(1);
}
}
@ -137,15 +145,9 @@ void TaskSerial(void * pvParameters) {
// Init Arduino serial
Serial.begin(9600);
// Wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
while (!Serial) {
vTaskDelay(1);
}
for (;;)
{
struct pinRead currentPinRead;
/**
@ -169,13 +171,13 @@ void TaskBlink(void *pvParameters)
{
(void) pvParameters;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(blink_pin, OUTPUT);
for (;;)
{
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(blink_pin, HIGH);
vTaskDelay( 250 / portTICK_PERIOD_MS );
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(blink_pin, LOW);
vTaskDelay( 250 / portTICK_PERIOD_MS );
}
}

View File

@ -8,31 +8,36 @@ TaskHandle_t TaskSerial_Handler;
void TaskBlink( void *pvParameters );
void TaskSerial(void* pvParameters);
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
// 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(
TaskBlink
, "Blink" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL //Parameters passed to the task function
, 2 // Priority, with 2 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, &TaskBlink_Handler );//Task handle
TaskBlink,
"Blink", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL, // Parameters passed to the task function
2, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
&TaskBlink_Handler); // Task handle
xTaskCreate(
TaskSerial
, "Serial"
, 128 // Stack size
, NULL //Parameters passed to the task function
, 1 // Priority
, &TaskSerial_Handler ); //Task handle
TaskSerial,
"Serial",
128, // Stack size
NULL, // Parameters passed to the task function
1, // Priority
&TaskSerial_Handler); // Task handle
}
@ -73,13 +78,12 @@ void TaskSerial(void* pvParameters){
void TaskBlink(void *pvParameters) // This is a task.
{
(void) pvParameters;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(blink_pin, OUTPUT);
for (;;) // A Task shall never return or exit.
{
//Serial.println(11);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
digitalWrite(blink_pin, HIGH); // turn the pin on (HIGH is the voltage level)
vTaskDelay(1000 / portTICK_PERIOD_MS); // wait for one second
digitalWrite(blink_pin, LOW); // turn the pin off by making the voltage LOW
vTaskDelay(1000 / portTICK_PERIOD_MS); // wait for one second
}
}

View File

@ -16,17 +16,25 @@ TaskHandle_t taskDeletedHandle;
TaskHandle_t taskBlockedHandle;
void setup() {
// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif
void setup() {
/**
* Task creation
*/
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
xTaskCreate(TaskBlink, // Task function
"Blink", // Task name
128, // Stack size
NULL,
0, // Priority
&taskBlinkHandle); // Task handler
0, // Priority
&taskBlinkHandle); // Task handler
xTaskCreate(TaskSerial,
"Serial",
@ -48,7 +56,6 @@ void setup() {
NULL,
1,
&taskBlockedHandle);
}
void loop() {}
@ -136,13 +143,13 @@ void TaskBlink(void *pvParameters)
{
(void) pvParameters;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(blink_pin, OUTPUT);
for (;;)
{
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(blink_pin, HIGH);
vTaskDelay( 250 / portTICK_PERIOD_MS );
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(blink_pin, LOW);
vTaskDelay( 250 / portTICK_PERIOD_MS );
}
}