/*#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
//defirir duas tarefas para piscar led e analogread
void TaskBlink (void *pvParameters);
void TaskAnalogRead (void *pvParameters);
void setup() {
Serial.begin(9600);
xTaskCreate(
TaskBlink, "Blink", 1000, NULL, 2, NULL);
}
void loop() {
// loop vazio
}
void TaskBlink(void *pvParameters) {
(void) pvParameters;
int PinLed = 13;
pinMode(PinLed, OUTPUT);
for (;;) {
digitalWrite(PinLed, HIGH);
vTaskDelay(1000 / portTICK_PERIOD_MS);
digitalWrite(PinLed, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
potenciometro sensor temp, sensor luz
//USAR MAP
*/
#include <Arduino.h>
#include <LiquidCrystal.h> //deu erro nessa biblioteca pq?
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <wifi.h>
#define BLYNK_TEMPLATE_ID "TMPL28q3ONdf0"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "KzT5xKl-ZvWzzPNnGDTdWQtFsFY_Inub"
// Definições de pinos
#define LED_PIN 13 //led piscando
#define SENSOR_PIN 34 // ADC do ESP32
#define LDR_PIN 35 // sensor luz LDR
#define RS 14
#define EN 27
#define D4 26
#define D5 25
#define D6 33
#define D7 32
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7); //configuração simples
// Variável compartilhada
volatile int valorSensor = 0; //p potenciometro
volatile int valorLDR = 0;//p sensor ldr luz
void TaskBlinkLED(void *pvParameters);
void TaskReadSensor(void *pvParameters);
void TaskReadLDR(void *pvParameters);
SemaphoreHandle_t xLCDSemaphore;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT); //led como saiída
lcd.begin(16, 2);
lcd.clear();
// Criação do semáforo (mutex) para o LCD
xLCDSemaphore = xSemaphoreCreateMutex();
if (xLCDSemaphore != NULL) {
xSemaphoreGive(xLCDSemaphore); // libera o recurso inicialmente
}
// P criar task
// T1 – Piscar LED (Core 0)
xTaskCreate(
TaskBlinkLED,"Blink LED",2048,NULL, 2,NULL);
// T2 – Ler Sensor (Core 1)
xTaskCreatePinnedToCore(
TaskReadSensor,"Read Sensor",2048,NULL,1,NULL,0);
// T3 – Ler LDR (core 1)
xTaskCreatePinnedToCore(
TaskReadLDR,"Read LDR",2048,NULL,1,NULL,1);
}
void loop() {
// loop vazio
}
// T1 – Piscar LED - igual o de sala
void TaskBlinkLED(void *pvParameters) {
(void) pvParameters;
for (;;) {
digitalWrite(LED_PIN, HIGH);
vTaskDelay(300 / portTICK_PERIOD_MS);
digitalWrite(LED_PIN, LOW);
vTaskDelay(300 / portTICK_PERIOD_MS);
Serial.print("task LCD core: ");
Serial.println(xPortGetCoreID()); //print qual core esta sendo usado naquele que nao especifiquei
}
}
// T2 – Ler Sensor
void TaskReadSensor(void *pvParameters) {
(void) pvParameters;
for (;;) {
valorSensor = analogRead(SENSOR_PIN);//le potenciometro e guarda na var valorsensor para print
if (xSemaphoreTake(xLCDSemaphore, (TickType_t) 5) == pdTRUE) {
lcd.setCursor(0, 0);
lcd.print("Sens1: ");
lcd.setCursor(7, 0); //contar casa certa p caractere começar linha 0
lcd.print(valorSensor); //print potenciometro
xSemaphoreGive(xLCDSemaphore);
}
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
// T3 – Ler LDR
void TaskReadLDR(void *pvParameters) {
(void) pvParameters;
for (;;) {
valorLDR = analogRead(LDR_PIN);//por ser pull down qnt mais luz menor o valor apresentado
vTaskDelay(150 / portTICK_PERIOD_MS); //aguardar 300ms
if (xSemaphoreTake(xLCDSemaphore, (TickType_t) 5) == pdTRUE) {
lcd.setCursor(0, 1);
lcd.print("Sens2: ");
lcd.setCursor(7, 1);
lcd.print(valorLDR); //print ldr
xSemaphoreGive(xLCDSemaphore);
}
vTaskDelay(500 / portTICK_PERIOD_MS); //aguardar 300ms
}
}