When x > y add one to y but when y < x decrease x by one

hello everyone, I have a question obout how i can get 2 values to behave a sertain way.

I want that when x becomes bigger than y, y gets pushed up to the same value but i also want it the other way arround, so when y becomes a lower vallue than x, x gets pulled to the y vallue.

so when both vallues are for exapmle 10, x can become 11 but has to pull y with it or y can pull it to 9 but also has to pull x to 9 but x can never become lower than y or y higer than x

can't seem to figure this out.

Grab two buckets. Label one X and the other Y. Get a bunch of rocks and start tossing them in the buckets. Pretty soon you'll see how to do it.

-jim lee

Neither can we! Show the code you have been testing with and perhaps someone can point out the errors.
Otherwise, you, your self, can debug your code by using serial print to show your variables each step of the way through you logic.
Paul

Please explain in words what the "||" in this expression is supposed to do, and why you think it is needed.

      if (Distance < Offset || Offset > Distance){
        Offset = Distance;}

The overall collection of if statements is close to incomprehensible. Consider some different approaches.

EDIT: OP deleted post.

I just tried a lot of different options and just coudnt think of annything that would help me in this case. I got it now tho.

in the end i was just stuck in my own way of thinking...

aState = digitalRead(CLK);
    if (aState != aLastState){     
      if (digitalRead(DT) != aState) {
        if (counter == 1){
          Distance ++;
        }
        else if (counter == 2){
          Offset ++;
          if (Offset > Distance){
            Distance ++;
          }
        }
        else if (counter == 3){
          Acceleration ++;
        }
        else if (counter == 4){
          FRWRD ++;
        }
        else if (counter == 5){
          BCKWRD ++;
        }
      } else {
        if (counter == 1){
          Distance --;
          if (Distance < Offset){
            Offset --;
          }
        }
        else if (counter == 2){
          Offset--;
        }
        else if (counter == 3){
          Acceleration --;
        }
        else if (counter == 4){
          FRWRD--;
        }
        else if (counter == 5){
          BCKWRD--;
        }
      }

You are welcome!

Thank you!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.