Advertisement
Guest User

unixtippse

a guest
Apr 10th, 2010
3,812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. // Simple raw infrared replay for IRremote.h
  2. //
  3. // Martin Schmitt <unixtippse at gmail dot com>
  4. //
  5. // IR LED is on PIN 3
  6. // driven by ULN 2803 with 5R8 series resistor
  7.  
  8. #include <IRremote.h>
  9. #define ON         11
  10. #define OFF        12
  11. #define ACT        13
  12. #define DEBOUNCE   50
  13.  
  14. IRsend irsend;
  15.  
  16. unsigned int on[129] = {
  17.   7800, 4200, 400, 2050, 850, 1550, 400, 2050, 900, 1600, 900, 1550, 400, 2050, 900, 1550,
  18.   900, 1600, 850, 1600, 400, 2000, 900, 1550, 900, 1600, 850, 1600, 850, 1600, 850, 1550,
  19.   450, 2050, 900, 1550, 850, 1600, 850, 1550, 900, 1600, 900, 1550, 850, 1600, 850, 1600,
  20.   850, 1600, 900, 1550, 850, 1600, 850, 1600, 850, 1600, 900, 1550, 900, 1550, 850, 1600,
  21.   850, 1600, 900, 1550, 900, 1550, 850, 1600, 850, 1600, 900, 1550, 900, 1550, 850, 1600,
  22.   850, 1600, 900, 1550, 900, 1550, 900, 1550, 850, 1600, 900, 1550, 900, 1550, 900, 1550,
  23.   850, 1600, 900, 1550, 900, 1550, 900, 1550, 850, 1650, 850, 1550, 900, 1550, 900, 1500,
  24.   900, 1650, 850, 1550, 900, 1550, 900, 1550, 400, 2100, 400, 2050, 850, 1550, 400, 2050,
  25.   400 };
  26. unsigned int off[129]  = {
  27.   7750, 4200, 400, 2050, 900, 1550, 400, 2050, 900, 1600, 850, 1600, 400, 2050, 850, 1550,
  28.   900, 1600, 850, 1600, 850, 1600, 850, 1550, 900, 1600, 850, 1600, 850, 1600, 850, 1550,
  29.   450, 2050, 900, 1550, 850, 1600, 850, 1550, 900, 1600, 900, 1550, 850, 1600, 850, 1600,
  30.   850, 1600, 900, 1550, 850, 1600, 850, 1600, 850, 1600, 900, 1550, 900, 1550, 850, 1600,
  31.   850, 1600, 900, 1550, 900, 1500, 900, 1600, 850, 1600, 900, 1550, 900, 1550, 850, 1600,
  32.   850, 1600, 900, 1550, 900, 1550, 900, 1550, 850, 1600, 900, 1550, 900, 1550, 850, 1600,
  33.   850, 1600, 900, 1550, 900, 1550, 900, 1550, 850, 1650, 850, 1550, 900, 1550, 900, 1550,
  34.   850, 1650, 850, 1550, 450, 2000, 900, 1550, 400, 2100, 400, 2050, 850, 1600, 400, 2050,
  35.   400 };
  36.  
  37. // Button states and debouncing
  38. // Inverted input logic, so we can use internal pull-up
  39. long last_statechange_on;
  40. long last_statechange_off;
  41.  
  42. int state_on  = HIGH;
  43. int state_off = HIGH;
  44.  
  45. int oldstate_on = HIGH;
  46. int oldstate_off = HIGH;
  47.  
  48. void setup() {
  49.   pinMode(ACT, OUTPUT);
  50.   pinMode(ON,  INPUT);
  51.   pinMode(OFF, INPUT);
  52.   // Activate internal pull-up for inputs
  53.   // Observe that this inverts button press logic. Don't be confused. ;-)
  54.   digitalWrite(ON,  HIGH);
  55.   digitalWrite(OFF, HIGH);
  56. }
  57.  
  58. void loop() {
  59.   int reading;
  60.   long now = millis();
  61.  
  62.   // Track state of ON button & debounce
  63.   reading = digitalRead(ON);
  64.   if(reading != oldstate_on){
  65.     last_statechange_on = now;
  66.   }
  67.   if (now - last_statechange_on > DEBOUNCE){
  68.       state_on = reading;
  69.   }
  70.   oldstate_on = reading;
  71.  
  72.   // Track state of OFF button & debounce
  73.   reading = digitalRead(OFF);
  74.   if(reading != oldstate_off){
  75.     last_statechange_off = now;
  76.   }
  77.   if (now - last_statechange_off > DEBOUNCE){
  78.       state_off = reading;
  79.   }
  80.   oldstate_off = reading;
  81.  
  82.   if ((state_on == LOW) and (state_off == HIGH)){
  83.     // Send ON code
  84.     digitalWrite(ACT, HIGH);
  85.     irsend.sendRaw(on, 129, 38);
  86.     digitalWrite(ACT, LOW);
  87.   }
  88.   if ((state_on == HIGH) and (state_off == LOW)){
  89.     // Send OFF code
  90.     digitalWrite(ACT, HIGH);
  91.     irsend.sendRaw(off, 129, 38);
  92.     digitalWrite(ACT, LOW);
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement