Help with code related to rotary encoder and 4x7segment display

I'm working on a project to turn the rotary encoder to increase a number (i have the basics of this figured out) and then push it to move to the next digit over and increase/decrease the number for that digit.

So i know the code for increasing the number related to the rotary encoder is wrong, but i don't know what to change it to. And then how to make all the digits the same brightness and actually display the correct numbers...

Here's the code i have so far... Any help would be appreciated! (I know the hardware setup is a little weird, i set it up as i went...)

/* Purpose: Defines hardware setup */

define sega 2

define segb 3

define segc 4

define segd 5

define sege 6

define segf 7

define segg 9

define encoderpina 10

define encoderpinb 11

define encoderpinswitch 12

define digit4 0

define digit3 1

define digit2 13

define digit1 8

/* Purpose: Declares each number output function */ void output_0(); void output_1(); void output_2(); void output_3(); void output_4(); void output_5(); void output_6(); void output_7(); void output_8(); void output_9();

/* Purpose: Declares variables for the loop function */ int number=0; int number4=0; int number3=0; int number2=0; int number1=0; int encoderpinaLast4=LOW; int encoderpinaLast3=LOW; int encoderpinaLast2=LOW; int encoderpinaLast1=LOW; int digit=4; int switchLast=LOW;

/* Purpose: Declares each pin as input or output */ void setup() { pinMode(sega, OUTPUT); pinMode(segb, OUTPUT); pinMode(segc, OUTPUT); pinMode(segd, OUTPUT); pinMode(sege, OUTPUT); pinMode(segf, OUTPUT); pinMode(segg, OUTPUT); pinMode(encoderpina, INPUT_PULLUP); pinMode(encoderpinb, INPUT_PULLUP); pinMode(encoderpinswitch, INPUT); pinMode(digit1, OUTPUT); pinMode(digit2, OUTPUT); pinMode(digit3, OUTPUT); pinMode(digit4, OUTPUT);


/* Purpose: Increments number when turned clockwise, decrements number when turned counter-clockwise */ void loop() { int n4=digitalRead(encoderpina); int n3=digitalRead(encoderpina); int n2=digitalRead(encoderpina); int n1=digitalRead(encoderpina);

if(digitalRead(encoderpinswitch==HIGH)){ digit--; if(digit==0) digit=4; }

if(digit==4){ if ((encoderpinaLast4 == LOW) && (n4 == HIGH)) { if (digitalRead(encoderpinb) == LOW) { number4++; if(number4==10) number4=0; } else { number4--; if(number4==-1) number4=9; } } encoderpinaLast4=n4; } if(digit==3){ if ((encoderpinaLast3 == LOW) && (n3 == HIGH)) { if (digitalRead(encoderpinb) == LOW) { number3++; if(number3==10) number3=0; } else { number3--; if(number3==-1) number3=9; } } encoderpinaLast3=n3; } if(digit==2){ if ((encoderpinaLast2 == LOW) && (n2 == HIGH)) { if (digitalRead(encoderpinb) == LOW) { number2++; if(number2==10) number2=0; } else { number2--; if(number2==-1) number2=9; } } encoderpinaLast2=n2; }

if(digit==1){ if ((encoderpinaLast1 == LOW) && (n1 == HIGH)) { if (digitalRead(encoderpinb) == LOW) { number1++; if(number1==10) number1=0; } else { number1--; if(number1==-1) number1=9; } } encoderpinaLast1=n1; }

digitalWrite(digit4, HIGH); digitalWrite(digit3, LOW); digitalWrite(digit2, LOW); digitalWrite(digit1, LOW); if(number4==0) output_0(); if(number4==1) output_1(); if(number4==2) output_2(); if(number4==3) output_3(); if(number4==4) output_4(); if(number4==5) output_5(); if(number4==6) output_6(); if(number4==7) output_7(); if(number4==8) output_8(); if(number4==9) output_9();

digitalWrite(digit4, LOW); digitalWrite(digit3, HIGH); digitalWrite(digit2, LOW); digitalWrite(digit1, LOW); if(number3==0) output_0(); if(number3==1) output_1(); if(number3==2) output_2(); if(number3==3) output_3(); if(number3==4) output_4(); if(number3==5) output_5(); if(number3==6) output_6(); if(number3==7) output_7(); if(number3==8) output_8(); if(number3==9) output_9();

digitalWrite(digit4, LOW); digitalWrite(digit3, LOW); digitalWrite(digit2, HIGH); digitalWrite(digit1, LOW); if(number2==0) output_0(); if(number2==1) output_1(); if(number2==2) output_2(); if(number2==3) output_3(); if(number2==4) output_4(); if(number2==5) output_5(); if(number2==6) output_6(); if(number2==7) output_7(); if(number2==8) output_8(); if(number2==9) output_9();

digitalWrite(digit4, LOW); digitalWrite(digit3, LOW); digitalWrite(digit2, LOW); digitalWrite(digit1, HIGH); if(number1==0) output_0(); if(number1==1) output_1(); if(number1==2) output_2(); if(number1==3) output_3(); if(number1==4) output_4(); if(number1==5) output_5(); if(number1==6) output_6(); if(number1==7) output_7(); if(number1==8) output_8(); if(number1==9) output_9(); }

See the solution in this post to get your display to work. Afterwards, we'll work on the encoder.


What happens if the encoder skips? perhaps you would want to check if counter is greater than 9 and/or less than 0.

if ((encoderpinaLast1 == LOW) && (n1 == HIGH)) { if (digitalRead(encoderpinb) == LOW) { number1++; if(number1*==10) number1=0; } else { number1--; if(number1==*-1) number1=9; } }