Pages: [1]   Go Down
Author Topic: Moving from one value to another  (Read 351 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi everyone

My first post here and i'm hoping someone might be able to point me in the right direction.

what i'm trying to achieve (badly if i say so myself)

starting with a value of zero for "a"

to input a number using the serial monitor (this will hopefully later come from a analog input)
then step to that number ...then be able to enter another number and step from the previous to the new number.

I thought i could do this by passing a to b ..but obviously that don't work

i've been banging my head for a while....and can't even work out if this is an easy thing or a hard thing ...don't even know what the procedure would be called so i don't know what to search for

any pointers would be great.....as i'm old and  my brain has stopped working

thanks for reading


merry Christmas

here's where i'm at





Code:
int a = 100;
 int b = 20;

void setup() {
Serial.begin(115200);
}

void loop() {
 
  if (Serial.available()>0){int a = Serial.parseInt();Serial.println(a); // reads from serial monitor

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

  }
}


Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for

Code:
int b = 100;
for(int a = 0; a < b; a++)
{
    do something
}
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi

thanks for the speedy reply

i tried the code as suggested (i think)

i wanted to be able to

enter a number for example 45

and it would step from 0 to 45

enter a number for example 9

and it would step from 45 down to 9

enter a number for example 27

and it would step from 9 to 27 etc etc

sorry if i wasn't clear

Code:


 int a = 100;
 int b = 100;



void setup() {

  Serial.begin(115200);
}

void loop() {
 
  if (Serial.available()>0){int a = Serial.parseInt();Serial.println(a); // reads from serial monitor




                     for(int a = 0; a < b; a++){Serial.println(a);}
 
 
                      // if (a < b){Serial.println("positive up");
                        //do {a++; Serial.println(a); delay (20);} while(a != b);  Serial.println("+++");  }
                     
                     
                     
                 // if (a > b){Serial.println("negative down");
                     // do {a--; Serial.println(a); delay (20);} while(b != a); Serial.println("---");}
                     
b = a;

  }
}





Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and welcome smiley

I think you need to use a "swap" function:
Code:
void swap( int &a, int &b )
{
   int c = a;
   a = b;
   b = c;
}

...

if ( a > b )
{
  swap( a, b );
}

It will then be easier to handle a and b in a single for loop smiley
« Last Edit: December 09, 2012, 01:28:09 am by guix » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Aside from the horrid layout, what is wrong with your code? It does something, including printing serial output.

You want it to do something. Clearly if those two somethings were the same thing, you wouldn't be here asking for help. So, I think it's safe to assume that the code does not do what you want.

However, you've failed to describe what it actually does, what output you get, and how that differs from what you want.
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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


}



 
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Pages: [1]   Go Up
Jump to: