I'm working on a script that uses the IRremote library https://github.com/shirriff/Arduino-IRremote I'm able to receive signals from my remote. But for some reason when I send a signal it behaves as if I'm sending that same signal over and over.
For example if I press the on/off button from the remote it toggles the pin on and off every half a second. The same is true if I press the up button which brings it from 0 to 255 in roughly half second increments. In both scenarios it goes on for infinity.
I've tried adding and removing curled braces with no luck. How can I turn the pins on and off with one signal from the remote?
#include <IRremote.h>
int RECV_PIN = A1; // IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;
int statLED1 = 5; //LED 1
int statLED2 = 3; //LED 2
int statLED3 = 9; //LED 3
int statLED_state1 = LOW; //mac
int statLED_state2 = LOW; //mac
int statLED_state3 = LOW; //mac
int pins[] = { // array of the pins
3, 5, 9};
int arrayLevel = 0;
void setup() {
pinMode(RECV_PIN, INPUT);
irrecv.enableIRIn(); // Start the receiver
pinMode(statLED1, OUTPUT);
pinMode(statLED2, OUTPUT);
pinMode(statLED3, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(statLED1, statLED_state1); //mac
digitalWrite(statLED2, statLED_state2); //mac
digitalWrite(statLED3, statLED_state3); //mac
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(9, LOW);
Serial.begin(9600);
Serial.println("Waiting: ");
}
void loop() {
irrecv.decode(&results);
unsigned long key = results.value; //Fetch the key
if(key != 0) //Ignore keys that are zero
{
Serial.println("Key Recieved: ");
Serial.println(key);
switch(key)
{
case 0xA55A21DE:
if(statLED_state1 != LOW)
statLED_state1 = LOW;
else
statLED_state1 = HIGH;
digitalWrite(statLED1, statLED_state1); //mac
Serial.println("On/Off");
break;
case 0xA55A01FE:
Serial.println("Brightening lights up");
arrayLevel = arrayLevel + 10;
if (arrayLevel < 0) arrayLevel = 0;
if (arrayLevel > 255) arrayLevel = 255;
arrayWriteAnalog(arrayLevel);
Serial.println(arrayLevel);
break;
case 0xA55A817E:
Serial.println("Dimming lights down");
arrayLevel = arrayLevel - 10;
if (arrayLevel < 0) arrayLevel = 0;
if (arrayLevel > 255) arrayLevel = 255;
arrayWriteAnalog(arrayLevel);
Serial.println(arrayLevel);
break;
}
delay(500);
}
}
void arrayWriteAnalog(int val) {
val = constrain(val, 0, 200);
for (int i = 0; i < sizeof(pins) / sizeof(int); i++) {
Serial.print("analogWrite ");
Serial.print(pins[i]);
Serial.print(" ");
Serial.println(val);
Serial.flush();
analogWrite(pins[i], val);
}
}
void arrayWrite(int pins[], int val) {
for (int i = 0; i <= (sizeof(pins)/sizeof(int)); i++) {
digitalWrite(pins[i], val);
}
}
getting closer. It's no longer executing for infinity but when I send a signal it executes it twice, so if I send the on/off code it blinks on and off.
void loop() {
unsigned long key=0;
if(irrecv.decode(&results))
{key = results.value; //Fetch the key
irrecv.resume(); // Receive the next value
}
if(key != 0) //Ignore keys that are zero
{
Serial.println("Key Recieved: ");
Serial.println(key);
switch(key)
{
case 0xA55A21DE:
if(statLED_state1 != LOW)
statLED_state1 = LOW;
else
statLED_state1 = HIGH;
digitalWrite(statLED1, statLED_state1); //mac
Serial.println("On/Off");
break;
case 0xA55A01FE:
Serial.println("Brightening lights up");
arrayLevel = arrayLevel + 10;
if (arrayLevel < 0) arrayLevel = 0;
if (arrayLevel > 255) arrayLevel = 255;
arrayWriteAnalog(arrayLevel);
Serial.println(arrayLevel);
break;
case 0xA55A817E:
Serial.println("Dimming lights down");
arrayLevel = arrayLevel - 10;
if (arrayLevel < 0) arrayLevel = 0;
if (arrayLevel > 255) arrayLevel = 255;
arrayWriteAnalog(arrayLevel);
Serial.println(arrayLevel);
break;
}
delay(500);
}
}[\code]
Or you could eliminate the delay, and not do anything as long as the received code is the same as the last received code, within a reasonable timeout. (except where you may want an auto-repeat, like for fade up/down)