/* Basic Multi Threading Arduino Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
// Please read file README.md in the folder containing this example.
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
SemaphoreHandle_t readButton_s;
SemaphoreHandle_t led_low;
SemaphoreHandle_t led_high;
SemaphoreHandle_t blink_s;
SemaphoreHandle_t display_s;
/* Global Variables */
bool buttonState = false;
bool whell_detect = false;
/* Auxiliary Variables */
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
const int led = 33; // GPIO Number
const int button = 32;
/* TASK 1 */
void readButton(void *pvParameters)
{
while (1)
{
xSemaphoreTake(readButton_s, portMAX_DELAY);
buttonState = digitalRead(button);
if (buttonState == true)
{
whell_detect = true;
xSemaphoreGive(led_high);
}
if (buttonState == false)
{
whell_detect = false;
xSemaphoreGive(led_low);
}
xSemaphoreGive(blink_s);
vTaskDelay(50/portTICK_PERIOD_MS);
}
}
/* TASK 2 */
void blinking(void *pvParameters)
{
while (1)
{
xSemaphoreTake(blink_s, portMAX_DELAY);
/* See if we can obtain the semaphore. If the semaphore is not
available wait 10 ticks to see if it becomes free. */
if (xSemaphoreTake(led_high, ( TickType_t ) 10 ) == pdTRUE)
{
digitalWrite(led, HIGH);
}
if (xSemaphoreTake(led_low, ( TickType_t ) 10 ) == pdTRUE)
{
digitalWrite(led, LOW);
}
xSemaphoreGive(display_s);
vTaskDelay(50/portTICK_PERIOD_MS);
}
}
/* TASK 3*/
void display(void *pvParameters)
{
while (1)
{
xSemaphoreGive(readButton_s);
xSemaphoreTake(display_s, portMAX_DELAY);
if (whell_detect == true)
{
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Ajuste manual");
LCD.setCursor(0, 1);
LCD.print("o automatico");
}
if (whell_detect == false)
{
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Llanta no");
LCD.setCursor(0, 1);
LCD.print("detectada");
}
vTaskDelay(50/portTICK_PERIOD_MS);
}
}
void setup()
{
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
LCD.init();
LCD.backlight();
LCD.clear();
/* Mensaje inicial */
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Colocar llanta en");
LCD.setCursor(0, 1);
LCD.print("la base");
while (!Serial) {
delay(100);
}
// Create the semaphore
readButton_s = xSemaphoreCreateBinary();
blink_s = xSemaphoreCreateBinary();
display_s = xSemaphoreCreateBinary();
led_high = xSemaphoreCreateBinary();
led_low = xSemaphoreCreateBinary();
// Creating tasks
xTaskCreate(readButton, "task1", 2048, NULL, 1, NULL);
xTaskCreate(blinking, "task2", 2048, NULL, 2, NULL);
xTaskCreate(display, "task3", 2048, NULL, 3, NULL);
}
void loop()
{
}