rotary encoder with momentary pushbutton help


i'm trying to make my project to work, but i'm a begginer on programing, i really apreciate if somebody can help me solving this problem.

i have an arduino MEGA2560 and i'm using a rotary encoder to set a 4 digits value in a flight simulator airplane transponder panel (4 is like a 7 segment display using max7219) using an interface program. LINK2FS from

program says:

to increase first digit: Serial.println("A34");}else{Serial.println("A38");} to decrease value
to increase second digit : Serial.println("A35");}else{Serial.println("A39");} to decrease value
to increase third digit: Serial.println("A36");}else{Serial.println("A40");} to decrease value
to increase fourth digit: Serial.println("A37");}else{Serial.println("A41");} to decrease value

what i need is to jump from one digit to another using the momentary pushbutton included on the rotary encoder, and then use the encoder to set the wished value in each digit interfacing the program with the flight simulator through serial.print number code.

i have written a code but it does not work when i check it in the serial monitor. it's all scrambled.

any support will be appreciated.

thank you.


int pinA4 = 32; //CLK
int pinB4 = 33; //DT
int pinC4 = 31; //SW
int encoderPosCount4 = 0;
int pinALast4;
int aVal4;
boolean sentido4; //clockwise
int valorPulsador4 = 0;
int jump = 0; // salta de digito

void setup() {

Serial.begin (115200);


pinMode (pinA4,INPUT); //CLK HDG
pinMode (pinB4,INPUT); //DT HDG
pinMode (pinC4,INPUT_PULLUP); //SW HDG

pinALast4 = digitalRead(pinA4); //read the encoder on pinA

void loop() {


aVal4 = digitalRead(pinA4); //read pinA again

if (aVal4 != pinALast4){ //if read value is diferent at previous value, encoder has turned

if (digitalRead(pinB4) != aVal4) { // if pinB is diferent at pinA, turn clockwise
encoderPosCount4 ++;
sentido4 = true;
} else { // otherwise turn counterclockwise
sentido4 = false;

pinALast4 = aVal4;


if (digitalRead(pinC4) == LOW){

jump ++; // jump digit
if (jump > 3){
jump = 0; // to initial digit


void secuence(){
case 0:
if (sentido4){

case 1:
if (sentido4){

case 2:
if (sentido4){

case 3:
if (sentido4){

while(digitalRead(pinC4) == 0);{

Firstly - Revise your post so the code is enclosed in code tags (item 7).

Recommendation #1: Use an encoder library, it makes life so much easier. A good one by : Buxton.

Recommendation #2: Make an array to hold the digit values you're going to manipulate. That way instead of writing digit1, digit2, etc. whenever you need to do something you can use the digit's array index, which allows you to work on all the digits with one section of code (hopefully a function).


Your sketch needs two mutually exclusive states. This can be done by making the pushbutton act as a push on/ push off switch and then using that state to determine whether: 1. Encoder selects a digit to be acted upon or; 2. Encoder increments/decrements the variable represented by the selected digit.

Visit the first five demos in IDE -> file/examples/digital and become familiar with the concepts illustrated therein.

Don't try to build the entire thing in one shot, test one small thing and make it work, test the next small thing and make it work, then combine the two. Lather, rinse, repeat. Example: get the encoder working then use it to change a digit value.

Finally, key to all is being able to do several things at the same time.

You have:

if (digitalRead(pinC4) == LOW)

Do not look at a level, instead look at a change in state and then check for the LOW.

Are your baud rates the same?