Rotary Encoder incrementing digital readout

Hi

This is my first time posting on here because I have been able to figure out most stuff. Anyway. I have a 2 bit gray scale encoder connected to my Arduino. Turning the encoder is supposed to increment or decrement the 4 digit 7 segment LED display. I know the electrical end works because I have tested both the encoder and the 7 segment display with other programs I've written. The 7 segment display is run using a max7219 chip. I'll turn the serial monitor on and watch the "result" go up or down. I don't have an interrupt in the program so I have to turn the encoder slowly otherwise it will skip and get lost. I wrote the doEncoder_Expanded section, I wasn't happy with an existing method so I wrote my own. Not necessarily efficient, but I learned something along the way. The readout should be limited between 0 and 9999 Here is the issue:

The digital readout is not displaying the "result".

I'm running out of ideas. Any help would be appreciated.

big
btw these are highly addictive and a gas to play with.

#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=100;
int encoder0PinA = 2;
int encoder0PinB = 4;
//unsigned int encoder0Pos = 0;
int oldA = 0;
int oldB = 0;
int newA = 0;
int newB = 0;
int result = 0;
int a =0;
int b = 0;
int v = 0;
int ones=0;
int tens=0;
int hundreds=0;
int thousands=0;

void setup() {
  
pinMode(encoder0PinA, INPUT); 
digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
pinMode(encoder0PinB, INPUT); 
digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor
Serial.begin(9600);  
}
  
void loop() {
  newA = digitalRead(encoder0PinA);
  newB = digitalRead(encoder0PinB);
  if(newA != oldA || newB != oldB){
    doEncoder_Expanded();}
displaynumber(result);
Serial.print(result);
Serial.println();
  oldA = newA;
  oldB = newB;
  delay(delaytime);
}
 
void doEncoder_Expanded(){
  if(oldA == LOW && newA ==LOW){
    if(newB ==HIGH){
    result++;}
    else if(newB ==LOW){
     result--;}
  }
  else if(oldA == HIGH && newA ==HIGH){
    if(newB = LOW){
     result++;}
    else if(newB == HIGH){
     result--;}
  }
  else if(oldB == LOW && newB ==LOW){
    if(newA ==HIGH){
    result--;}
    else if(newA ==LOW){
     result++;}
  }
  else if(oldB == HIGH && newB ==HIGH){
    if(newA = LOW){
     result--;}
    else if(newA == HIGH){
     result++;}
  }
  }
void displaynumber(int v){
  if(v<0){
  v=0;}
  else if(v>9999){
    v=9999;}  
  int ones=0;
    int tens=0;
    int hundreds=0;
    int thousands=0;
    ones=v%10;
    a=v%100;
    tens=a/10;
    b=v%1000;
    hundreds=b/100;
    thousands=v/1000;
    lc.setDigit(0,0,(byte)thousands,false);
    lc.setDigit(0,1,(byte)hundreds,false);
    lc.setDigit(0,2,(byte)tens,true); 
    lc.setDigit(0,3,(byte)ones,false);
}

The digital readout is not displaying the "result".

Instead, it's showing the words to "It's a Small World"?

Oh my bad, it's not lighting up at all, no segments or decimal places.

big_i11580:
Oh my bad, it's not lighting up at all, no segments or decimal places.

Have you written any program that displays data on the LED display?

If not, write a short program just to learn how to do it.

...R

Robin

Have you written any program that displays data on the LED display?

Yes, I have several other programs that I wrote using the same display code with a few modifications, one was giving the value of a potentiometer. Worked fine, another was a timer setup counting down.

If not, write a short program just to learn how to do it.

Already did, I am missing something on this one. Hence why I am asking for help.

big

+'0'

So I added the volatile to the code. Still nothing. I am running another program on the LED display to make sure it works. Yep it works. Still puzzled why the LED display doesn't light up at all. It should be displaying the "result" as it is incremented or decremented with the encoder. Encoder still works too. Head scratcher.

[/#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=100;
int encoder0PinA = 2;
int encoder0PinB = 4;
//unsigned int encoder0Pos = 0;
int oldA = 0;
int oldB = 0;
int newA = 0;
int newB = 0;
volatile int result = 0;
int a =0;
int b = 0;
int v = 0;
int ones=0;
int tens=0;
int hundreds=0;
int thousands=0;

void setup() {
  
pinMode(encoder0PinA, INPUT); 
digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
pinMode(encoder0PinB, INPUT); 
digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor
Serial.begin(9600);  
}
  
void loop() {
  newA = digitalRead(encoder0PinA);
  newB = digitalRead(encoder0PinB);
  if(newA != oldA || newB != oldB){
    doEncoder_Expanded();}
displaynumber(result);
Serial.print(result);
Serial.println();
  oldA = newA;
  oldB = newB;
  delay(delaytime);
}
 
void doEncoder_Expanded(){
  if(oldA == LOW && newA ==LOW){
    if(newB ==HIGH){
    result++;}
    else if(newB ==LOW){
     result--;}
  }
  else if(oldA == HIGH && newA ==HIGH){
    if(newB = LOW){
     result++;}
    else if(newB == HIGH){
     result--;}
  }
  else if(oldB == LOW && newB ==LOW){
    if(newA ==HIGH){
    result--;}
    else if(newA ==LOW){
     result++;}
  }
  else if(oldB == HIGH && newB ==HIGH){
    if(newA = LOW){
     result--;}
    else if(newA == HIGH){
     result++;}
  }
  }
void displaynumber(int v){
  if(v<0){
  v=0;}
  else if(v>9999){
    v=9999;}  
  int ones=0;
    int tens=0;
    int hundreds=0;
    int thousands=0;
    ones=v%10;
    a=v%100;
    tens=a/10;
    b=v%1000;
    hundreds=b/100;
    thousands=v/1000;
    lc.setDigit(0,0,(byte)thousands,false);
    lc.setDigit(0,1,(byte)hundreds,false);
    lc.setDigit(0,2,(byte)tens,true); 
    lc.setDigit(0,3,(byte)ones,false);
}
code]

Try this approach to see if your code is really doing what you want it to do.

It is called emulating and debugging.

#include <Arduino.h>

#define DEBUG

#ifndef DEBUG
#include "LedControl.h"
LedControl lc = LedControl(12, 11, 10, 1);
#endif

unsigned long delaytime = 100;
int encoder0PinA = 2;
int encoder0PinB = 4;
//unsigned int encoder0Pos = 0;
int oldA = 0;
int oldB = 0;
int newA = 0;
int newB = 0;
volatile int result = 0;
int a = 0;
int b = 0;
int v = 0;
int ones = 0;
int tens = 0;
int hundreds = 0;
int thousands = 0;




void doEncoder_Expanded(void);
void displaynumber(int v);

byte GrayCode[][2] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};

void setup() {

  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor
  Serial.begin(115200);
}
 

  
void loop() {
#ifndef DEBUG
  newA = digitalRead(encoder0PinA);
  newB = digitalRead(encoder0PinB);
#else
  newA = 1;
  newB = 0;
#endif

for(int i = 0; i != 4; i++)
{
    newA = GrayCode[i][0];
    newB = GrayCode[i][1];
    Serial.print("newA " );
    Serial.println(newA);
     Serial.print("newB " );
    Serial.println(newB);
    
    if (newA != oldA || newB != oldB) {
      doEncoder_Expanded();
    }

    displaynumber(result);
    Serial.print(result);
    Serial.println();
    //for (;;);

    oldA = newA;
    oldB = newB;
    delay(delaytime);
  }
for(int i = 3; i != -1 ; i--)
{
    newA = GrayCode[i][0];
    newB = GrayCode[i][1];
        Serial.print("newA " );
    Serial.println(newA);
     Serial.print("newB " );
    Serial.println(newB);
    
    if (newA != oldA || newB != oldB) {
      doEncoder_Expanded();
    }

    displaynumber(result);
    Serial.print(result);
    Serial.println();
    //for (;;);

    oldA = newA;
    oldB = newB;
    delay(delaytime);
  }








  
  for(;;); 
}

  

  void doEncoder_Expanded() {
    if (oldA == LOW && newA == LOW) {
      if (newB == HIGH) {
        result++;
      }
      else if (newB == LOW) {
        result--;
      }
    }
    else if (oldA == HIGH && newA == HIGH) {
      if (newB = LOW) {
        result++;
      }
      else if (newB == HIGH) {
        result--;
      }
    }
    else if (oldB == LOW && newB == LOW) {
      if (newA == HIGH) {
        result--;
      }
      else if (newA == LOW) {
        result++;
      }
    }
    else if (oldB == HIGH && newB == HIGH) {
      if (newA = LOW) {
        result--;
      }
      else if (newA == HIGH) {
        result++;
      }
    }
  }
  void displaynumber(int v) {

  if (v < 0) {
    v = 0;
  }
  else if (v > 9999) {
    v = 9999;
  }
  Serial.print("display "); 
  Serial.println(v);
  return;





  
//  int ones = 0;
//  int tens = 0;
//  int hundreds = 0;
//  int thousands = 0;
//  ones = v % 10;
//  a = v % 100;
//  tens = a / 10;
//  b = v % 1000;
//  hundreds = b / 100;
//  thousands = v / 1000;
//  lc.setDigit(0, 0, (byte)thousands, false);
//  lc.setDigit(0, 1, (byte)hundreds, false);
//  lc.setDigit(0, 2, (byte)tens, true);
//  lc.setDigit(0, 3, (byte)ones, false);
  }

Thanks guys for the help. I figured it out. I was missing some code for the LC control library required in the setup. As usual if you stare at it long enough you eventually figure it out. Thanks again.

big