Go Down

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

billroy

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.

-br

oldman2758

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() {

  Serial.begin(115200);
}

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);}
                         
                    }
                         


}




billroy

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!

Cheers,

-br



Go Up