Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // led
- byte red = 13;
- byte yellow = 12;
- byte green = 11;
- // buzzer
- byte buzzer = 10;
- // hc-SR04
- #define pingPin 7
- long duration, distance;
- // monitor
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x3f,16,2);
- void setup() {
- // initialize the LCD
- lcd.begin();
- // Turn on the blacklight and print a message.
- lcd.backlight();
- lcd.setCursor(1,0);
- lcd.print("Program Arduino");
- lcd.setCursor(2,1);
- lcd.print("Sensor Jarak");
- delay(30000);
- // put your setup code here, to run once:
- Serial.begin(9600);
- // led
- pinMode(red, OUTPUT);
- pinMode(yellow, OUTPUT);
- pinMode(green, OUTPUT);
- // buzzer
- pinMode(buzzer, OUTPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- // hc-SR04
- duration = 0;
- distance = 0;
- pinMode(pingPin, OUTPUT);
- digitalWrite(pingPin, LOW);
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH);
- delayMicroseconds(5);
- digitalWrite(pingPin, LOW);
- pinMode(pingPin, INPUT);
- duration = pulseIn(pingPin, HIGH);
- distance = duration/29/2;
- Serial.print(distance);
- Serial.print("cm");
- Serial.println();
- delay(100);
- // Turn on the blacklight and print a message.
- lcd.backlight();
- lcd.setCursor(0,0);
- lcd.print("jarak = ");
- lcd.print(distance);
- lcd.print(" cm ");
- Serial.println();
- lcd.setCursor(0,1);
- lcd.print("Aman!");
- lcd.backlight();
- lcd.setCursor(0,0);
- lcd.print("jarak = ");
- lcd.print(distance);
- lcd.print(" cm ");
- Serial.println();
- lcd.setCursor(0,1);
- lcd.print("Hati-hati!");
- digitalWrite(green, HIGH);
- delay(1000);
- digitalWrite(green, LOW);
- lcd.backlight();
- lcd.setCursor(0,0);
- lcd.print("jarak = ");
- lcd.print(distance);
- lcd.print(" cm");
- Serial.println();
- lcd.setCursor(0,1);
- lcd.print("Waspada! ");
- digitalWrite(yellow, HIGH);
- delay(1000);
- digitalWrite(yellow, LOW);
- lcd.backlight();
- lcd.setCursor(0,0);
- lcd.print("jarak = ");
- lcd.print(distance);
- lcd.print(" cm");
- Serial.println();
- lcd.setCursor(0,1);
- lcd.print("Bahaya!");
- digitalWrite(red, HIGH);
- digitalWrite(buzzer, HIGH);
- delay(500);
- digitalWrite(red, LOW);
- digitalWrite(buzzer, LOW);
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement