Go Down

Topic: Moving from one value to another (Read 403 times) previous topic - next topic


Here's how I think about this algorithm.  Whatever 'goal' is set to, the code in loop() will cause 'value' catch up to it eventually.

Code: [Select]

int value, goal;

void setup() {...} // set goal

void loop {
if (value > goal) value--;
else if (value < goal) value++;
else {;}

If you want the values to sync on one call to loop() change both 'if's to 'while's.



thank you

to all of those who made a newbie welcome.

I had a complete brain freeze which you helped unfreeze

but now have it working  was chasing "a" rather than "b"

Code: [Select]

int a = 0;
int b = 0;

void setup() {


void loop() {
  if (Serial.available()>0){int a = Serial.parseInt();

                    if (a > b){do {b++; Serial.println(b); delay (20);} while(a != b);}
                   if (a < b){do {b--; Serial.println(b); delay (20);} while(a != b);}



I dare say most of us have found that fortune favors those who name their variables something that will keep that sort of thing straight. 

Let this be the last program you use A and B!



Go Up