Go Down

Topic: Trouble switching between different modes on LED matrix using IR Remote (Read 162 times) previous topic - next topic

Hicario

I'm currently working on a project using a 16x16 NeoPixel LED matrix. It's my first time working with LED Matricies and I'm running into some errors, namely with latency caused by the IR Remote.

I want the code to be able to switch between different modes by pressing buttons on an IR Remote, but I keep running into an error where the IR Receiver receives the first code correctly and triggers the respective mode, but receives all subsequent codes incorrectly. The subsequent codes look like random numbers to me and don't correspond to the button I'm pressing at all.

Additionally, when the function does decide to work, the fade is extremely laggy.

I'm not entirely sure, but I think the issues are caused by the loop iterating too fast for the IR remote to process its signal and too slow due to all of the different modes I've added.

I've also considered using an interrupt in my code, but I'm not sure how to implement it using an IR Remote/variable inputs so if anyone has any advice/experience with it that would be extremely helpful.

Anyways, attached below is my code. Let me know if I should be doing anything differently. Thanks for your help!

Code: [Select]
#include <Adafruit_NeoMatrix.h>
#include <gamma.h>

#include <IRremote.h>

#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif

int recieverpin = 12;
#define Up 16736925
#define Down 16754775
#define b0 16738455
#define b1 16724175
#define b2 16718055
#define b3 16743045
#define UNKNOWN_Up 5316027
#define UNKNOWN_Down 2747854299
#define UNKNOWN_b0 3238126971
#define UNKNOWN_b1 2534850111
#define UNKNOWN_b2 1033561079
#define UNKNOWN_b3 1635910171
IRrecv irrecv(recieverpin);
decode_results results;
unsigned long val;
unsigned long IR_PreMillis;

#define PIN 7

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(16,16, PIN,
  NEO_MATRIX_TOP     + NEO_MATRIX_RIGHT +
  NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  NEO_GRB            + NEO_KHZ800);

int brightness;
int offTF;

//Breathe
int color[3];
int i,j,k=0;

enum FUNCTIONMODE{
  IDLE,
  OFF,
  CLOCK,
  BREATHE,
  VISUALIZER
} func_mode = IDLE;

//IRReciever Intialization

class Color{

void setup() {
  irrecv.enableIRIn();
 
  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(5);
  matrix.setTextSize(2);
  offTF=1;
  brightness=255;
  Serial.begin(9600);


}

void delays(unsigned long t) {
  for(unsigned long i = 0; i < t; i++) {
    getIR();
    delay(1);
  }
}

void getIR(){
  if(irrecv.decode(&results)) {
    val = results.value;
    Serial.println(val);
//    Serial.println("IR");
//    irrecv.resume();
//   
    switch(val){
      case Up:
      case UNKNOWN_Up: bUp(); break;
      case Down:
      case UNKNOWN_Down: bDown(); break;
      case b0:
      case UNKNOWN_b0: func_mode=OFF; Serial.println("Off");break;
      case b1:
      case UNKNOWN_b1: func_mode=CLOCK; Serial.println("Clock");break;
      case b2:
      case UNKNOWN_b2: func_mode=BREATHE; Serial.println("Breathe");break;
      case b3:
      case UNKNOWN_b3: func_mode=VISUALIZER; Serial.println("Visualizer");break;
      default: break;
     }
     irrecv.resume();
  }
}

void bUp(){}

void bDown(){}

void off() {
  if(func_mode==OFF){
      Serial.print("OFF1");
      matrix.fillScreen(0);
      offTF=0;
      matrix.show();
    }
}

void clock() {
  if(func_mode==CLOCK){
    offTF=1;
    matrix.setCursor(0,0);
    matrix.setTextColor(matrix.Color(51, 151, 255));
    matrix.setTextSize(1); 
    matrix.println("A");
    matrix.show();
  }
}

void breathe() {
  //if(func_mode==BREATHE){
      if(i<=255){
        Serial.print("i");
        color[0]=i;
        color[1]=255-i;
        color[2]=0;
        i++;
      }
      if(j<=255 & i==256){
        Serial.print("j");
        color[0]=255-j;
        color[1]=0;
        color[2]=j;
        j++;
      }
      if(k<=255 &j==256){
        Serial.print("k");
        color[0]=0;
        color[1]=k;
        color[2]=255-k;
        k++;
      }
      if(i==256&j==256&k==256){
        i,j,k=0;
      }
      matrix.fillScreen(matrix.Color(color[0],color[1],color[2]));
      matrix.show();
  //}
}
void visualizer() {
  if(func_mode==VISUALIZER){}
}

void loop() {
  //getIR();
  //off();
  //clock();
  breathe();
  //visualizer();
   
}

Go Up