Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. // initialize the library by associating any needed LCD interface pin
  4. // with the arduino pin number it is connected to
  5. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  6. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  7.  
  8. int distanceThreshold = 0;
  9.  
  10. int cm = 0;
  11.  
  12. int inches = 0;
  13.  
  14. long readUltrasonicDistance(int triggerPin, int echoPin)
  15. {
  16. pinMode(triggerPin, OUTPUT); // Clear the trigger
  17. digitalWrite(triggerPin, LOW);
  18. delayMicroseconds(2);
  19. // Sets the trigger pin to HIGH state for 10 microseconds
  20. digitalWrite(triggerPin, HIGH);
  21. delayMicroseconds(10);
  22. digitalWrite(triggerPin, LOW);
  23. pinMode(echoPin, INPUT);
  24. // Reads the echo pin, and returns the sound wave travel time in microseconds
  25. return pulseIn(echoPin, HIGH);
  26. }
  27.  
  28. void setup() {
  29. /*lcd.begin(16, 2);
  30. lcd.print("Time to wash");
  31. lcd.setCursor(0, 1);
  32. lcd.print("your clothes");*/
  33.  
  34.  
  35.  
  36.  
  37.  
  38. }
  39.  
  40. void loop() {
  41. // set threshold distance to activate LEDs
  42. distanceThreshold = 350;
  43. // measure the ping time in cm
  44. cm = 0.01723 * readUltrasonicDistance(7, 6);
  45. // convert to inches by dividing by 2.54
  46. inches = (cm / 2.54);
  47. Serial.print(cm);
  48. Serial.print("cm, ");
  49. Serial.print(inches);
  50. Serial.println("in");
  51. if (cm > distanceThreshold) {
  52. lcd.begin(16, 2);
  53. lcd.print("Time to wash");
  54. lcd.setCursor(0, 1);
  55. lcd.print("your clothes");
  56. }
  57. if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
  58. lcd.begin(16, 2);
  59. lcd.print("Time to wash");
  60. lcd.setCursor(0, 1);
  61. lcd.print("your clothes");
  62. }
  63. if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
  64. lcd.begin(16, 2);
  65. lcd.print("No need to wash");
  66. lcd.setCursor(0, 1);
  67. lcd.print("your clothes");
  68. }
  69. if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
  70. lcd.begin(16, 2);
  71. lcd.print("No need to wash");
  72. lcd.setCursor(0, 1);
  73. lcd.print("your clothes");
  74. }
  75. if (cm <= distanceThreshold - 350) {
  76. lcd.begin(16, 2);
  77. lcd.print("No need to wash");
  78. lcd.setCursor(0, 1);
  79. lcd.print("your clothes");
  80. }
  81. delay(1000);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement