Advertisement
agungsugiarto

arduino_sketch

Mar 11th, 2018
3,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  led
  2. byte red = 13;
  3. byte yellow = 12;
  4. byte green = 11;
  5. // buzzer
  6. byte buzzer = 10;
  7. // hc-SR04
  8. #define pingPin 7
  9. long duration, distance;
  10. // monitor
  11. #include <Wire.h>
  12. #include <LiquidCrystal_I2C.h>
  13. LiquidCrystal_I2C lcd(0x3f,16,2);
  14.  
  15.  
  16. void setup() {
  17.   // initialize the LCD
  18.   lcd.begin();
  19.  
  20.   // Turn on the blacklight and print a message.
  21.   lcd.backlight();
  22.   lcd.setCursor(1,0);
  23.   lcd.print("Program Arduino");
  24.   lcd.setCursor(2,1);
  25.   lcd.print("Sensor Jarak");
  26.   delay(30000);
  27.    
  28.   // put your setup code here, to run once:
  29.   Serial.begin(9600);
  30.   // led
  31.   pinMode(red, OUTPUT);
  32.   pinMode(yellow, OUTPUT);
  33.   pinMode(green, OUTPUT);
  34.   // buzzer
  35.   pinMode(buzzer, OUTPUT);
  36.  
  37. }
  38.  
  39. void loop() {
  40.   // put your main code here, to run repeatedly:
  41.   // hc-SR04
  42.   duration = 0;
  43.   distance = 0;
  44.   pinMode(pingPin, OUTPUT);
  45.   digitalWrite(pingPin, LOW);
  46.   delayMicroseconds(2);
  47.   digitalWrite(pingPin, HIGH);
  48.   delayMicroseconds(5);
  49.   digitalWrite(pingPin, LOW);
  50.   pinMode(pingPin, INPUT);
  51.   duration = pulseIn(pingPin, HIGH);
  52.   distance = duration/29/2;
  53.   Serial.print(distance);
  54.   Serial.print("cm");
  55.   Serial.println();
  56.   delay(100);
  57.  
  58.   // Turn on the blacklight and print a message.
  59.   lcd.backlight();
  60.   lcd.setCursor(0,0);
  61.   lcd.print("jarak = ");
  62.   lcd.print(distance);
  63.   lcd.print(" cm   ");
  64.   Serial.println();
  65.   lcd.setCursor(0,1);
  66.   lcd.print("Aman!");
  67.   lcd.backlight();
  68.   lcd.setCursor(0,0);
  69.   lcd.print("jarak = ");
  70.   lcd.print(distance);
  71.   lcd.print(" cm   ");
  72.   Serial.println();
  73.   lcd.setCursor(0,1);
  74.   lcd.print("Hati-hati!");
  75.   digitalWrite(green, HIGH);
  76.   delay(1000);
  77.   digitalWrite(green, LOW);
  78.   lcd.backlight();
  79.   lcd.setCursor(0,0);
  80.   lcd.print("jarak = ");
  81.   lcd.print(distance);
  82.   lcd.print(" cm");
  83.   Serial.println();
  84.   lcd.setCursor(0,1);
  85.   lcd.print("Waspada!      ");
  86.   digitalWrite(yellow, HIGH);
  87.   delay(1000);
  88.   digitalWrite(yellow, LOW);
  89.   lcd.backlight();
  90.   lcd.setCursor(0,0);
  91.   lcd.print("jarak = ");
  92.   lcd.print(distance);
  93.   lcd.print(" cm");
  94.   Serial.println();
  95.   lcd.setCursor(0,1);
  96.   lcd.print("Bahaya!");
  97.   digitalWrite(red, HIGH);
  98.   digitalWrite(buzzer, HIGH);
  99.   delay(500);
  100.   digitalWrite(red, LOW);
  101.   digitalWrite(buzzer, LOW);
  102.   delay(500);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement