Advertisement
Guest User

Arduino A/C #2

a guest
Sep 21st, 2013
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. //*****************************************
  2. // NEC (Japanese) Infrared code sending library for the Arduino
  3. // Send a standard NEC 4 byte protocol direct to an IR LED on the define pin
  4. // Assumes an IR LED connected on I/O pin to ground, or equivalent driver.
  5. // Tested on a Freetronics Eleven Uno compatible
  6. // Written by David L. Jones www.eevblog.com
  7. // Youtube video explaining this code: http://www.youtube.com/watch?v=BUvFGTxZBG8
  8. // License: Creative Commons CC BY
  9. //*****************************************
  10.  
  11. #define IRLEDpin  3              //the arduino pin connected to IR LED to ground. HIGH=LED ON
  12. #define BITtime   562            //length of the carrier bit in microseconds
  13. //put your own code here - 4 bytes (ADDR1 | ADDR2 | COMMAND1 | COMMAND2)
  14. unsigned long IRcode=0xB335A962;
  15.  
  16. // SOME CODES:
  17. // Canon WL-D89 video remote START/STOP button = 0b11000001110001111100000000111111
  18.  
  19. void setup()
  20. {
  21. }
  22.  
  23. void IRsetup(void)
  24. {
  25.   pinMode(IRLEDpin, OUTPUT);
  26.   digitalWrite(IRLEDpin, LOW);    //turn off IR LED to start
  27. }
  28.  
  29. // Ouput the 38KHz carrier frequency for the required time in microseconds
  30. // This is timing critial and just do-able on an Arduino using the standard I/O functions.
  31. // If you are using interrupts, ensure they disabled for the duration.
  32. void IRcarrier(unsigned int IRtimemicroseconds)
  33. {
  34.   for(int i=0; i < (IRtimemicroseconds / 26); i++)
  35.     {
  36.     digitalWrite(IRLEDpin, HIGH);   //turn on the IR LED
  37.     //NOTE: digitalWrite takes about 3.5us to execute, so we need to factor that into the timing.
  38.     delayMicroseconds(9);          //delay for 13us (9us + digitalWrite), half the carrier frequnecy
  39.     digitalWrite(IRLEDpin, LOW);    //turn off the IR LED
  40.     delayMicroseconds(9);          //delay for 13us (9us + digitalWrite), half the carrier frequnecy
  41.     }
  42. }
  43.  
  44. //Sends the IR code in 4 byte NEC format
  45. void IRsendCode(unsigned long code)
  46. {
  47.   //send the leading pulse
  48.   IRcarrier(9000);            //9ms of carrier
  49.   delayMicroseconds(4500);    //4.5ms of silence
  50.  
  51.   //send the user defined 4 byte/32bit code
  52.   for (int i=0; i<32; i++)            //send all 4 bytes or 32 bits
  53.     {
  54.     IRcarrier(BITtime);               //turn on the carrier for one bit time
  55.     if (code & 0x80000000)            //get the current bit by masking all but the MSB
  56.       delayMicroseconds(3 * BITtime); //a HIGH is 3 bit time periods
  57.     else
  58.       delayMicroseconds(BITtime);     //a LOW is only 1 bit time period
  59.      code<<=1;                        //shift to the next bit for this byte
  60.     }
  61.   IRcarrier(BITtime);                 //send a single STOP bit.
  62. }
  63.  
  64. void loop()                           //some demo main code
  65. {
  66.   IRsetup();                          //Only need to call this once to setup
  67.   IRsendCode(IRcode);                
  68.   delay(20);
  69.   IRsendCode(~IRcode);
  70.   delay(20);  
  71.   IRsendCode(IRcode);
  72.   while(1);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement