/*
Vamos a encender el ESP32 con un potenciómetro y
de una forma parecida al ESP32 PWM led aumentaremos y
disminuiremos la intensidad del led.
*/
// Pin de entrada del potenciometro
int pinPot=34;
// Pin de salida al led Potenciometro
int pinLedPote=5;
// Pin de salida al led del boton
int pinLedBoton=2;
// Declaramos el pin al que estará conectado el pulsador
int pinPulsador=4;
#define SIZE_QUEUE 8
#define BOTON_ID 0
#define POTE_ID 1
typedef struct {
byte deviceID;
int value;
} SENSOR;
QueueHandle_t queueSensor = xQueueCreate(SIZE_QUEUE, sizeof(SENSOR));
void functionBoton(void *ptParam)
{
pinMode(pinPulsador, INPUT);
SENSOR dataBoton;
int estadoBoton;
dataBoton.deviceID=BOTON_ID;
while(1)
{
estadoBoton=digitalRead(pinPulsador);
if (estadoBoton== HIGH)
{
dataBoton.value=HIGH;
}
else
{
dataBoton.value=LOW;
}
TickType_t timeOut = 2000;
if (xQueueSend(queueSensor, &dataBoton, timeOut) != pdPASS)
{
Serial.println("Button: Queue is full.");
}
// Delay the task
vTaskDelay(200);
}
}
void functionPotenciometro(void *ptParam)
{
//Características del PWM
const int frecuencia = 1000;
const int canal = 0;
const int resolucion = 10;
//Inicializamos las características del pwm
ledcSetup(canal, frecuencia, resolucion);
// Definimos que el pin 2 sacara el voltaje
ledcAttachPin(pinLedPote, OUTPUT);
SENSOR dataPote;
// Declaramos la intensidad del brillo
int brillo = 0;
dataPote.deviceID=POTE_ID;
while(1)
{
//Obtenemos la señal del potenciometro
brillo = analogRead(pinPot);
brillo = (brillo / 16.2);
dataPote.value=brillo;
TickType_t timeOut = 2000;
if (xQueueSend(queueSensor, &dataPote, timeOut) != pdPASS)
{
Serial.println("Potenciometer: Queue is full.");
}
// Delay the task
vTaskDelay(200 );
}
}
void functionLed(void *ptParam)
{
pinMode(pinLedBoton, OUTPUT);
SENSOR data;
while(1)
{
//TickType_t timeOut = portMAX_DELAY;
TickType_t timeOut = 2000;
if (xQueueReceive(queueSensor, &data, timeOut) == pdPASS)
{
switch (data.deviceID)
{
case BOTON_ID:
digitalWrite(pinLedBoton,data.value);
Serial.print("valor boton:");
Serial.println(data.value);
break;
case POTE_ID:
analogWrite(pinLedPote, data.value);
Serial.print("valor Pote:");
Serial.println(data.value);
break;
default:
Serial.println("LED: Unkown Device");
break;
}
}
else
{
Serial.println("LED: Message Queue is Empty");
}
vTaskDelay(100);
}
}
void setup()
{
Serial.begin(9600);
xTaskCreate(functionBoton, "Function Boton", 1024 * 8, NULL, 1, NULL);
xTaskCreate(functionLed, "Function Led", 1024 * 4, NULL, 1, NULL);
xTaskCreate(functionPotenciometro, "Function Potenciometro", 1024 * 4, NULL, 1, NULL);
}
void loop()
{
}