Advertisement
Redxone

[Elegoo] Alarm system!

Oct 24th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include "IRremote.h"
  2.  
  3. int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
  4.  
  5. /*-----( Declare objects )-----*/
  6. IRrecv irrecv(receiver);     // create instance of 'irrecv'
  7. decode_results results;      // create instance of 'decode_results'
  8.  
  9. #define trig 3
  10. #define echo 2
  11. #define omergurd 4
  12. int range = 100;
  13. int onoff = 30;
  14. bool triggered = false;
  15. bool enabled = true;
  16.  
  17. void annoy(int freq, int dur)
  18. {
  19.    tone(omergurd,freq,dur);
  20.    delay(dur);
  21. }
  22.  
  23. void setup() {
  24.   // put your setup code here, to run once:
  25.   Serial.begin (9600);
  26.   pinMode(trig, OUTPUT);
  27.   pinMode(echo, INPUT);
  28.   irrecv.enableIRIn();
  29. }
  30.  
  31. void loop() {
  32.   if (irrecv.decode(&results)) // have we received an IR signal?
  33.   {
  34.     translateIR();
  35.     irrecv.resume(); // receive the next value
  36.   }  
  37.  
  38.   if(triggered)
  39.   {
  40.     annoy(4000,500);
  41.     exit;
  42.   }
  43.   // put your main code here, to run repeatedly:
  44.   int duration, distance;
  45.   digitalWrite(trig,HIGH);
  46.   delayMicroseconds(1000);
  47.   digitalWrite(trig,LOW);
  48.   duration = pulseIn(echo, HIGH);
  49.   distance = (duration/2) / 29.1;
  50.   Serial.print(distance);
  51.   Serial.print(" cm ");
  52.   if( distance < range && enabled && !triggered)
  53.   {
  54.     //annoy(map(distance, 0, range, 2093, 22));
  55.     annoy(20,2000);
  56.     triggered = true;
  57.     Serial.print(" TRIGGERED!");
  58.   }
  59.   Serial.print("\n");
  60.   delay(500);
  61. }
  62.  
  63. void enabledisable()
  64. {
  65.    onoff = 30;
  66.    if(!enabled){
  67.      Serial.println("DISABLED.");
  68.      onoff = 5000;
  69.    }
  70.    if(enabled){
  71.      Serial.println("ENABLED.");
  72.      onoff = 30;
  73.    }
  74.     annoy(onoff,500);
  75. }
  76. /*-----( Function )-----*/
  77. void translateIR() // takes action based on IR code received
  78. {
  79.   Serial.print(int(results.value));
  80.   if(int(results.value) == 9755)
  81.   {
  82.    if(triggered)
  83.    {
  84.      annoy(20,500);
  85.      annoy(20,500);
  86.      triggered=false;
  87.      delay(500);
  88.    }
  89.     enabled = !enabled;
  90.     enabledisable();
  91.   }
  92.   if(int(results.value) == 19227) // if we pressed Play/Pause button
  93.   {  
  94.     if(!triggered)
  95.     {
  96.        enabled = !enabled;
  97.        enabledisable();
  98.     }
  99.     else
  100.     {
  101.       delay(500);
  102.       annoy(20,500);
  103.       annoy(20,500);
  104.       triggered = false;
  105.     }
  106.   }
  107.   delay(500); // Do not get immediate repeat
  108. } //END translateIR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement