RobinBrusbo

Med display WIP

Jan 24th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. #define TRIG_PIN 9
  4. #define ECHO_PIN 8
  5. #define SOUND_PIN 12
  6. #define BUTTON_PIN 2
  7. #define RED_LIGHT 5
  8. #define BLUE_LIGHT 6
  9.  
  10. int LIGHT_RED_TIME = 500;
  11. int LIGHT_BLUE_TIME = 500;
  12.  
  13. long long start;
  14. long long lastToggle;
  15. long long lampTimer;
  16. bool active;
  17.  
  18. LiquidCrystal lcd(11, 10, 7, 4, 3, 1);
  19.  
  20. void setup(){
  21.  
  22. Serial.begin(115200);
  23.  
  24. pinMode(ECHO_PIN, INPUT);
  25. pinMode(TRIG_PIN, OUTPUT);
  26. pinMode(SOUND_PIN, OUTPUT);
  27. pinMode(BUTTON_PIN, INPUT);
  28. pinMode(RED_LIGHT, OUTPUT);
  29. pinMode(BLUE_LIGHT, OUTPUT);
  30.  
  31. lastToggle = millis();
  32. active = false;
  33.  
  34. lcd.begin(16,2);
  35. }
  36.  
  37. void skriv(){
  38. lcd.print("Ringer polis");
  39. lcd.clear();
  40. }
  41.  
  42. float getDistance(){
  43. digitalWrite(TRIG_PIN, LOW);
  44. delayMicroseconds(2);
  45. digitalWrite(TRIG_PIN, HIGH);
  46. delayMicroseconds(10);
  47. digitalWrite(TRIG_PIN, LOW);
  48.  
  49. double duration = pulseIn(ECHO_PIN, HIGH);
  50.  
  51. return(0.0343*duration/2);
  52. }
  53.  
  54. bool isButtonPressed(){
  55. if (digitalRead(BUTTON_PIN)){
  56. if (millis() - lastToggle > 250){
  57. lastToggle = millis();
  58. digitalWrite(RED_LIGHT, LOW);
  59. digitalWrite(BLUE_LIGHT, LOW);
  60. return(true);
  61. }
  62. }
  63. return(false);
  64. }
  65.  
  66. void alarm(){
  67.  
  68. lampTimer = millis();
  69. noTone(SOUND_PIN);
  70.  
  71. while (!isButtonPressed()){
  72. //ändra lampkod
  73. }
  74. }
  75.  
  76. void loop(){
  77.  
  78. if (getDistance() < 50 && active == false){
  79. tone(SOUND_PIN, 400);
  80. start = millis();
  81. active = true;
  82. }
  83.  
  84. while (active){
  85. if (millis() - start < 5000){
  86. if (isButtonPressed()){
  87. active = false;
  88. noTone(SOUND_PIN);
  89. delay(3000);
  90. break;
  91. }
  92. }
  93. if (millis() - start > 5000){
  94. active = false;
  95. alarm();
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment