#include <Arduino_FreeRTOS.h>
#include <task.h>
#include<TimerOne.h> //library yang dibutuhkan untuk menggunakan interrupt
#include <LiquidCrystal_I2C.h> // Library for LCD
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledhijau = 4; // the number of the LED pin
const int ledmerah = 3; // the number of the LED pin
const int trigPin = 6;
const int echoPin = 5;
const int ldrPin = A0;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
long duration;
int distance;
int intensitas;
String cahaya;
bool stopTasks = false;
TaskHandle_t Handle_aTask; //ledhijau blinking
TaskHandle_t Handle_bTask; //membaca sensor ultrasonic
TaskHandle_t Handle_cTask; //membaca sensor ldr
// TaskHandle_t Handle_dTask;
//ledhijau blinking
static void aTask(void* pvParameters) {
while (1) {
if (!stopTasks){
digitalWrite(ledhijau, HIGH);
delay(1000);
digitalWrite(ledhijau, LOW);
delay(1000);
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
//membaca sensor ultrasonic
static void bTask(void* pvParameters) {
while (1) {
// Clears the trigPin
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);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
//membaca sensor ldr
static void cTask(void* pvParameters) {
while (1) {
intensitas = analogRead(ldrPin);
if(intensitas <= 200){
cahaya = "Terang";
}else{
cahaya = "gelap";
}
// Serial.print("Cahaya: " );
// Serial.println(cahaya);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void setup() {
pinMode(ledhijau, OUTPUT);
pinMode(ledmerah, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Timer1.initialize(10000);
Timer1.attachInterrupt(blinkmerah);
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
xTaskCreate(aTask, "Task A", 256, NULL, tskIDLE_PRIORITY + 2, &Handle_aTask);
xTaskCreate(bTask, "Task B", 256, NULL, tskIDLE_PRIORITY + 1, &Handle_bTask);
xTaskCreate(cTask, "Task C", 256, NULL, tskIDLE_PRIORITY + 1, &Handle_bTask);
// Start the RTOS, this function will never return and will schedule the tasks.
vTaskStartScheduler();
}
void loop() {
if (!stopTasks){
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Distance: "); // print message at (0, 0)
lcd.setCursor(11, 0); // move cursor to (11, 0)
lcd.print(distance); // print message at (11, 0)
lcd.setCursor(0, 1); // move cursor to (0, 0)
lcd.print("Cahaya : "); // print message at (0, 0)
lcd.setCursor(11, 1); // move cursor to (11, 0)
lcd.print(cahaya); // print message at (11, 0)
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
void blinkmerah(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledmerah, HIGH);
stopTasks = true;
} else {
// turn LED off:
digitalWrite(ledmerah, LOW);
stopTasks = false;
}
}