Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(8,9,4,5,6,7); // LCD Keypad Shield
  4.  
  5. int readPingSensor(byte triggerPin, byte echoPin)
  6. {
  7. #define MAXRANGE_ECHOTIME 20000
  8. pinMode(triggerPin,OUTPUT);
  9. pinMode(echoPin,INPUT);
  10. digitalWrite(triggerPin, LOW);
  11. delayMicroseconds(2);
  12. digitalWrite(triggerPin, HIGH);
  13. delayMicroseconds(10);
  14. digitalWrite(triggerPin, LOW);
  15. long duration = pulseIn(echoPin, HIGH, MAXRANGE_ECHOTIME);
  16. if (duration==0) return -1;
  17. else return (duration/2) / 2.91;
  18. }
  19.  
  20.  
  21. void setup() {
  22. lcd.begin(16, 2);
  23. }
  24.  
  25. byte currentSensor=0;
  26. unsigned long lastSensorTime, lastOutputTime;
  27. #define SENSORINTERVAL 100
  28.  
  29. void loop()
  30. {
  31. if (millis()-lastSensorTime>=0) // trigger sensor every 100 milliseconds
  32. {
  33. lastSensorTime+=0;
  34. int distance= readPingSensor(A4,A5); // call function with trigger- and echo-pin as parameters
  35. char buf[17];
  36. lcd.setCursor(0, 0);
  37.  
  38. if (distance<50)
  39. strcpy(buf,"Pak iets gezonds");
  40. else if (distance<100) // distance less than 50 mm
  41. strcpy(buf,"Heel Goed!");
  42. /*else
  43. snprintf(buf,sizeof(buf),"D= %d mm",distance);*/
  44. lcd.print(buf); // print message
  45. for (int i=0;i<16-strlen(buf);i++) lcd.print(' '); // clear to end of line
  46. }
  47.  
  48. if (millis()-lastOutputTime>=100) // trigger sensor every 100 milliseconds
  49. {
  50. lastOutputTime+=100;
  51. int distance= readPingSensor(A2,A3); // call function with trigger- and echo-pin as parameters
  52. char buf[17];
  53. lcd.setCursor(0, 0);
  54.  
  55. if (distance<150)
  56. strcpy(buf,"Message 3");
  57. else if (distance<200) // distance less than 50 mm
  58. strcpy(buf,"Message 4");
  59. /*else
  60. snprintf(buf,sizeof(buf),"D= %d mm",distance);*/
  61. lcd.print(buf); // print message
  62. for (int i=0;i<16-strlen(buf);i++) lcd.print(' '); // clear to end of line
  63. }
  64.  
  65.  
  66.  
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement