Hi, i have a project to do in ARDUINO UNO for tomorrow and i just found out i have to use Tasks from FreeRTOS and i have no ideea how. Could someone please help me?
This is my code that works but is not using tasks:
#include <SoftwareSerial.h>
// Create a software serial object for communication over Bluetooth
SoftwareSerial bluetoothSerial(12, 13);
int carRed = 6;
int carGreen = 5;
// assign the pedestrian lights
int pedRed = 7;
int pedGreen = 4;
const int buttonPin = 2; // the pin that the pushbutton is attached to
int crossTime = 10000; // time allowed to cross for pedestrian
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
//digitalWrite(buttonPin, HIGH);
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH ) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// Call the function to change the lights
delay(5000);
changeLights();
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
void changeLights() {
digitalWrite(carGreen, LOW); // carGreen off
delay(4000); // wait 4 seconds
digitalWrite(carRed, HIGH); // carRed on
delay(3000); // wait till its safe
digitalWrite(pedRed, LOW); // pedRed off
digitalWrite(pedGreen, HIGH); // pedGreen on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<5; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
// turn pedRed on
digitalWrite(pedRed, HIGH);
delay(2000);
delay(2000);
digitalWrite(carRed, LOW); // carRed off
digitalWrite(carGreen, HIGH); //carGreen on
// record the time since last change of lights
}
And I need to make it do the same thing but using tasks, i started something here but i don’t know if it’s ok and i really don’t know how to create those tasks :
#include <Arduino_FreeRTOS.h>
#include <SoftwareSerial.h>
#include <semphr.h>
// Create a software serial object for communication over Bluetooth
SemaphoreHandle_t xBinarySemaphore;
SoftwareSerial BTSerial(12, 13);
int carRed = 6;
int carGreen = 5;
// assign the pedestrian lights
int pedRed = 7;
int pedGreen = 4;
const int buttonPin = 2; // the pin that the pushbutton is attached to
int crossTime = 10000; // time allowed to cross for pedestrian
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// Declarare task-uri
void TaskPussButon( void *pvParameters );
void TaskBluetooth( void *pvParameters );
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
//digitalWrite(buttonPin, HIGH);
// Se initializeaza comunicatia seriala cu 9600 biti pe secunda
xBinarySemaphore = xSemaphoreCreateBinary();
Serial.begin(9600);
BTserial.begin(9600);
xTaskCreate(
TaskPussButon
, "PussButton"
, 128
, NULL
, 1
, NULL );
xTaskCreate(
TaskBluetooth
, "Bluetooth"
, 128
, NULL
, 2
, NULL );
xSemaphoreGive(xBinarySemaphore);
vTaskStartScheduler();
}
void loop() {}
PLEASE HELP!!