#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Arduino_FreeRTOS.h>
#define SERVO_PIN 9
#define POTENTIOMETER_PIN A0
#define PHOTOSENSOR_PIN A1
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
Servo myservo;
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
TaskHandle_t xHandle_LCD;
volatile int potValue = 0;
volatile int photoValue = 0;
volatile bool blinkingState = false;
void TaskReadPotentiometer(void *pvParameters) {
(void)pvParameters;
for (;;) {
potValue = analogRead(POTENTIOMETER_PIN);
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void TaskReadPhotoSensor(void *pvParameters) {
(void)pvParameters;
for (;;) {
vTaskDelay(500 / portTICK_PERIOD_MS); // Wait 500 ms before reading
photoValue = analogRead(PHOTOSENSOR_PIN);
}
}
void TaskControlServo(void *pvParameters) {
(void)pvParameters;
for (;;) {
int targetPosition = map(potValue, 0, 1023, 0, 180);
myservo.write(targetPosition);
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
void TaskDisplayLCD(void *pvParameters) {
(void)pvParameters;
lcd.init();
lcd.backlight();
for (;;) {
lcd.setCursor(0, 0);
lcd.print("Angle: ");
lcd.print(map(potValue, 0, 1023, 0, 180));
lcd.print(" deg ");
vTaskDelay(500 / portTICK_PERIOD_MS);
lcd.setCursor(0, 1);
lcd.print("Light: ");
lcd.print(map(photoValue, 0, 1023, 0, LCD_COLUMNS));
lcd.print(" Oleksii ");
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
void setup() {
myservo.attach(SERVO_PIN);
xTaskCreate(TaskReadPotentiometer, "ReadPot", 128, NULL, 1, NULL);
xTaskCreate(TaskReadPhotoSensor, "ReadPhoto", 128, NULL, 1, NULL);
xTaskCreate(TaskDisplayLCD, "DisplayLCD", 128, NULL, 2, &xHandle_LCD);
xTaskCreate(TaskControlServo, "ControlServo", 128, NULL, 3, NULL);
vTaskStartScheduler();
}
void loop() {
}