Advertisement
Guest User

Jib Sensor

a guest
Aug 3rd, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
  4.  
  5. int pingPin = 2;
  6. int redLED = 3;
  7. int greenLED = 12;
  8. int blueLED = 11;
  9.  
  10. void setup() {
  11.  
  12. pinMode(redLED, OUTPUT);
  13. pinMode(greenLED, OUTPUT);
  14. pinMode(blueLED, OUTPUT);
  15.  
  16. // set up the LCD's number of columns and rows:
  17. lcd.begin(16, 2);
  18. //lcd.setCursor(2, 0);
  19. //lcd.print("COMPANYY NAME);
  20. //lcd.setCursor(3, 1);
  21. //lcd.print("Jib Sensor");
  22. //delay(5000);
  23.  
  24. }
  25.  
  26. void loop(){
  27. // establish variables for duration of the ping,
  28. // and the distance result in inches and centimeters:
  29. long duration, inches, cm;
  30.  
  31. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  32. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  33. pinMode(pingPin, OUTPUT);
  34. digitalWrite(pingPin, LOW);
  35. delayMicroseconds(2);
  36. digitalWrite(pingPin, HIGH);
  37. delayMicroseconds(10);
  38. digitalWrite(pingPin, LOW);
  39.  
  40. // The same pin is used to read the signal from the PING))): a HIGH
  41. // pulse whose duration is the time (in microseconds) from the sending
  42. // of the ping to the reception of its echo off of an object.
  43. pinMode(pingPin, INPUT);
  44. duration = pulseIn(pingPin, HIGH);
  45.  
  46. // convert the time into a human readable distance
  47. inches = microsecondsToInches(duration);
  48. cm = microsecondsToCentimeters(duration);
  49.  
  50. //High Warning
  51. if (inches > 6 && inches < 18){
  52. lcd.clear();
  53. lcd.setCursor(0,0);
  54. lcd.print("Caution - HIGH");
  55. lcd.setCursor(0, 1);
  56. lcd.print(inches);
  57. lcd.print(" in");
  58. setColor(255, 255, 0); //Yellow
  59. delay(1500);
  60. setColor(0, 0, 0); //Off
  61. delay(500);
  62.  
  63. }
  64.  
  65. //High DANGER
  66. else if (inches < 6){
  67. lcd.clear();
  68. lcd.setCursor(0,0);
  69. lcd.print("TOO HIGH!");
  70. lcd.setCursor(0, 1);
  71. lcd.print(inches);
  72. lcd.print(" in");
  73. setColor(255, 0, 0); //Red
  74. delay(150);
  75. setColor(0, 0, 0); //Off
  76.  
  77. }
  78.  
  79. //Low Warning
  80. else if (inches < 48 && inches >60){
  81. lcd.clear();
  82. lcd.setCursor(0,0);
  83. lcd.print("Caution - LOW");
  84. lcd.setCursor(0, 1);
  85. lcd.print(inches);
  86. lcd.print(" in");
  87. setColor(255, 255, 0); //Yellow
  88. delay(1500);
  89. setColor(0, 0, 0); //Off
  90. delay(500);
  91.  
  92. }
  93.  
  94. //Low DANGER
  95. else if (inches > 60){
  96. lcd.clear();
  97. lcd.setCursor(0,0);
  98. lcd.print("TOO LOW!");
  99. lcd.setCursor(0, 1);
  100. lcd.print(inches);
  101. lcd.print(" in");
  102. setColor(255, 0, 0); //Red
  103. delay(150);
  104. setColor(0, 0, 0); //Off
  105.  
  106. }
  107.  
  108. else {
  109.  
  110. // clear lcd content
  111. lcd.clear();
  112. // set the cursor to column 0, line 0
  113. // (note: line 1 is the second row, since counting begins with 0):
  114. lcd.setCursor(0, 0);
  115. lcd.print("Current Distance");
  116. lcd.setCursor(0, 1);
  117. lcd.print(inches);
  118. lcd.print(" in");
  119. setColor(0, 255, 0); //Green
  120. }
  121.  
  122. //delay(100);
  123.  
  124. }
  125.  
  126. long microsecondsToInches(long microseconds){
  127. // According to Parallax's datasheet for the PING))), there are
  128. // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  129. // second). This gives the distance travelled by the ping, outbound
  130. // and return, so we divide by 2 to get the distance of the obstacle.
  131. return microseconds / 74 / 2;}
  132.  
  133. long microsecondsToCentimeters(long microseconds){
  134. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  135. // The ping travels out and back, so to find the distance of the
  136. // object we take half of the distance travelled.
  137. return microseconds / 29 / 2;}
  138.  
  139. void setColor(int red, int green, int blue)
  140. {
  141. analogWrite(redLED, red);
  142. analogWrite(greenLED, green);
  143. analogWrite(blueLED, blue);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement