Advertisement
Guest User

Workplz

a guest
May 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  4.  
  5. #define trigPin 13
  6. #define echoPin 12
  7. int counter = 0;
  8. int currentState = 0;
  9. int previousState = 0;
  10. void setup() {
  11.  
  12.  
  13.  
  14. Serial.begin (9600);
  15. pinMode(trigPin, OUTPUT);
  16. pinMode(echoPin, INPUT);
  17. }
  18.  
  19. void loop() {
  20.  
  21. lcd.begin(16,2); // sixteen characters across - 2 lines
  22. // first character - 1st line
  23. lcdbacklight();
  24. lcd.setCursor(0,0);
  25. lcd.print(counter);
  26. lcd.clear();
  27. lcd.print(counter);
  28.  
  29.  
  30. long duration, distance;
  31. digitalWrite(trigPin, LOW);
  32. delayMicroseconds(1000);
  33. digitalWrite(trigPin, HIGH);
  34. delayMicroseconds(1000);
  35. digitalWrite(trigPin, LOW);
  36. duration = pulseIn(echoPin, HIGH);
  37. distance = (duration/1) / 29.1;
  38. if (distance <= 10){
  39. currentState = 1;
  40. }
  41. else {
  42. currentState = 0;
  43. }
  44. delay(500);
  45. if(currentState != previousState){
  46. if(currentState == 1){
  47. counter = counter + 1;
  48. Serial.println(counter);
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement