Go Down

Topic: LED fade will not listen to IR input for interupt. (Read 3304 times) previous topic - next topic

MrKovacic


I'll start by saying this, im pretty good with Autoit, but a baby when it comes to C++..

I have a decent script set up to blink LED's using an old blu-ray player remote. As it stands, the remote has numbered buttons, volume buttons etc.. It also has a red, green, blue and yellow button. I have it so if I push the R, G or B button, the LED of that color lights up. If I push Yellow, it shuts everything off. I am trying to incorperate fading into this, and while it will being and run the fade, it will no longer pay attention to the IR remote during the fade. I tried removing all delays, and even adding in some calls to read the IR, but no matter what I do, the Fade codes stay going until I reset the Arduino.

I have a simple blink on button 2, a fairly simple fade using cos on button  1, and a more advanced fade on button 7.

While any of the fades are going, I can't get Arduino to check the IR for changes.

here is the code:
Code: [Select]

// include some junk
#include <IRremote.h>


int RECV_PIN = 11;
int periode = 2000;
int displace = 500;
int testtime = 1000;
long time=0;
/*  FUTURE USE
int RED1 = 2;
int GREEN1 = 3;
int BLUE1 = 4;

int RED2 = 5;
int GREEN2 = 6;
int BLUE2 = 7;

int RED3 = 8;
int GREEN3 = 9;
int BLUE3 = 10;

int RED4 = 11;
int GREEN4 = 12;
int BLUE4 = 13;
*/
int redPin = 7;   // Red LED,   connected to digital pin 9
int grnPin = 6;  // Green LED, connected to digital pin 10
int bluPin = 8;  // Blue LED,  connected to digital pin 11

int value, value2, value3 ;

int RED = 7;
int GREEN = 6;
int BLUE = 8;

// Color arrays
int black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 100, 100, 0 };
int pink[3] = { 0, 100, 100 };
int magenta[3] = { 100, 0, 100 };

// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];


//int wait = 2;      // no longer used

int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
IRrecv irrecv(RECV_PIN);
decode_results results;
long previousMillis = 0;   
long interval = 1000;
long chill = 2;

int ledState = LOW; // only for BLUE LED for now, will add more later, just want to blink blue for now
int blinkone = 0;
int blinktwo= 0;
int blinkthree = 0;
int blinkfour = 0;
int blinkfive = 0;
int blinksix = 0;
int blinkseven = 0;
int blinkeight = 0;
int blinknine = 0;


void setup() //Just some obvious crap
{
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  irrecv.enableIRIn(); // kick on the IR receiver
}



void loop() {
  unsigned long currentMillis = millis(); // Start the arduino counting so it can keep time
  if (irrecv.decode(&results)) {
    if (results.value == 0x66B47) { // BLUE button on the remote
      digitalWrite(BLUE, HIGH);
    }
    else if (results.value == 0xE6B47) { // RED button on the remote
      digitalWrite(RED, HIGH);
    }   
   
        else if (results.value == 0x490) { // Vol Up button on the remote
      interval = interval - 50;
      if  (interval < 75) {
            interval =100;
                           }
    } 
   
        else if (results.value == 0xC90) { // Vol Down button on the remote
            interval = interval + 50;
    } 
   
            else if (results.value == 0x290) { // Vol Down button on the remote
            interval = 1000;
    } 
   
        else if (results.value == 0x16B47) { // GREEN button on the remote
      digitalWrite(GREEN, HIGH);
    }
   
        else if (results.value == 0xB47) { // 1 button on the remote
  while (results.value == 0xB47) {
  time = millis();
  value = 128+127*cos(2*PI/periode*time);
  value2 = 128+127*cos(2*PI/periode*(displace-time));
  value3 = 128+127*cos(2*PI/periode*(testtime-time));
  analogWrite(redPin, value);           // sets the value (range from 0 to 255)
  analogWrite(grnPin, value2);           // sets the value (range from 0 to 255)
  analogWrite(bluPin, value3);
  irrecv.resume();
        }}
       
       
        else if (results.value == 0x80B47) { // 2 button on the remote

blinkone = 1;
blinktwo= 0;
blinkthree = 0;
blinkfour = 0;
blinkfive = 0;
blinksix = 0;
blinkseven = 0;
blinkeight = 0;
blinknine = 0;
        }        else if (results.value == 0x40B47) { // 3 button on the remote

blinkone = 0;
blinktwo= 0;
blinkthree = 1;
blinkfour = 0;
blinkfive = 0;
blinksix = 0;
blinkseven = 0;
blinkeight = 0;
blinknine = 0;
        }        else if (results.value == 0xC0B47) { // 4 button on the remote

blinkone = 0;
blinktwo= 0;
blinkthree = 0;
blinkfour = 1;
blinkfive = 0;
blinksix = 0;
blinkseven = 0;
blinkeight = 0;
blinknine = 0;
        }        else if (results.value == 0x20B47) { // 5 button on the remote

blinkone = 0;
blinktwo= 0;
blinkthree = 0;
blinkfour = 0;
blinkfive = 1;
blinksix = 0;
blinkseven = 0;
blinkeight = 0;
blinknine = 0;
        }        else if (results.value == 0xA0B47) { // 6 button on the remote

blinkone = 0;
blinktwo= 0;
blinkthree = 0;
blinkfour = 0;
blinkfive = 0;
blinksix = 1;
blinkseven = 0;
blinkeight = 0;
blinknine = 0;
        }        else if (results.value == 0x60B47) { // 7 button on the remote
while (results.value == 0x60B47) {

  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);
  crossFade(white);
  crossFade(pink);
  crossFade(magenta);
  }

        }        else if (results.value == 0xE0B47) { // 8 button on the remote

blinkone = 0;
blinktwo= 0;
blinkthree = 0;
blinkfour = 0;
blinkfive = 0;
blinksix = 0;
blinkseven = 0;
blinkeight = 1;
blinknine = 0;
        }        else if (results.value == 0x10B47) { // 9 button on the remote

blinkone = 0;
blinktwo= 0;
blinkthree = 0;
blinkfour = 0;
blinkfive = 0;
blinksix = 0;
blinkseven = 0;
blinkeight = 0;
blinknine = 1;
        }
       
       
       
       

        else if (results.value == 0x96B47  ) { // YELLOW - shut everything off. aka blackout
      digitalWrite(BLUE, LOW);
      digitalWrite(RED, LOW);
      digitalWrite(GREEN, LOW);
      blinkone = 0; // stops that pesky blue light from blinking like a little bitch

    }
    irrecv.resume(); //doesnt matter where you put this, it workd right here so I guess dont mess with it
  }
 
 
 
  if (blinkone == 1) {   // First flash pattern. On and off, bow to its awesomeness!
     
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    digitalWrite(BLUE, ledState);
      }   
     }   //include this
 
}


int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?
  if (step) {                      // If its non-zero,
    step = 1020/step;              //   divide by 1020
  }
  return step;
}

int calculateVal(int step, int val, int i) {

  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;           
    }
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    }
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  }
  else if (val < 0) {
    val = 0;
  }
  return val;
}




void crossFade(int color[3]) {
    unsigned long currentMillis = millis();
  // Convert to 0-255
  int R = (color[0] * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G);
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);     
    analogWrite(bluPin, bluVal);
   
int zag = 1;
do {
zag = zag + 1;
getIR();
} while (zag < 200);

     
     


  }
  // Update current values for next loop
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;

}

void getIR() {
IRrecv irrecv(RECV_PIN);
decode_results nresults;
}


Any help would be very appreciated! :smiley-eek:

Arrch

Code: [Select]

void getIR() {
IRrecv irrecv(RECV_PIN);
decode_results nresults;
}


I assume this is what you think is checking for IR commands while you are fading? It's not, it's declaring two local variables, then destroying them. This goes back to the very first response in this thread made by Nick.

MrKovacic

My initial reason for posting was to try to get some help and or direction on how to handle that, in a matter I might understand. I did mention that I am new to this, and most of what I am being told is that my code is all over the place and hard to read, and that most of it does nothing. You assume correct, I was hoping that would read the IR and process as needed. I can already see what not to do, because the code doesn't work as I had hoped. After spending days and weeks doing what not to do, and googling for answers and hitting the code reference libraries, I will eventually come up with the answer. I was hoping to shorten that time by seeking help here.

MrKovacic

Also, here are the libraries I have installed:

EEPROM
Ethernet
Firmata
IRremote
LiquidCrystal
SD
Servo
SoftwareSerial
SPI
Stepper
Wire
       

Nick Gammon

Without re-reading all 4 pages of this thread, this is what I recall...

You wanted to break out of a loop when an IR code was received. The loop was fading something.

This is eminently achievable, I think. However your original code had two problems:


  • Your function getIR, which you call repeatedly, does nothing useful, because it does not alter any global variables.

  • Even if it did set global variables, you are not testing any variables in the loop which does the fade.



I can't see that you have attempted to address those issues.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

MrKovacic

Interesting. I took the fader out of the main IF section of the statements, and put it after in its own section, still under loop, and now it seems to be working.

I will say this, I don't quite understand it, but I will go with it.

Thanks!

Go Up