#include <Arduino_FreeRTOS.h>
#include <LiquidCrystal.h>
#define PIN_ECHO 2
#define PIN_TRIG 3
#define LED_PIN 4
#define BUTTON_PIN 5
#define PERIODO_LED 300
#define PERIODO_TEMP 100
#define PERIODO_DIST 150
#define TIEMPO_REBOTE 300
const float BETA = 3950; // harus cocok dengan Koefisien Beta termistor
bool estadoLED;
bool estadoSensorDistancia;
bool botonRebotando;
float celsius;
float distancia;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// menentukan semua tugas
void TaskBlink(void *pvParameters);
void TaskLCD(void *pvParameters);
void ReadTemperature(void *pvParameters);
void ReadDistance(void *pvParameters);
void TaskDistanceOff(void *pvParameters);
// fungsi pengaturan berjalan sekali saat Anda menekan reset atau menyalakan papan
void setup() {
Serial.begin(9600);
// Now set up four tasks to run independently.
xTaskCreate(TaskBlink, "BlinkLED", 96, NULL, 2, NULL);
xTaskCreate(ReadTemperature, "ReadTemperature", 128, NULL, 2, NULL);
xTaskCreate(ReadDistance, "ReadDistance", 128, NULL, 1, NULL);
xTaskCreate(TaskLCD, "TaskLCD", 192, NULL, 1, NULL);
xTaskCreate(TaskDistanceOff, "TaskDistanceOff", 128, NULL, 1, NULL);
}
void loop()
{
// Kosong. Hal-hal dilakukan di Tugas.
}
void TaskBlink(void *pvParameters) {
(void) pvParameters;
// inisialisasi LED_PIN digital pada pin 4 sebagai output.
pinMode(LED_PIN, OUTPUT);
estadoLED = true;
for (;;) // A Task shall never return or exit.
{
if (estadoLED) digitalWrite(LED_PIN, HIGH);
else digitalWrite(LED_PIN, LOW);
estadoLED = !estadoLED;
vTaskDelay(PERIODO_LED/portTICK_PERIOD_MS );
}
}
void ReadTemperature(void *pvParameters) {
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
int analogValue = analogRead(A0);
celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
vTaskDelay(PERIODO_TEMP/portTICK_PERIOD_MS);
}
}
void ReadDistance(void *pvParameters) {
(void) pvParameters;
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
for (;;) // A Task shall never return or exit.
{
// Empieza una nueva lectura de distancia:
if(estadoSensorDistancia){
digitalWrite(PIN_TRIG, HIGH);
vTaskDelay(0.001/portTICK_PERIOD_MS);
digitalWrite(PIN_TRIG, LOW);
// Lee el resultado:
int duracion = pulseIn(PIN_ECHO, HIGH);
distancia = duracion / 58;
}
vTaskDelay(PERIODO_DIST/portTICK_PERIOD_MS);
}
}
void TaskLCD(void *pvParameters)
{
(void) pvParameters;
lcd.begin(16, 2);
for (;;) // A Task shall never return or exit.
{
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(celsius);
lcd.setCursor(0, 1);
lcd.print("Jarak: ");
lcd.print(distancia);
}
}
void TaskDistanceOff(void *pvParameters) {
(void) pvParameters;
pinMode(BUTTON_PIN, INPUT_PULLUP);
botonRebotando = false;
for (;;)
{
//Activa o desactiva el sensor de distancia
if (digitalRead(BUTTON_PIN) == LOW) {
if(!botonRebotando){
estadoSensorDistancia = !estadoSensorDistancia;
botonRebotando = true;
vTaskDelay(TIEMPO_REBOTE/portTICK_PERIOD_MS);
botonRebotando = false; //Sekarang diasumsikan bahwa tombol telah berhenti memantul
}
}
}
}