Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

Arduino FreeRTOS code flip

Posted by omermertkaya on January 1, 2019

Hello to everyone;

I have my arduino code written in my c language.I need to write FreeRTOS code that performs the same function. How should I make a conversion? Where can I find out?

~~~

include

const int sensorMin = 0; // sensor minimum const int sensorMax = 1024; // sensor maximum int speakerPin = 3; Servo sg90;

void setup() { // initialize serial communication @ 9600 baud: Serial.begin(9600); pinMode (speakerPin, OUTPUT); sg90.attach(7);

} void loop() { // read the sensor on analog A0: int sensorReading = analogRead(A0); // map the sensor range (four options): // ex: 'long int map(long int, long int, long int, long int, long int)' int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// range value: switch (range) { case 0: // A fire closer than 1.5 feet away. Serial.println("** Close Fire "); analogWrite (speakerPin, 50); sg90.write(180); break; case 1: // A fire between 1-3 feet away. Serial.println(" Distant Fire **"); analogWrite (speakerPin, 255); sg90.write(0); break; } delay(1); // delay between reads } ~~~


Arduino FreeRTOS code flip

Posted by omermertkaya on January 1, 2019

I've set task1. But I have to move the buzzer and servo control I have on task 1 to task 2. But when I carry the codes, the buzzer works continuously and doesn't move according to the data. ~~~

include
include

const int sensorMin = 0; // sensor minimum const int sensorMax = 1024; // sensor maximum int speakerPin = 3; Servo sg90; int range;

void setup() { Serial.begin(9600); pinMode (speakerPin, OUTPUT); sg90.attach(7);

xTaskCreate(Task1,"Sensor", 100, NULL, 1, NULL); xTaskCreate(Task2,"Servo Motor", 100, NULL, 0, NULL); }

void loop() { // put your main code here, to run repeatedly:

}

void Task1(void *pvParameters) { for(;;) { int sensorReading = analogRead(A0); vTaskDelay(2/portTICKPERIODMS); range = map(sensorReading, sensorMin, sensorMax, 0, 3); vTaskDelay(10/portTICKPERIODMS); if(range==0) { Serial.println("** Close Fire **"); analogWrite (speakerPin, 50); sg90.write(180); }

if(range==1)
{
  Serial.println("** Distant Fire **");
  analogWrite(speakerPin, 255);
  sg90.write(0);
}

} }

void Task2(void *pvParameters) { vTaskDelay(20/portTICKPERIODMS); for(;;) {

vTaskDelay(20/portTICK_PERIOD_MS);

} } ~~~


Arduino FreeRTOS code flip

Posted by rtel on January 1, 2019

I have no idea about Arduino, but from what I can see from your original post....

The code that is is setup() can be code that appears in main() before the scheduler is started.

The code that is in loop can be added to a task that uses vTaskDelayUntil() (https://www.freertos.org/vtaskdelayuntil.html ) to control the period of its execution - unless delay(1) is very short, in which just run the task at the idle priority so it doesn't starve out higher priority tasks and call vTaskDelay( 1 ) instead of vTaskDelayUntil().

In your second post you added a couple of extra delays around the call to map() - not sure why, but if you are waiting for the map() operation to complete, then it would be better to block on an event primitive, such as a direct to task notification, so the delay is exactly as long as the operation takes rather than choosing an arbitrary block time (https://www.freertos.org/RTOS-task-notifications.html ).


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS v10.2.1 is available for immediate download. MIT licensed, includes 64-bit RISC-V, NXP Cortex-M33 demo, Nuvoton Cortex-M23 demo & STM32H745 dual core (AMP) demo.

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Cadence Tensilica Cortes

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Mediatek

Renesas

RISC-V

SiFIve RISC-V

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner