Advertisement
ZulRocky

Interface Modul 8

Feb 24th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <LiquidCrystal_SR_LCD3.h>
  2. #include <EEPROM.h>
  3. const int PIN_LCD_STROBE = 2;
  4. const int PIN_LCD_DATA = 3;
  5. const int PIN_LCD_CLOCK = 4;
  6. LiquidCrystal_SR_LCD3 lcd(PIN_LCD_DATA, PIN_LCD_CLOCK, PIN_LCD_STROBE);
  7.  
  8. const int trigPin = 8;
  9. const int echoPin = 9;
  10. int button=7, push=0, ok=0;
  11. int buzzer=11;
  12. int led=6, led1=12, led2=13;
  13.  
  14. void setup() {
  15. // initialize serial communication:
  16. Serial.begin(9600);
  17. pinMode(button, INPUT);
  18. pinMode(buzzer, OUTPUT);
  19. pinMode(led, OUTPUT);
  20. pinMode(led1, OUTPUT);
  21. pinMode(led2, OUTPUT);
  22. analogWrite(led, 0);
  23.  
  24. lcd.begin(16,2);
  25. lcd.home();
  26. }
  27.  
  28. void loop()
  29. {
  30. // establish variables for duration of the ping,
  31. // and the distance result in inches and centimeters:
  32. long duration, inches, cm;
  33.  
  34. // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  35. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  36. pinMode(trigPin, OUTPUT);
  37. digitalWrite(trigPin, LOW);
  38. delayMicroseconds(2);
  39. digitalWrite(trigPin, HIGH);
  40. delayMicroseconds(10);
  41. digitalWrite(trigPin, LOW);
  42.  
  43. // Read the signal from the sensor: a HIGH pulse whose
  44. // duration is the time (in microseconds) from the sending
  45. // of the ping to the reception of its echo off of an object.
  46. pinMode(echoPin, INPUT);
  47. duration = pulseIn(echoPin, HIGH);
  48.  
  49. // convert the time into a distance
  50. inches = microsecondsToInches(duration);
  51. cm = microsecondsToCentimeters(duration);
  52.  
  53. while(digitalRead(button)==LOW){
  54.  
  55. if(push==0 && ok==0){
  56. push=1;
  57. }else if(push==1 && ok==1){
  58. push=2;
  59. }else if(push==2 && ok==2){
  60. push=3;
  61. }else if(push==3 && ok==3){
  62. digitalWrite(led, LOW);
  63. digitalWrite(led1, LOW);
  64. digitalWrite(led2, LOW);
  65. push=4;
  66. }else if(push==4 && ok==4){
  67. push=1;
  68. }
  69. }
  70.  
  71. if(push==0){
  72. ok=0;
  73. }
  74.  
  75. if(push==1){
  76. digitalWrite(led, 0);
  77. ok=1;
  78. Serial.print(inches);
  79. Serial.print("in, ");
  80. Serial.print(cm);
  81. Serial.print("cm");
  82. Serial.println();
  83. lcd.print(cm);
  84. lcd.setCursor(5,0);
  85. lcd.print(" CM");
  86.  
  87. delay(100);
  88. lcd.clear();
  89. }else if(push==2){
  90. ok=2;
  91. lcd.print(cm);
  92. lcd.setCursor(5,0);
  93. lcd.print(" CM");
  94. if(cm < 10){
  95. digitalWrite(buzzer, HIGH);
  96. lcd.setCursor(0,1);
  97. lcd.print(" BUZZER : ON");
  98. }else{
  99. digitalWrite(buzzer, LOW);
  100. lcd.setCursor(0,1);
  101. lcd.print(" BUZZER : OFF");
  102. }
  103.  
  104. delay(100);
  105. lcd.clear();
  106. }else if(push==3){
  107. ok=3;
  108. lcd.print(cm);
  109. lcd.setCursor(5,0);
  110. lcd.print(" CM");
  111. int val = 255-(cm);
  112. if(val < 0){
  113. val=0;
  114. }
  115. Serial.println(val);
  116. analogWrite(led, val);
  117.  
  118. delay(100);
  119. lcd.clear();
  120. }else if(push==4){
  121. ok=4;
  122. Serial.println(cm);
  123. lcd.print(cm);
  124. lcd.setCursor(5,0);
  125. lcd.print(" CM");
  126. if(cm < 10){
  127. lcd.setCursor(0,1);
  128. lcd.print("1");
  129. digitalWrite(led, HIGH);
  130. digitalWrite(led1, LOW);
  131. digitalWrite(led2, LOW);
  132. digitalWrite(buzzer, HIGH);
  133. delay(1000);
  134. digitalWrite(buzzer, LOW);
  135. }else if(cm < 20 && cm > 10){
  136. lcd.setCursor(2,1);
  137. lcd.print("1");
  138. digitalWrite(led, LOW);
  139. digitalWrite(led1, HIGH);
  140. digitalWrite(led2, LOW);
  141. }else if(cm < 30 && cm > 20){
  142. lcd.setCursor(4,1);
  143. lcd.print("1");
  144. digitalWrite(led, LOW);
  145. digitalWrite(led1, LOW);
  146. digitalWrite(led2, HIGH);
  147. }else if(cm > 40){
  148. digitalWrite(led, LOW);
  149. digitalWrite(led1, LOW);
  150. digitalWrite(led2, LOW);
  151. }
  152. delay(100);
  153. lcd.clear();
  154. digitalWrite(buzzer, LOW);
  155. }
  156. }
  157.  
  158. long microsecondsToInches(long microseconds)
  159. {
  160. return microseconds / 74 / 2;
  161. }
  162.  
  163. long microsecondsToCentimeters(long microseconds)
  164. {
  165. return microseconds / 29 / 2;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement