/*
-->> Create 3 tasks
--> Send sensor data to lcd
--> Print increment count variable to serial monitor
--> Blink First LED on btn click
--> Blink second LED frequently after 1000 msec.
*/
#include <Arduino.h>
#include <Arduino_FreeRTOS.h>
#include <LCD_I2C.h>
// Sensor
// defines pins numbers
const int trigPin = 4;
const int echoPin = 5;
// defines variables
long duration;
int distance;
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change according
int ledRed = 11;
byte ledBlue = 12;
// btn
#define btn 2
int lastButtonState = HIGH; // Initial button state (assuming HIGH is not pressed)
const int debounceTime = 50;
unsigned long lastDebounceTime = 0; // Last time the button state changed
// --------------------------------------------------------------------------
void sendSensorDataToLCD(void *pvParameters);
void printCountsOnSerialMonitor(void *pvParameters);
void blinkLEDonBtnClick(void *pvParameters);
void blinkLED(void *pvParameters);
void sendSensorDataToLCD(void *pvParameters)
{
for (;;)
{
digitalWrite(trigPin, LOW);
//delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
// delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
// Serial.print("Distance: ");
// Serial.println(distance);
lcd.setCursor(5, 1);
lcd.print(distance);
vTaskDelay(1000 / portTICK_PERIOD_MS); // wait for one second
}
}
void printCountsOnSerialMonitor(void *pvParameters)
{ int count;
for (;;)
{
count++;
Serial.println(count);
vTaskDelay(1000 / portTICK_PERIOD_MS); // wait for one second
}
}
void blinkLEDonBtnClick(void *pvParameters)
{
while (1)
{
int currentButtonState = digitalRead(btn);
if (currentButtonState != lastButtonState)
{
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime >= debounceTime)
{
lastButtonState = currentButtonState;
digitalWrite(ledRed, !digitalRead(ledRed)); // Replace ledPin with your LED pin
lastDebounceTime = currentTime;
// vTaskDelay(pdMS_TO_TICKS(50));
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
}
void blinkLED(void *pvParameters)
{
(void)pvParameters;
for (;;)
{
digitalWrite(ledBlue, !digitalRead(ledBlue));
vTaskDelay(500 / portTICK_PERIOD_MS); // wait for one second
}
}
void setup()
{
lcd.begin(); // If you are using more I2C devices using the Wire library use lcd.begin(false)
// this stop the library(LCD_I2C) from calling Wire.begin()
lcd.backlight();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledRed, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(btn, INPUT_PULLUP);
Serial.begin(9600); // Starts the serial communication
xTaskCreate(sendSensorDataToLCD, "Task 1", 256, NULL, 1, NULL);
xTaskCreate(blinkLEDonBtnClick, "Task 2", 128, NULL, 1, NULL);
xTaskCreate(blinkLED, "Task 3", 128, NULL, 1, NULL);
xTaskCreate(printCountsOnSerialMonitor, "Task 4", 128, NULL, 4, NULL);
}
void loop()
{
}