Arduino FreeRTOS code flip

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<Servo.h>

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
 }

You could start here.
Your task might look something like this:

void task()
{
    // setup all things here
    while(true)
    {
        // this is your loop
    }
}

thank you sir. but I don't have a lot of time If you know something about the subject, can you help? I don't have any priorities. Is it actually a logic used to assign freeRTOS task priorities?

Someone on the Gigs and Collaborations forum may be willing to do your work for you. You'll need to negotiate a mutually-agreeable fee for their services.

it could be a logical approach for me. I'm in a hurry about this. But I will wait a little longer if he can help me here.

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 <Arduino_FreeRTOS.h>
#include <Servo.h>

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/portTICK_PERIOD_MS);
    range = map(sensorReading, sensorMin, sensorMax, 0, 3);
    vTaskDelay(10/portTICK_PERIOD_MS);
     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/portTICK_PERIOD_MS);
  for(;;)
  {
   
    vTaskDelay(20/portTICK_PERIOD_MS);
  }
}

Do you need to use an RTOS at all ?

What are you trying to do ?

I would like to schedule the code with 2 pieces rtos and let the servo motor run in the second task of the buzzer.

darkdemon06:
I would like to schedule the code with 2 pieces rtos and let the servo motor run in the second task of the buzzer.

OK, but do you need to use an RTOS ?
You seem stuck on the idea of using an RTOS without considering alternatives

A quote from somewhere

to a man with a hammer everything looks like a nail

Please explain in words what the program should do and when

According to the information received from the flame sensor program runs the buzzer. Turns the servo motor to 180 degrees. The flame sensor must operate in the first task. Servo motor and buzzer should be changed according to the information in the first task. Thanks :slight_smile:

As far as I can see from the code in reply #5 then depending on the sensor reading then either the servo is moved to to 180 or 0 and either 50 or 255 is written to the speaker pin.

What else needs to happen ?

Yes sir. I want to perform servo and buzzer operations on task 2. But when I move the codes there, the buzzer keeps singing and I don't understand what the servo does.

darkdemon06:
Yes sir. I want to perform servo and buzzer operations on task 2. But when I move the codes there, the buzzer keeps singing and I don't understand what the servo does.

The requirements that I suggested in reply #10, which you seem to agree with, seem simple to achieve

start of loop()
  read the input
    if input value greater than high threshold
      write 50 to the speaker
      write 180 to the servo
    end if
    else
    if input value less than low threshold
      write 255 to the speaker
      write 0 to the servo
    end if   
end of loop()

What have I missed ?

#include <Arduino_FreeRTOS.h>
#include <Servo.h>

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/portTICK_PERIOD_MS);
   range = map(sensorReading, sensorMin, sensorMax, 0, 3);
   vTaskDelay(10/portTICK_PERIOD_MS);
    
 }
}

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


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);
   }


  
   vTaskDelay(20/portTICK_PERIOD_MS);
 }
}

this is the arrangement I want. But it doesn't work this way.

#include <Servo.h>

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);
}

void loop()
{
  int sensorReading = analogRead(A0);
  range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  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);
  }
}

The fixation on FreeRTOS has not yet been explained.

gfvalvo:
The fixation on FreeRTOS has not yet been explained.

Indeed not

Yes this method works. But I'm being asked to use tasks.

I'm being asked to use tasks.

Is this for the purpose of learning about using tasks in a generally unsuitable environment or does the person doing the asking not know that there are other more suitable ways of meeting the requirement ?

Homework?