iMac Apple remote ir decoding

0

I am trying to modify an Arduino sketch to use an old Apple remote IR transmitter. It works, and I have a list of the HEX codes for the various buttons. What is confusing me is that the sketch won't compile with the HEX codes included, but will do so if I convert them to DEC equivalent. And, the Serial outputs as defined in sketch lines 102 to 126 work, but the LEDs do not seem to perform as suggested. I don't know if it is tied to the HEX/DEC issue, or where to look. The code, as it now stands, is below. It includes comments referring to the remote's frequency , which I have not addressed. Thanks for helping me to understand this.

 /* 
   This sketch uses Ken Shirriff's *awesome* IRremote library:
       https://github.com/shirriff/Arduino-IRremote

   Hardware setup:
     * The output of an IR Receiver Diode (38 kHz demodulating
       version) should be connected to the Arduino's pin 11.
       * The IR Receiver diode should also be powered off the
         Arduino's 5V and GND rails.
     * A common cathode RGB LED is connected to Arduino's pins 
       5, 9, and 6 (red, green, and blue pins).
 */

#include <IRremote.h> // Include the IRremote library

/* Setup constants for SparkFun's IR Remote: */
#define NUM_BUTTONS 6 // The remote has 6 buttons
/* Define the IR remote button codes. We're only using the
   least signinficant two bytes of these codes. Each one 
   should actually have 0x10EF in front of it. Find these codes
   by running the IRrecvDump example sketch included with
   the IRremote library.*/
const uint16_t BUTTON_PLUS = 2011254893; // i.e. 0x10EFD827
const uint16_t BUTTON_MINUS = 2011246701;
const uint16_t BUTTON_LEFT = 2011271277;
const uint16_t BUTTON_RIGHT = 2011258989;
const uint16_t BUTTON_MENU = 2011283565;
const uint16_t BUTTON_STARTSTOP = 2011275373;
//const uint16_t BUTTON_LEFT = 0x10EF;
//const uint16_t BUTTON_RIGHT = 0x807F;
//const uint16_t BUTTON_CIRCLE = 0x20DF;

/* Connect the output of the IR receiver diode to pin 11. */
int RECV_PIN = 11;
/* Initialize the irrecv part of the IRremote  library */
IRrecv irrecv(RECV_PIN);
decode_results results; // This will store our IR received codes
uint16_t lastCode = 0; // This keeps track of the last code RX'd

/* Setup RGB LED pins: */
enum ledOrder // Make an enum to add some clarity in the code
{
  RED,   // 0
  GREEN, // 1
  BLUE   // 2
};
const int rgbPins[3] = {5, 9, 6}; // Red, green, blue pins respectively
byte rgbValues[3] = {55, 23, 200}; // This keeps track of channel brightness
byte activeChannel = RED; // Start with RED as the active channel
boolean ledEnable = 1; // Start with the LED on.

void setup()
{
  Serial.begin(9600); // Use serial to debug. 
  irrecv.enableIRIn(); // Start the receiver

  /* Set up the RGB LED pins: */
  for (int i=0; i<3; i++)
  {
    pinMode(rgbPins[i], OUTPUT);
    analogWrite(rgbPins[i], rgbValues[i]);
  }
}

// loop() constantly checks for any received IR codes. At the
// end it updates the RGB LED.
void loop() 
{
  if (irrecv.decode(&results)) 
  {
    /* read the RX'd IR into a 16-bit variable: */
    uint16_t resultCode = (results.value & 65535); //0xFFFF

    /* The remote will continue to spit out 0xFFFFFFFF if a 
     button is held down. If we get 0xFFFFFFF, let's just
     assume the previously pressed button is being held down */
    if (resultCode == 65535) //0xFFFF
      resultCode = lastCode;
    else
      lastCode = resultCode;

    // This switch statement checks the received IR code against
    // all of the known codes. Each button press produces a 
    // serial output, and has an effect on the LED output.
    switch (resultCode)
    {
      case BUTTON_PLUS:
        Serial.println("+");
        if (ledEnable) ledEnable = 0;
        else ledEnable = 1; // Flip ledEnable
        break;
      case BUTTON_MINUS:
        Serial.println("-");
        activeChannel = RED;
        break;
      case BUTTON_LEFT:
        Serial.println("<-");
        activeChannel = GREEN;
        break;
      case BUTTON_RIGHT:
        Serial.println("->");
        activeChannel = BLUE;
        break;
      case BUTTON_MENU:
        Serial.println("Menu");
        rgbValues[activeChannel]++; // Increment brightness
        break;
      case BUTTON_STARTSTOP:
        Serial.println("-> =");
        rgbValues[activeChannel]--; // Decrement brightness
        break;
//      case BUTTON_LEFT:
//        Serial.println("Left");
//        rgbValues[activeChannel] = 0; // Min brightness (off)
//        break;
//      case BUTTON_RIGHT:
//        Serial.println("Right");
//        rgbValues[activeChannel] =  255; // Max brightness
//        break;
//      case BUTTON_CIRCLE:
//        Serial.println("Circle");
//        rgbValues[activeChannel] = 127; // Medium brightness
//        break;
      default:
        Serial.print("Unrecognized code received: 0x");
        Serial.println(results.value, HEX);
        break;        
    }    
    irrecv.resume(); // Receive the next value
  }

  // Every time through the loop, update the RGB LEDs:
  if (ledEnable)
  {
    for (int i=0; i<3; i++)
    {
      analogWrite(rgbPins[i], rgbValues[i]);
    }
  }
  else
  {
    for (int i=0; i<3; i++)
    {
      analogWrite(rgbPins[i], 0);
    }
  }
}
arduino
asked on Stack Overflow Jun 22, 2017 by user65118

1 Answer

0

If you say your code does not compile if you use hex notation for those numbers it would help to provide the actual code that does not compile, because the code you posted here compiles even if I enter hex numbers instead of decimals.

As gre_gor already pointed out in his comment you also have a problem with your values.

const uint16_t BUTTON_PLUS = 2011254893;

Here you're trying to store 2011254893 in a 16bit unsigned integer.

If all 16 bits are 1 you end up with 2^16 -1 which is 65535. So that is the maximum number you can store in a variable of type uint16_t.

If you assign larger values to that variable you will cause a so called integer overflow. The actual value stored in your variable will be 2011244893 modulus 65536, which is 20589. That's not the value you were supposed to assign.

If you read the comments in that code carefully:

Define the IR remote button codes. We're only using the least signinficant two bytes of these codes. Each one should actually have 0x10EF in front of it.

Also read this on integer overflow and I guess it wouldn't hurt if you make your self familiar with data types in general.

https://en.wikipedia.org/wiki/Integer_overflow

http://www.cplusplus.com/articles/DE18T05o/

answered on Stack Overflow Jun 22, 2017 by Piglet • edited Jul 20, 2018 by Flimzy

User contributions licensed under CC BY-SA 3.0