Pages: [1]   Go Down
Author Topic: For loop not working... FIXED. thank you AWOL  (Read 619 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 13
I don't look for the problems. Just for the answers. ö¿ö
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: December 25, 2012, 03:06:15 pm by Inter-Magic » Logged


Dave
ö¿ö

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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)
« Last Edit: December 25, 2012, 03:01:06 pm by AWOL » Logged

"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.

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 13
I don't look for the problems. Just for the answers. ö¿ö
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.

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

i changed it to this.

Code:
for(int i=0;i <= steps;i++){
It works.

Thank you
Logged


Dave
ö¿ö

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1271
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: December 25, 2012, 03:08:36 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

steps clearly isn't an array, but if you want fifteen steps, then yes, <= is incorrect.
Logged

"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.

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 13
I don't look for the problems. Just for the answers. ö¿ö
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

steps is not an array.
Code:
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
Logged


Dave
ö¿ö

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

Code:
// 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.
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 13
I don't look for the problems. Just for the answers. ö¿ö
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you PaulS,
I did try <=   >= != and so on.  Here is what i found just minutes ago.
I changed
Code:
for (int i=0;i<=steps;i++){
to a
Code:
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

Logged


Dave
ö¿ö

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

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

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 13
I don't look for the problems. Just for the answers. ö¿ö
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged


Dave
ö¿ö

Pages: [1]   Go Up
Jump to: