Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "IRremote.h"
- int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
- /*-----( Declare objects )-----*/
- IRrecv irrecv(receiver); // create instance of 'irrecv'
- decode_results results; // create instance of 'decode_results'
- #define trig 3
- #define echo 2
- #define omergurd 4
- int range = 100;
- int onoff = 30;
- bool triggered = false;
- bool enabled = true;
- void annoy(int freq, int dur)
- {
- tone(omergurd,freq,dur);
- delay(dur);
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin (9600);
- pinMode(trig, OUTPUT);
- pinMode(echo, INPUT);
- irrecv.enableIRIn();
- }
- void loop() {
- if (irrecv.decode(&results)) // have we received an IR signal?
- {
- translateIR();
- irrecv.resume(); // receive the next value
- }
- if(triggered)
- {
- annoy(4000,500);
- exit;
- }
- // put your main code here, to run repeatedly:
- int duration, distance;
- digitalWrite(trig,HIGH);
- delayMicroseconds(1000);
- digitalWrite(trig,LOW);
- duration = pulseIn(echo, HIGH);
- distance = (duration/2) / 29.1;
- Serial.print(distance);
- Serial.print(" cm ");
- if( distance < range && enabled && !triggered)
- {
- //annoy(map(distance, 0, range, 2093, 22));
- annoy(20,2000);
- triggered = true;
- Serial.print(" TRIGGERED!");
- }
- Serial.print("\n");
- delay(500);
- }
- void enabledisable()
- {
- onoff = 30;
- if(!enabled){
- Serial.println("DISABLED.");
- onoff = 5000;
- }
- if(enabled){
- Serial.println("ENABLED.");
- onoff = 30;
- }
- annoy(onoff,500);
- }
- /*-----( Function )-----*/
- void translateIR() // takes action based on IR code received
- {
- Serial.print(int(results.value));
- if(int(results.value) == 9755)
- {
- if(triggered)
- {
- annoy(20,500);
- annoy(20,500);
- triggered=false;
- delay(500);
- }
- enabled = !enabled;
- enabledisable();
- }
- if(int(results.value) == 19227) // if we pressed Play/Pause button
- {
- if(!triggered)
- {
- enabled = !enabled;
- enabledisable();
- }
- else
- {
- delay(500);
- annoy(20,500);
- annoy(20,500);
- triggered = false;
- }
- }
- delay(500); // Do not get immediate repeat
- } //END translateIR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement