Pulse Counts Encoder and Teaching a Position

I'm at the end of my wits here. I have a 3-axis machine with push button encoders (every time the shaft driving the axis rotates it clicks the switch and counts up in the micro-controller.) With this, each possible position is given a coordinate (x,y,z.) Once in a position I press a button which "teaches" this position by storing the x, y and z values in tags: x1, y1 and z1. I move to another spot and teach the position again, this time in tags: x2, y2 and z2. To prevent bounce in the "encoder" P.B.s I have a delay of 75-100 milliseconds which i tuned through testing to find the ideal time. There is again a bounce in the "teach" button but I again have found a way around that. Now in serial monitor if I just monitor: x encoder count and y encoder count I have no problems, however if i monitor: x encoder count, y encoder count, x1, y1, z1, x2, y2 and z2 then my x count and y count become inaccurate (when they should be around 20 they have only registered 5 clicks of the encoder P.B.) Any ideas of what this might be?

// Encoder Count/Position + Debounce

if (y_toggle == HIGH && com_sum == 1)
    y_pos = y_pos+1;

if (y_toggle == HIGH && com_sum == 2)
    y_pos = y_pos-1;

if (x_toggle == HIGH && com_sum == 4)
    x_pos = x_pos+1;

if (x_toggle == HIGH && com_sum == 3)
    x_pos = x_pos-1;

// Teach Position

if (com_sum == 7 && cur_pos == 0)
    x1 = x_pos;
    y1 = y_pos;
    z1 = z_pos;
    cur_pos = cur_pos+1;

if (com_sum == 7 && cur_pos == 1 && x1 != x_pos && y1 != y_pos)
    x2 = x_pos;
    y2 = y_pos;
    z2 = z_pos;
    cur_pos = cur_pos+1;


The com_sum refers to different buttons
com_sum = 1 --> joy stick up
com_sum = 2 --> joy stick down
com_sum = 3 --> joy stick left
com_sum = 4 --> joy stick right
com_sum = 7 --> Teach Position P.B.

y_toggle is the y encoder P.B.
x_toggle is the x encoder P.B.

You need to look at the blink without delay example. Get rid of the delay()s. You can ignore the switch changing state, if the change happens within x milliseconds after the last change, by recording when the change happens.

The delay()s are killing you.


takes 4 times as long as


but conveys no more information.

Thank you Paul.