Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- // initialize the library by associating any needed LCD interface pin
- // with the arduino pin number it is connected to
- const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- int distanceThreshold = 0;
- int cm = 0;
- int inches = 0;
- long readUltrasonicDistance(int triggerPin, int echoPin)
- {
- pinMode(triggerPin, OUTPUT); // Clear the trigger
- digitalWrite(triggerPin, LOW);
- delayMicroseconds(2);
- // Sets the trigger pin to HIGH state for 10 microseconds
- digitalWrite(triggerPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(triggerPin, LOW);
- pinMode(echoPin, INPUT);
- // Reads the echo pin, and returns the sound wave travel time in microseconds
- return pulseIn(echoPin, HIGH);
- }
- void setup() {
- /*lcd.begin(16, 2);
- lcd.print("Time to wash");
- lcd.setCursor(0, 1);
- lcd.print("your clothes");*/
- }
- void loop() {
- // set threshold distance to activate LEDs
- distanceThreshold = 350;
- // measure the ping time in cm
- cm = 0.01723 * readUltrasonicDistance(7, 6);
- // convert to inches by dividing by 2.54
- inches = (cm / 2.54);
- Serial.print(cm);
- Serial.print("cm, ");
- Serial.print(inches);
- Serial.println("in");
- if (cm > distanceThreshold) {
- lcd.begin(16, 2);
- lcd.print("Time to wash");
- lcd.setCursor(0, 1);
- lcd.print("your clothes");
- }
- if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
- lcd.begin(16, 2);
- lcd.print("Time to wash");
- lcd.setCursor(0, 1);
- lcd.print("your clothes");
- }
- if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
- lcd.begin(16, 2);
- lcd.print("No need to wash");
- lcd.setCursor(0, 1);
- lcd.print("your clothes");
- }
- if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
- lcd.begin(16, 2);
- lcd.print("No need to wash");
- lcd.setCursor(0, 1);
- lcd.print("your clothes");
- }
- if (cm <= distanceThreshold - 350) {
- lcd.begin(16, 2);
- lcd.print("No need to wash");
- lcd.setCursor(0, 1);
- lcd.print("your clothes");
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement