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
}
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.
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);
}
}
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
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.
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()
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 ?