Go Down

Topic: For loop not working... FIXED. thank you AWOL (Read 736 times) previous topic - next topic

Inter-Magic

Dec 25, 2012, 08:55 pm Last Edit: Dec 25, 2012, 09:06 pm by Inter-Magic Reason: 1
here is the problem.

the var i will not increase.

my out put via Serial.

DIR= 1 steps= 15
15151515151515151515151515151515151515151515151515151515151515151515151515151515

Here is the code.

Code: [Select]

void setup(){
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
Serial.begin(9600);
}

void loop(){
 step(true,15);
}

void step(boolean dir,int steps){
 digitalWrite(8,dir);              // set the direction via call dir
 digitalWrite(7,HIGH);             // Set sleep on
 delay(50);
 Serial.print("DIR= ");
 Serial.print(dir);
 Serial.print(" steps= ");
 Serial.println(steps);
 for(int i=0;i = steps;i++){
     //CHSW();        // take time to check the button
     digitalWrite(9, HIGH);
     delayMicroseconds(90);
     digitalWrite(9, LOW);
     delayMicroseconds(90);
   Serial.print(i);
 } // END for(int i=0;i = steps;i++)
 digitalWrite(7,LOW);
 
} //END step(boolean dir,int steps)



Thanks for the help
Dave
ö¿ö

AWOL

#1
Dec 25, 2012, 08:57 pm Last Edit: Dec 25, 2012, 09:01 pm by AWOL Reason: 1
Code: [Select]
i = steps
That's an assignment, not a comparison.
Rest assured, for loops do work.

(It's a kind of comparison; the assignment is done and the outcome is tested zero/non-zero)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Inter-Magic

Ok.

It would have been nice to see what it should have been.

i changed it to this.

Code: [Select]

for(int i=0;i <= steps;i++){

It works.

Thank you
Dave
ö¿ö

marco_c

#3
Dec 25, 2012, 09:05 pm Last Edit: Dec 25, 2012, 09:08 pm by marco_c Reason: 1
Quote
for(int i=0;i = steps;i++){

should be
for(int i=0;i < steps;i++){

if you have declared steps[15] as an array
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

AWOL

steps clearly isn't an array, but if you want fifteen steps, then yes, <= is incorrect.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Inter-Magic

steps is not an array.
Code: [Select]

steps = 15;
    for (int i = 0  ;        i <= steps           ;  i++       ) {
// for (start at 0; if i equals/same DO IT; add 1 to i) {
DO-IT();
}

This is how I understand it. 

I will be making a different post.  Its the original problem....

Dave
Dave
ö¿ö

PaulS

Code: [Select]
// for (start at 0; if i equals/same DO IT; add 1 to i) {
The middle clause is a while clause. Execute the body of the for statement while i is less than or equal steps.

Inter-Magic

thank you PaulS,
I did try <=   >= != and so on.  Here is what i found just minutes ago.
I changed
Code: [Select]
for (int i=0;i<=steps;i++){ to a
Code: [Select]
for(int i=0;i<=TheDelay;i++){ and it works great.

this was just a test but it worked. Is steps a reserved word? it did not turn red.

*TheDelay = 90

Any insight on why?

thanks

Dave
ö¿ö

PaulS

Quote
Is steps a reserved word?

No.

Quote
*TheDelay = 90

What does this mean? How is TheDelay declared?

Quote
Any insight on why?

On why what? You changed the code you initially posted, but haven't shown us the changed code. You can't reasonably expect us yo answer questions about code we can't see, can you?

Inter-Magic

Thanks for the response PaulS.

*TheDelay = 90 was not in a code window.  So its not code. I was letting viewers know TheDelay equaled 90.
as for not showing the changes. Posted on: December 25, 2012, 03:04:40 PM
Quote
Code: [Select]
for(int i=0;i <= steps;i++){


Then i stated i was having a different problem. (well it was the first problem i did not post until now but was the main problem.)
Quote
I will be making a different post.

So i hope this clears thing up for you.

This question was answered.

Dave
ö¿ö

Go Up