Need help with "for"

This is my first ground up sketch, using functions I have gathered in learning. I am currently using the for statement to ramp a motor speed up and down. Partial Code

  for (int i=0; i <= 170; i++){      //ramp up motor speed

My problem is I can’t figure out how to use a variable here, I don’t want a constant value here, because my goal is to use an analog read on a pot to set this value and allow this value to change between when it starts and when it stops. This is the value that I am doing the digitalWrite with.
I have tried leaving this portion out, but the function fails for me if I have anything other than i=(value) Is there a better way to do this? What is a better function I could use?

For fixed speed values this sketch works. press the button, the motor speeds up starts counting and then slows down if time runs out of if you press the button again.

Thank You

Full Code

/*
 *  Choo-Choo Operator
 */
int i = 0;                    //this will be the PMM output and motor speed
int switchPin = 2;              // switch is connected to pin 2
int runPin = 9;                //pwm output
int val1;                        // variable for reading the pin status
int val2;                        //  the delayed/debounced status- Thanks Ladyada!
int sensor = 4;                // reed switch on track
int valsensor;                //variable for reed sensor
int buttonState;                // variable to hold the button state
long runtime = 0;
long totaltime = 79000;    //Stop if running too long
int run = 0;              // initilise as stopped
int count = 0;
long interval = 30000;    //normal run time before watching the sensor
long stoptime = 0;
void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(sensor, INPUT);        //reed switch
  pinMode(runPin, OUTPUT);
  Serial.begin(9600);                // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  if (run== 3){
    valsensor = digitalRead(sensor);        //reed switch on track
    if (valsensor == LOW){
      Serial.print(valsensor);
      run = 4;
    }
  }
  val1 = digitalRead(switchPin);      // input value  val1
  delay(10);                         
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val1 == val2) {                 // make sure we got 2 consistant readings!
    if (val1 != buttonState) {          // the button state has changed!
      if (val1 == LOW) {                // check if the button is pressed pull low?
        if (run == 0) {          // is it off?
          run = 1;               // running
        } 
        else {
          if (run == 3){          // run time has expired
            run = 4;
          }
          else{
            if (run == 2)
              run = 4;      // time to stop
          }

        }
      }
    }
    buttonState = val1;                 // save the new state in our variable
  }

  if (run == 1) {                    // check if the status is starting
    for (int i=0; i <= 170; i++){      //ramp up motor speed
      analogWrite(runPin, i);
      delay(25);
      if (i<=170){
        run = 2;
        runtime = millis();
        stoptime = millis();
      }
    }
  }
  if (run == 2){
    if (millis() - runtime > interval){     //run for the time defined by interval
  
      run = 3;
    }

    else
    {
      Serial.println(millis() - runtime);
    }
  }
  if (run == 3){
    if (millis() - stoptime > totaltime){     //max time to run if reed switch missed.
      run = 4;
    }
    else {
      Serial.print("stop time is:");
      Serial.println(millis() - stoptime);
    }
  }


  if (run == 4){                  //ramp down motor speed
    for (int i=i; i >=0; i--){
      analogWrite(runPin, i);
      delay(25);
      Serial.println(i);  
      run = 0;  
    }
  }
  Serial.print("value of run in global scope is:");
  Serial.println(run);
}

If you want to vary the start value that is fine.

for (int i= start; i <= 170; i++)

Just be sure to give the variable a starting value in the pre setup stage. Then you can change it however you want. You could even change it threw the serial monitor.

Also since your using the variable i for your motor speed you may want to use a different variable in each of your for() statements or change the name of the variable your using to declare your motor speed. Otherwise you may run into some conflicts when running the code on the arduino. Things may not work as you had planned.

@digimike: What does this mean:

Otherwise you may run into some conflicts when running the code on the arduino

?

Digimike, thank you. I believe I understand. I used a different int inside the For loop and that worked.
Anyway it did solve the problem I was working toward.
here is the code.

/*
 *  Choo-Choo Operator
 */
int i = 0;                    //this will be the PMM output and motor speed
int maxspeed = 0;
int potpin = 0;
int switchPin = 2;              // switch is connected to pin 2
int runPin = 9;                //pwm output
int val1;                        // variable for reading the pin status
int val2;                        //  the delayed/debounced status- Thanks Ladyada!
int sensor = 4;                // reed switch on track
int valsensor;                //variable for reed sensor
int buttonState;                // variable to hold the button state
long runtime = 0;
long totaltime = 79000;    //Stop if running too long
int run = 0;              // initilise as stopped
int count = 0;
long interval = 30000;    //normal run time before watching the sensor
long stoptime = 0;
void setup() {
  pinMode(potpin, INPUT);  //pot for maxspeed
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(sensor, INPUT);        //reed switch
  pinMode(runPin, OUTPUT);
  Serial.begin(9600);                // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  if (run== 3){
    valsensor = digitalRead(sensor);        //reed switch on track
    if (valsensor == LOW){
      Serial.print(valsensor);
      run = 4;
    }
  }
  val1 = digitalRead(switchPin);      // input value  val1
  delay(10);                         
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val1 == val2) {                 // make sure we got 2 consistant readings!
    if (val1 != buttonState) {          // the button state has changed!
      if (val1 == LOW) {                // check if the button is pressed pull low?
        if (run == 0) {          // is it off?
          run = 1;               // running
        } 
        else {
          if (run == 3){          // run time has expired
            run = 4;
          }
          else{
            if (run == 2)
              run = 4;      // time to stop
          }

        }
      }
    }
    buttonState = val1;                 // save the new state in our variable
  }

  if (run == 1) {                    // check if the status is starting
  int maxspeed = analogRead(potpin);
        maxspeed = map(maxspeed, 0, 1023, 50, 240);
    for (int i=0; i <= maxspeed; i++){      //ramp up motor speed
      analogWrite(runPin, i);
      delay(25);
        int maxspeed = analogRead(potpin);
        maxspeed = map(maxspeed, 0, 1023, 50, 240);
      if (i<=maxspeed){
        run = 2;
        runtime = millis();
        stoptime = millis();
      }
    }
  }
  if (run == 2){
    int maxspeed = analogRead(potpin);
    maxspeed = map(maxspeed, 0, 1023, 50, 240);
    analogWrite(runPin, maxspeed);
    if (millis() - runtime > interval){     //run for the time defined by interval

        run = 3;
    }

    else
    {
      Serial.println(millis() - runtime);
    }
  }
  if (run == 3){
    if (millis() - stoptime > totaltime){     //max time to run if reed switch missed.
      run = 4;
    }
    else {
      //Serial.print("stop time is:");
      //Serial.println(millis() - stoptime);
    }
  }


  if (run == 4){                  //ramp down motor speed
    int maxspeed = analogRead(potpin);      //read pot
    maxspeed = map(maxspeed, 0, 1023, 50, 240);    //map
    analogWrite(runPin, maxspeed);   //can maxspeed here be used in the for function below?
    for (int i=maxspeed; i >=0; i--){
      analogWrite(runPin, i);
      delay(25);
      Serial.println(i);  
      run = 0;  
    }
  }
  //Serial.print("value of run in global scope is:");
  Serial.println(maxspeed);
}

Some basics on FOR

You have those three expressions. You usually see them as
for (i=0;i<something;i++)

The first expression is executed before the loop starts.
The second is executed at the top of the loop to see if it should loop again
The third is executed at the bottom of the loop.

IT DOES NOT MATTER WHAT THE EXPRESSIONS ARE or what they do. They can be anything you want. There isn’t anything that says they have to involve an integer. There isn’t any thing that says they have to use the same variable. There isn’t anything that says the third expression has to add 1. It can be anything. The middle one has to resolve to an expression that has a true/false answer, but since true = non-zero, it really can be just about anything.

All the matters is that it’s going to execute the first one before, the second one at the top of the loop to decide if it will go around again, and the bottom one at the end of the loop.

this works
for (i=-234.5;j<200;a=b*c) {
… }

now, what is inside the loop had better modify j in some way that eventually, j becomes less than 200 or the loop won’t end (although you can break out of a loop other ways).

Because it doesn’t matter, there is NOTHING special about the “loop variable”. You can modify it with impunity inside the loop. All that matters is that, eventually, the middle expression becomes true.

Now, there might be clearer ways of writing code that has
for (i=-234.5;j<200;a=b*c) {

BUT IT’S PERFECTLY GOOD C CODE

@digimike: What does this mean:

Does this not cause some sort of problem?

for (int i=i; i >=0; i--){

Correct me if i'm wrong but isn't it a problem to have int i = i?

As i see it he was trying to use the end value of 'i' from a previous for() loop to be the starting point for another for() statement in the code. But doesn't the int i end up resetting the 'i' before it takes the value of 'i' and stores it to 'i'?

It all makes since in my head just hard to spell it out in plain English.

@digimike - Ah, I see what you mean.

But doesn't the int i end up resetting the 'i' before it takes the value of 'i' and stores it to 'i'?

No, there's no resetting involved - it simply won't work.

for (int i = i;...

declares a new "i" and since that new "i" is the only one now in scope, the assignment is a self-assignment; "i" takes the value of itself. The value will be whatever happened to be on the stack at that point.

Just to further confuse the OP - any or all of the arguments are optional. For example you can write:

for(;;)
{
...
}

which will execute the loop endlessly -- unless there is a break ( or a goto) inside the loop.

That's why you'll sometimes see:#define EVER (;;)and hence:for EVER { ...} :D