So I've been "developing" this sketch over a number of years that reads IR signals to control LEDs around the house. I've successfully installed this code on several sets of hardware and now I've pulled it out of the archive and I'm trying again. However this time around when I compile and upload it to an Uno it only recognizes one signal, "1302" or what I describe in the sketch as "Channel 1".
I know the remote is working because I'm in a room where I have an older version of the system and it's reading the signals just fine.
I have another IR receiver so I swapped it out get the same behavior.
I'm running Arduino IDE on a new laptop running Windows 10; it was a pain in the butt to get running properly. I don't recall what version I was running on my old laptop. I'm currently running 1.5.8 Beta Could this be a cause of the sketch not running properly?
Below is the code. Note the function at the bottom that reads the IR signal, perhaps there is something wrong here?
Any suggestions are appreciated.
Thanks!
Rich
/*
int irPin = A4; //Sensor pin 1 wired to Arduino's pin A4
int statLED1 = 3; //Toggle the status LED every time Power is pressed
int statLED2 = 6; //Toggle the status LED every time Power is pressed
int statLED3 = 5; //Toggle the status LED every time Power is pressed
int statLED4 = 9; //Toggle the status LED every time Power is pressed
int statLED5 = 10; //Toggle the status LED every time Power is pressed
int statLED6 = 11; //Toggle the status LED every time Power is pressed
int statLED7 = 4;
int allPins[] = {3,5, 6, 9, 10, 11};
int statLED_state1 = LOW; //mac
int statLED_state2 = LOW; //mac
int statLED_state3 = LOW; //mac
int statLED_state4 = LOW; //mac
int statLED_state5 = LOW; //mac
int statLED_state6 = LOW; //mac
int statLED_state7 = LOW; //mac
int arrayPins_state = LOW;
int ledLevel = 0;
int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)
void setup() {
pinMode(statLED1, OUTPUT);
pinMode(statLED2, OUTPUT);
pinMode(statLED3, OUTPUT);
pinMode(statLED4, OUTPUT);
pinMode(statLED5, OUTPUT);
pinMode(statLED6, OUTPUT);
pinMode(statLED7, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(statLED1, statLED_state1); //mac
digitalWrite(statLED2, statLED_state2); //mac
digitalWrite(statLED3, statLED_state3); //mac
digitalWrite(statLED4, statLED_state4); //mac
digitalWrite(statLED5, statLED_state5); //mac
digitalWrite(statLED6, statLED_state6); //macc
digitalWrite(statLED7, statLED_state7); //macc
pinMode(irPin, INPUT);
Serial.begin(9600);
Serial.println("Waiting: ");
}
void loop() {
int key = getIRKey(); //Fetch the key
if(key != 0) //Ignore keys that are zero
{
Serial.print("Key Recieved: ");
Serial.print(key);
switch(key)
{
case 1302:
Serial.print("Circuit 1");
if(statLED_state1 != LOW) //This toggles the statLED every time power button is hit
statLED_state1 = LOW;
else
statLED_state1 = HIGH;
digitalWrite(statLED1, statLED_state1); //mac
delay(250);
break;
case 1281:
Serial.print("Circuit 2");
if(statLED_state2 != LOW) //This toggles the statLED every time power button is hit
statLED_state2 = LOW;
else
statLED_state2 = HIGH;
digitalWrite(statLED2, statLED_state2); //mac
delay(250);
break;
case 1282:
Serial.print("Circuit 3");
if(statLED_state3 != LOW) //This toggles the statLED every time power button is hit
statLED_state3 = LOW;
else
statLED_state3 = HIGH;
digitalWrite(statLED3, statLED_state3); //mac
delay(250);
break;
case 1283:
Serial.print("Circuit 4");
if(statLED_state4 != LOW) //This toggles the statLED every time power button is hit
statLED_state4 = LOW;
else
statLED_state4 = HIGH;
digitalWrite(statLED4, statLED_state4); //mac
delay(250);
break;
case 1284:
Serial.print("Circuit 5");
if(statLED_state5 != LOW) //This toggles the statLED every time power button is hit
statLED_state5 = LOW;
else
statLED_state5 = HIGH;
digitalWrite(statLED5, statLED_state5); //mac
delay(250);
break;
case 1285:
Serial.print("Circuit 6");
if(statLED_state6 != LOW) //This toggles the statLED every time power button is hit
statLED_state6 = LOW;
else
statLED_state6 = HIGH;
digitalWrite(statLED6, statLED_state6); //mac
delay(250);
break;
case 1289: // button 0 on the remote. This does PWM
Serial.println("All on All off");
arrayWrite(allPins, HIGH); //mac
delay(250);
break;
case 1288: // button 9 on the remote. This does PWM
Serial.print("Down Circuit 1");
ledLevel = ledLevel - 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED1, ledLevel);
break;
case 1286: // button 7 on the remote. This does PWM
Serial.print("Down Circuit 6");
ledLevel = ledLevel - 2;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED6, ledLevel);
break;
case 1287:// button 8 on the remote. This does PWM
Serial.print("Down Circuit 2");
ledLevel = ledLevel - 2;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED2, ledLevel);
break;
case 1330: // play button on the remote. This does PWM
Serial.print("Up Circuit 1");
ledLevel = ledLevel + 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED1, ledLevel);
break;
case 1337: // pause button on the remote
Serial.print("Down Circuit 1");
ledLevel = ledLevel - 1;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED1, ledLevel);
break;
case 1379: // button 9 on the remote. This does PWM
Serial.print("Up Circuit 3");
ledLevel = ledLevel + 3;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED3, ledLevel);
break;
case 1380:
Serial.print("Down Circuit 3");
ledLevel = ledLevel - 1;
if (ledLevel > 0) ledLevel = 0;
analogWrite(statLED3, ledLevel);
break;
Serial.print(key);
}
Serial.println();
}
}
int getIRKey() {
int data[12];
int i;
while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
for(i = 0 ; i < 11 ; i++)
data[i] = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses
for(i = 0 ; i < 11 ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
else
return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
}
int result = 0;
for(i = 0 ; i < 11 ; i++) //Convert data bits to integer
if(data[i] == 1) result |= (1<<i);
return result; //Return key number
}
void arrayWrite(int pins[], int val) {
for (int i = 0; i == (sizeof(pins)/sizeof(int)); i++) {
digitalWrite(pins[i], val);
}
}