RobinBrusbo

JÄTTEBÄSTA KODEN WTF

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