Rotary Encoder stops working after about an hour

I’m fairly new to Arduino, but have been catching on.

I’m using processing with my Arduino and a rotary encoder (this one) to manually dim lights (I can also control the lights through touchOSC, the processing code is running quite a few tasks.

Upon first starting up the processing sketch, the rotary encoder works just fine. But after about an hour, it just stops working.

I’m wondering but can’t seem to figure out if it’s my:

  • Wiring
  • Code
  • Rotary Encoder
  • Other

that’s failing.

WIRING:
I’m wiring one rotary encoder pin to pin#2 (pmw) on my mega 2560, the middle to ground, second encoder pin to pin#51 (dig). On the button side, one is connected to pin#53 (digital) and the other to ground.

CODE:
I’m having trouble believing it’s inherently my code though, a) because it works for the first hour or so, and b) because at the very least, the button aspect of the encoder should work after that hour, since the code is so simple and worked before with other buttons. *also, as a note, this part of my code was based off of this Buildr blog post.

In either case, a simplified version of my processing code is:

int EPin1 = 51;       //encoder pin (digital)
int EPin2 = 2;       //encoder pin (pmw)
int EPinB = 53;       //encoder on,off button pin
float onoff = 0.0f;
float dimmingspeed = 0.03;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
float previousbuttonstate;
float currentbuttonstate;
float holdbright = 0.0f;
//...more variables... but I won't bore you

void setup (){
  
  //processing visuals
  size(320,480);
  background(0);
  
  
  //input parameters for physical buttons and sensors
  arduino.pinMode(EPin1, arduino.INPUT);
  arduino.pinMode(EPin2, arduino.INPUT);
  arduino.pinMode(EPinB, arduino.INPUT);
  arduino.pinMode(LPin, arduino.INPUT);
  
  //turn encoder pullup resistors on
  arduino.digitalWrite(EPin1, arduino.HIGH);
  arduino.digitalWrite(EPin2, arduino.HIGH);
  arduino.digitalWrite(EPinB, arduino.HIGH);
  
  //a bunch of other tasks, mostly reading google spreadsheets, saving csvs
      
}

void draw(){
  
  //Main Code and Functions
    EncoderButton();
    if(onoff == 1){updateEncoder();}
    
    /*More tasks
    if(scheduleover == 0){ColorSchedule();}
    if(over == 0){TemperatureToRGB();}
    if(sensorover == 0){LightSensor();}
    Brightness();
    checkLights();
    SaveCSV();
    RefreshIPs();*/
  
  //write to arduino
  arduino.analogWrite(R1Pin,int(R1));
  arduino.analogWrite(R2Pin,int(R2));
  arduino.analogWrite(R3Pin,int(R3));
  arduino.analogWrite(G1Pin,int(G1));
  arduino.analogWrite(G2Pin,int(G2));
  arduino.analogWrite(G3Pin,int(G3));
  arduino.analogWrite(B1Pin,int(B1));
  arduino.analogWrite(B2Pin,int(B2));
  arduino.analogWrite(B3Pin,int(B3));
      
}

//ENCODER BUTTON
void EncoderButton(){
  //on,off button
  previousbuttonstate = currentbuttonstate;
  currentbuttonstate = arduino.digitalRead(EPinB);
  
  if(currentbuttonstate > previousbuttonstate){
    if (onoff == 0){
      onoff = 1;}
      else if (onoff == 1){
        onoff = 0;}}
}
  


//UPDATE ENCODER
void updateEncoder(){
  int MSB = arduino.digitalRead(EPin1); //MSB = most significant bit
  int LSB = arduino.digitalRead(EPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  //turned clockwise
  if(sum == 13 || sum == 4 || sum == 2 || sum == 11){
    
    strip1 = 0;
    strip2 = 0;
    strip3 = 0;
    
    holdbright = brightness1;
      if(brightness1 < (1-dimmingspeed+0.001)){
        brightness1 = (holdbright + dimmingspeed);}
        else if (brightness1 > (1-dimmingspeed)){
          brightness1 = 0.999;}
          
    holdbright = brightness2;
      if(brightness2 < (1-dimmingspeed+0.001)){
        brightness2 = (holdbright + dimmingspeed);}
        else if (brightness2 > (1-dimmingspeed)){
          brightness2 = 0.999;}
          
    holdbright = brightness3;
      if(brightness3 < (1-dimmingspeed+0.001)){
        brightness3 = (holdbright + dimmingspeed);}
        else if (brightness3 > (1-dimmingspeed)){
          brightness3 = 0.999;}
  }
  
  //turned counterclockwise
  if(sum == 14 || sum == 7 || sum == 1 || sum == 8){
    
    strip1 = 0;
    strip2 = 0;
    strip3 = 0;
    
    holdbright = brightness1;
      if(brightness1 > dimmingspeed){
        brightness1 = (holdbright - dimmingspeed);}
        else if (brightness1 < dimmingspeed){
          brightness1 = 0.0;}
    
    holdbright = brightness2;
      if(brightness2 > dimmingspeed){
        brightness2 = (holdbright - dimmingspeed);}
        else if (brightness2 < dimmingspeed){
          brightness2 = 0.0;}
    
    holdbright = brightness3;
      if(brightness3 > dimmingspeed){
        brightness3 = (holdbright - dimmingspeed);}
        else if (brightness3 < dimmingspeed){
          brightness3 = 0.0;}      
  }

  lastEncoded = encoded; //store this value for next time
}

I sincerely cannot figure out why it’s crapping out on my after a while, it’s almost like it just turns off.

Any ideas?

mballina:
I'm using processing with my Arduino and a rotary encoder

Why use processing when you have a Adruino? Just use the Arduino IDE. And if you want to use processing, you have to ask it there because this is a forum for Arduino... Now we can't even compile it.

But I got some tips:

Mind the indentation!

  if(currentbuttonstate > previousbuttonstate){
    if (onoff == 0){
      onoff = 1;}
      else if (onoff == 1){
        onoff = 0;}}

Should be:

  if(currentbuttonstate > previousbuttonstate){
    if (onoff == 0){
      onoff = 1;}
    else if (onoff == 1){
      onoff = 0;
    }
  }

yes, for the compiler it's the same but for us humans it's very different. The later is way easier to read and check. You could also just write it as:

  if(currentbuttonstate > previousbuttonstate){
    onoff = !onoff;
  }

But that brings me to the variable. Why on earth is it a float?! You treat it as a bool. Same for button state... a unsigned int of a byte would be as better.

Try to think of better variable names. rotaryPinA, rotaryPinB, rotaryPinButton are clearer what they do.

Try not to make every single variable a global variable. Or don't make a variable for each step you do.