//Structure Queue - v1
// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>
// include LCD library function
#include<LiquidCrystal.h>
// include Stepper Motor function
#include <Stepper.h>
const int stepsPerRevolution = 200;
//Define Stepper Motor Pins
Stepper myStepper(stepsPerRevolution, 6, 7, 8, 9);
//Define LCD pins
LiquidCrystal lcd (12,11,2,3,4,5); //RS, E, D4, D5, D6, D7
// Include semaphore support
#include "semphr.h"
// Define a struct
SemaphoreHandle_t xMutex; //Declare mutex handle
void setup()
{
Serial.begin(9600); //Enable serial communication library.
lcd.begin(16, 2); //Enable LCD library
myStepper.setSpeed(60); //Enable Stepper Motor library
pinMode(A0, INPUT); //Enable A0 Pin from analog input
xMutex = xSemaphoreCreateMutex(); //Create a MUTEX
xTaskCreate(TaskLcd,"Displaydata",128,NULL,1,NULL);
xTaskCreate(TaskFan,"Fan_ON",128,NULL,1,NULL);
xTaskCreate(TaskHeater,"Hearter_ON",128,NULL,1,NULL);
//Create Task to perform
vTaskStartScheduler(); //start the scheduler
}
void loop(){}
void TaskHeater(void *pvParameters) //task heater function definition
{
int temp = 0;
for (;;)
{temp = analogRead(A0); //read the analog input
if(temp>512)
{
xSemaphoreTake(xMutex, portMAX_DELAY); //take the mutex source
Serial.println("Heater ON"); //turn ON the Heater
myStepper.step(-stepsPerRevolution);
delay(500);
xSemaphoreGive(xMutex); //give back the mutex for other task
}
vTaskDelay(pdMS_TO_TICKS(1));
}
}
void TaskFan(void *pvParameters) //task fan function definition
{
int temp = 0;
for (;;)
{
temp = analogRead(A0);//read the analog input
if(temp<512)
{
xSemaphoreTake(xMutex, portMAX_DELAY);//take the mutex source
Serial.println("Fan ON"); //turn ON the Fan
myStepper.step(stepsPerRevolution);
delay(500);
xSemaphoreGive(xMutex);//give back the mutex for other task
}
vTaskDelay(pdMS_TO_TICKS(1));
}
}
void TaskLcd(void * pvParameters) //task LCD function to display LCD
{
float tmp = 0;
int temp = 0;
for (;;)
{
xSemaphoreTake(xMutex, portMAX_DELAY); //take the semaphore
temp = analogRead(A0); //read analog input
float tmp = 1 / (log(1 / (1023. / temp - 1)) / 3950 + 1.0 / 298.15) - 273.15;
Serial.print("Temperature : ");
Serial.println(tmp); //print the temperature in Serial monitor
lcd.setCursor(0, 0);
lcd.print("Temp = ");
lcd.setCursor(7, 0);
lcd.print(tmp);
lcd.print("'C");
delay(500); //print the temperature in LCD
xSemaphoreGive(xMutex);
vTaskDelay(pdMS_TO_TICKS(1));
}
}