Advertisement
RobinBrusbo

Med display

Jan 20th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 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.     bool redOn = false;
  70.    
  71.     while (!isButtonPressed()){
  72.         noTone(SOUND_PIN);
  73.         if (millis() - lampTimer > LIGHT_BLUE_TIME && redOn == false){
  74.             redOn = true;
  75.             digitalWrite(RED_LIGHT, HIGH);
  76.             digitalWrite(BLUE_LIGHT, LOW);
  77.             lampTimer = millis();
  78.             LIGHT_RED_TIME = getDistance() * 10;
  79.         }
  80.         if (millis() - lampTimer > LIGHT_RED_TIME && redOn == true){
  81.             redOn = false;
  82.             digitalWrite(RED_LIGHT, LOW);
  83.             digitalWrite(BLUE_LIGHT, HIGH);
  84.             lampTimer = millis();
  85.             LIGHT_BLUE_TIME = getDistance() * 10;
  86.         }
  87.         skriv();
  88.     }
  89. }
  90.  
  91. void loop(){
  92.  
  93.     if (getDistance() < 50 && active == false){
  94.         tone(SOUND_PIN, 400);
  95.         start = millis();
  96.         active = true;
  97.     }
  98.    
  99.     while (active){
  100.         if (millis() - start < 5000){
  101.             if (isButtonPressed()){
  102.                 active = false;
  103.                 noTone(SOUND_PIN);
  104.                 delay(3000);
  105.                 break;
  106.             }
  107.         }
  108.         if (millis() - start > 5000){
  109.             active = false;
  110.             alarm();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement