Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
5.4 kB
6
Indexable
Never
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Arduino_FreeRTOS.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

//mengatur pin sensor HC-SR04
#define trigpin 4  
#define echopin 2 

//mengatur pin led led buzzer dan relay
int ledlow = 5; 
int ledmiddle = 6; 
int ledhigh = 7; 
int buzzer = 8; 
int relay = 9;

//mendeklarasikan durasi dan distance untuk sensor HC-SR04
int duration, distance;

//mendeklarasikan setiap task
void Tasksonar(void *pvParameters);
void Tasklcd(void *pvParameters);
void Taskled(void *pvParameters);
void Taskbr(void *pvParameters);

void setup()
{
  Serial.begin(9600);
  lcd.begin();

  //mengatur mode pin sebagai input atau output
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  pinMode(ledlow, OUTPUT); 
  pinMode(ledmiddle, OUTPUT);
  pinMode(ledhigh, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(relay, OUTPUT);

  //mengatur kondisi awal saat sistem dihidupkan
  digitalWrite(ledlow, LOW); 
  digitalWrite(ledmiddle, LOW);
  digitalWrite(ledhigh, LOW);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW);
  
  //membuat task dan skala priotritasnya
  xTaskCreate(Tasksonar, // Task function
              "jarak", // Task name
              128, // Stack size 
              NULL, 
              3, // Priority
              NULL); 

  xTaskCreate(Tasklcd,
              "Serial",
              128,
              NULL, 
              2,
              NULL);
  xTaskCreate(Taskled,
              "Serial",
              128,
              NULL, 
              1,
              NULL);
  xTaskCreate(Taskbr,
              "Buzzer dan relay",
              128,
              NULL, 
              0,
              NULL);
}

void loop()
{
}

//task untuk sensor HC-SR04
void Tasksonar(void *pvParameters)
{
  (void) pvParameters;
  
  for (;;)
  {
    digitalWrite(trigpin, LOW); //memastikan tidak ada pengiriman gelombang suara (membersihkan)
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH); //memberikan trigger kepada transmiter selama 10 microsecond
    delayMicroseconds(10);
    digitalWrite(trigpin, LOW);

    duration = pulseIn(echopin, HIGH); //ketika menerima gelombang maka akan high dan menghitung waktunya (s = v . t) 

    distance = duration*0.034/2;
    vTaskDelay(1000/portTICK_PERIOD_MS);   
  }
}

//task menampilkan data ke lcd
void Tasklcd(void *pvParameters)
{
  (void) pvParameters;
  
  for (;;)
  {
    lcd.setCursor(0,0);
    lcd.print("Jarak = ");
    lcd.print(distance);       //Menampilkan jarak pada LCD 16x2
    lcd.print(" cm  ");

    if(  (distance > 0) && (distance <= 5)   ) //Batas Ambang
    {
      lcd.setCursor(0,1);
      lcd.print("Tangki Overload  ");
    } else
    if(  (distance > 5) && (distance <= 10)  ) //Level High
    {
      lcd.setCursor(0,1);
      lcd.print("Tangki Penuh  ");

    } else

    if(  (distance > 10) && (distance <= 40)  ) //Level Middle
    {
      lcd.setCursor(0,1);
      lcd.print("Tangki Berisi  ");
    } else

    if(  (distance > 40) && (distance <= 60)  ) //Level Low
    {
      lcd.setCursor(0,1);
      lcd.print("Tangki Kosong   ");
    }else

    if(  (distance > 60)  ) //Level Low
    {
      lcd.setCursor(0,1);
      lcd.print("Tangki Kosong  ");
    }
  vTaskDelay(1000/portTICK_PERIOD_MS);    
  }
}

//task untuk led
void Taskled(void *pvParameters)
{
  (void) pvParameters;
  for(;;){
  if(  (distance > 0) && (distance <= 5)   ) //Batas Ambang
    {
      digitalWrite(ledlow, HIGH); 
      digitalWrite(ledmiddle, LOW);
      digitalWrite(ledhigh, LOW);
    } else
    if(  (distance > 5) && (distance <= 10)  ) //Level High
    {

      digitalWrite(ledlow, LOW); 
      digitalWrite(ledmiddle, LOW);
      digitalWrite(ledhigh, HIGH);

    } else

    if(  (distance > 10) && (distance <= 40)  ) //Level Middle
    {

      digitalWrite(ledlow, LOW); 
      digitalWrite(ledmiddle, HIGH);
      digitalWrite(ledhigh, LOW);
    } else

    if(  (distance > 40) && (distance <= 60)  ) //Level Low
    {

      digitalWrite(ledlow, HIGH); 
      digitalWrite(ledmiddle, LOW);
      digitalWrite(ledhigh, LOW);
    }else

    if(  (distance > 60)  ) //Level Low
    {

      digitalWrite(ledlow, HIGH); 
      digitalWrite(ledmiddle, LOW);
      digitalWrite(ledhigh, LOW);
    }
  vTaskDelay(1000/portTICK_PERIOD_MS);    
  }    
}

//task untuk buzzer dan relay
void Taskbr(void *pvParameters)
{
  (void) pvParameters;
  for(;;){
     if(  (distance > 0) && (distance <= 5)   ) //Batas Ambang
    {
      digitalWrite(relay, HIGH);
      digitalWrite(buzzer, HIGH);
      delay(1000);
      digitalWrite(buzzer, LOW);
    } else
    if(  (distance > 5) && (distance <= 10)  ) //Level High
    {
      digitalWrite(buzzer, LOW);
      digitalWrite(relay, LOW); 
    } else

    if(  (distance > 10) && (distance <= 40)  ) //Level Middle
    {
      digitalWrite(buzzer, LOW);
      digitalWrite(relay, LOW);
    } else

    if(  (distance > 40) && (distance <= 60)  ) //Level Low
    {
      digitalWrite(buzzer, LOW);
      digitalWrite(relay, LOW);
    }else

    if(  (distance > 60)  ) //Level Low
    {
      digitalWrite(buzzer, LOW);
      digitalWrite(relay, LOW);
    }
  vTaskDelay(1000/portTICK_PERIOD_MS);  
  }  
}