Kode
Program Arduino
#define trigPin 10
#define echoPin 11
#define led1 2
#define led2 3
#define led3 4
#define led4 5
#define led5 6
#define led6 7
#define speaker 12
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin (9600);
lcd.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(speaker,
OUTPUT);
lcd.setCursor(0, 0);
lcd.print("SENSOR PARKIR");
delay(1000);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(8);
digitalWrite(trigPin, HIGH);
delayMicroseconds(8);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Jarak : ");
lcd.setCursor(8, 0);
lcd.print(distance);
lcd.print(" ");
lcd.print("CM");
lcd.print(" ");
if (distance <= 50 && distance >40) {
lcd.setCursor(0, 1);
lcd.print("SIAGA !!"); tone(speaker, 3000);
digitalWrite(led1, HIGH);
digitalWrite(led2,
HIGH);
delay(300);
|
noTone(speaker);
delay(300);
noTone(speaker);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
else
if (distance <= 40 && distance > 30) {
lcd.setCursor(0, 1);
lcd.print("WASPADA !!");
tone(speaker, 3000);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
delay(200);
noTone(speaker);
delay(200);
noTone(speaker);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
else if (distance <= 30 &&
distance > 10) {
lcd.setCursor(0, 1);
lcd.print("AWAS !!");
tone(speaker, 3000);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
delay(100);
noTone(speaker);
delay(100);
noTone(speaker);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
else if (distance <= 10 &&
distance >= 0) {
lcd.setCursor(0, 1);
lcd.print("BERHENTI !!!");
tone(speaker, 3000);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
delay(1000);
}
else {
noTone(speaker);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
delay(600);
}
}
|