Hi all, I'm new to the Arduino world. I'm trying to replicate with it the functions of my RGB controller.
I've managed to map some functions (change colours) to the remote control.
I'm trying to replicate the Fade 7 function but I can't figure out how to exit from it while it is running (endless loop) when another button is pressed.
Can you help me out? Here is the code:
#include "IRremote.h"
int pinRic = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
IRrecv ricevitore(pinRic); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
#define BLUE 3
#define GREEN 5
#define RED 6
#define IR_BPlus 0xFF3AC5 //
#define IR_BMinus 0xFFBA45 //
#define IR_ON 0xFF827D //
#define IR_OFF 0xFF02FD //
#define IR_R 0xFF1AE5 //
#define IR_G 0xFF9A65 //
#define IR_B 0xFFA25D //
#define IR_FADE7 0xFFE01F //
#define incFact 30
int brightFact = 0;
int lv[3];
void fade7()
{
#define delayTime 10 // fading time between colors
lv[0] = 255; // choose a value between 1 and 255 to change the color.
lv[1] = 0;
lv[2] = 0;
while(true)
{
for(int i = 0; i < 255; i += 1) // fades out R bring G full when i=255
{
lv[0] -= 1;
lv[1] += 1;
analogWrite(RED, lv[0]);
analogWrite(GREEN, lv[1]);
delay(delayTime);
}
lv[0] = 0;
lv[1] = 255;
lv[2] = 0;
for(int i = 0; i < 255; i += 1) // fades out G bring B full when i=255
{
lv[1] -= 1;
lv[2] += 1;
analogWrite(GREEN, lv[1]);
analogWrite(BLUE, lv[2]);
delay(delayTime);
}
lv[0] = 0;
lv[1] = 0;
lv[2] = 255;
for(int i = 0; i < 255; i += 1) // fades out B bring R full when i=255
{
lv[2] -= 1;
lv[0] += 1;
analogWrite(BLUE, lv[2]);
analogWrite(RED, lv[0]);
delay(delayTime);
}
}
}
void setColor(int r, int g, int b)
{
Serial.println("About to write:");
Serial.println(r);
Serial.println(g);
Serial.println(b);
analogWrite(RED,r);
analogWrite(GREEN,g);
analogWrite(BLUE,b);
}
void translateIR() // takes action based on IR code received
// describing Remote IR codes
{
Serial.println(results.value, HEX);
switch(results.value)
{
case IR_OFF: setColor(0,0,0);break;
//COLORS
case IR_R: setColor(255,0,0);break;
case IR_G: setColor(0,255,0);break;
case IR_B: setColor(0,0,255);break;
//END COLORS
case IR_FADE7: fade7();
}// End Case
delay(100); // Do not get immediate repeat
} //END translateIR
void setup() /*----( SETUP: RUNS ONCE )----*/
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
ricevitore.enableIRIn(); // Start the receiver*/
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (ricevitore.decode(&results)) // have we received an IR signal?
{
translateIR();
ricevitore.resume(); // receive the next value*/
}
} /*--(end main loop )-- */