Stymied by a single int variable

I’m trying to use an int variable to index an array. When I hard code the same value as the variable the function call works properly, but when I use the variable I get a drastically different result.

#include <Arduino.h>
#include <IRremote.h>

#define IR_SEND_PIN 3
const int khz PROGMEM = 38;

const unsigned int PowerOff[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405};
const unsigned int MaxCool[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405};
const unsigned int CoolFanMax[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405};
const unsigned int CoolFanHigh[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405};
const unsigned int CoolFanMid[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405};
const unsigned int CoolFanLow[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405};
const unsigned int CoolFanSleep[] PROGMEM = {2900, 1800, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 405, 1125, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 1125, 405, 405, 405, 405, 405, 405, 405, 405, 405};

const unsigned int* const CoolMode[] PROGMEM = {PowerOff, CoolFanSleep, CoolFanLow, CoolFanMid, CoolFanHigh, CoolFanMax, MaxCool};

const size_t signalLength PROGMEM = 227;

int CoolSetting = 5;

void setup() {
  //For some reason this won't work
  sendRAW_Flash(CoolMode[CoolSetting], signalLength, khz);
  //But hard coding the index does, I can't figure out why
  sendRAW_Flash(CoolMode[5], signalLength, khz);
}

void loop() {}

void sendRAW_Flash(const unsigned int * const signalArrayPtr, size_t signalLength, unsigned char carrierFreq) {

  IrSender.enableIROut(carrierFreq); //initialise the carrier frequency for each signal to be sent

  for (size_t i = 0; i < signalLength; i++) {
    if (i & 1) IrSender.space(pgm_read_word_near(signalArrayPtr + i));
    else IrSender.mark(pgm_read_word_near(signalArrayPtr + i));
  }
  IrSender.space(1);//make sure IR is turned off at end of signal
}

3 posts were merged into an existing topic: Need help with Multi-Dimensional Arrays