cant figure it out.

I know the title could be for anything. but i need help.

OK this is how it should work.

I made a dohicky to lower a ball and turn lights on at Midnight for you know when.

well for the most part it works. the switches work. the status LEDs work.
The motor will not spin. NOW i know you will ack is there a part broken .... NOPE i have a sketch that spins the motor fine.

the problem seems to be in the step().
The serial (DEBUG) shows the for loop not working. it just gives me 15

Pressed MotorOnOffSW on 1 15 15 15 15 15 15 15 ........

here is the full code. I need FRESH EYEs. I just cant see the problem.

// This workes.....
// Needs
// Kow many pulses to go 1 inch?:
// GEt MotorOnOffSW & DirectionSW to work together.

// move steper to move camera
// Watch MotorOnOffSW and DirectionSW for HIGH
// IF high change directon of stepper
// Pins
int sleepPin=7;
int dirPin= 8;
int stepperPin=9;

// LED setup
int DirectionLED = 02;                // This LED is for displaying the direction.
int MortorStatusOnOff=06;             // This LED is to show Motor On/Off
int LED1=13;                          // DEBUG LED

// Switches
int DirectionSW=12;                   //  This is held low when HIGH it will change the direction of the Motor
int MotorOnOffSW=11;                  //  This is held low when high it will turn off/on the motor
  
// Vars
int TheDelay= 90;                     // this delay controls the speed higher delay causes the motor to go slower  (MAX 78)
int MotorOnOff = 0;                   //  VAR to show the motor status on/off



int time = 0;           //used to track button presses
int oldTime = 0;        //used to track button presses
int steps = 0;           // set how many pulses to send to the Motor
//int i = 0;


// *************************************************************************************
void setup () {
  pinMode (dirPin, OUTPUT);                // This pin is used for the direction of the motor
  pinMode (stepperPin, OUTPUT);            // This pin is used to move the motor in steps 
  pinMode(sleepPin,OUTPUT);                // This pin is used to put the motor in sleep mode.  Sleep is no power supplied to the motor. (it can spin by hand)
  pinMode (LED1, OUTPUT);                  // LED to show NOT USED YET
  pinMode (MortorStatusOnOff, OUTPUT);     // LED to show the motor status on/off
  pinMode (MotorOnOffSW, INPUT);           // Switch to control on/off of motor. (limit switch for now)
  pinMode (DirectionSW, INPUT);            // Controls the direction of the motor. (used for testing and resiting position)
  pinMode(DirectionLED,OUTPUT);            // Led to show motor direction
  int MotorOnOff = 1;                      // Set motor on
  digitalWrite(MortorStatusOnOff, HIGH);   // Turn on the Status LED
  
  //digitalWrite(dirPin,HIGH);            //
  oldTime = millis();                      // MotorOnOffSW/MotorOnOffSW De-bounce
  //CHSW();                                // Check Switches
  //MotorOnOff = 1;                        // Turn motor on (
  Serial.begin(9600);                      // DEBUG start the Serial port 

} // END setup()
// *************************************************************************************

// *************************************************************************************
void step(boolean dir,int steps){          // spins the motor
  //Serial.println("Steping");             // DEBUG
//  if (MotorOnOff == 0 ) {                // If MotorOnOff = 0 don't run the motor
      dir=dir;                             // BLA
    digitalWrite(sleepPin,HIGH);           // Sleep pin held high to remove stress on motor
    // for testing 
    Serial.println(steps);                  // DEBUG
    //delay(50);                            // Pause for .05 Seconds
    BlinkLED1();                            // For DEBUG to show activity in this function
    int i=0;                                // added this to further set i
        for (int i=0; i<=steps; i++){       // start at zero ; keep looping until true ; keep adding 1 to i
            // for testing 
            //Serial.println(i);
            //CHSW();                          // take time to check the button
            digitalWrite(stepperPin, HIGH);   // Send blip to motor
            delayMicroseconds(TheDelay);      // Pause do the motor can see the blip and react
            digitalWrite(stepperPin, LOW);    // Send blip to motor
            delayMicroseconds(TheDelay);    // Send blip to motor
            digitalWrite(stepperPin, HIGH);  // Send blip to motor
            delayMicroseconds(TheDelay);    // Pause do the motor can see the blip and react
            digitalWrite(stepperPin, LOW);  // Send blip to motor
            delayMicroseconds(TheDelay);  // Pause do the motor can see the blip and react
        } // END for(int i=0;i = steps;i++)
     digitalWrite(sleepPin,LOW);              // Sleep pin held high to remove stress on motor
  // } // END Motor check
} //END step(boolean dir,int steps)
// *************************************************************************************

// *************************************************************************************  
void loop(){
  //CHSW();                                               // take time to check the button
   if (MotorOnOff == 1 ) {step(true,15);}                 // Do stepper in forward direction for 15 times.
   CHSW();                                                // take time to check the button
//BlinkLED1();
      
}  //end loop()
// *************************************************************************************

// *************************************************************************************
void CHSW(){                                               // Monitor the switches
  // Set switch delay
  time = millis();       // Set time = current mills
 
  if ( (digitalRead(DirectionSW) == HIGH)   && ((time - oldTime) > 500) ){          //******************* Button
    oldTime = time;                                                                 // De-bounce delay to let switch settle.
    Serial.print("Pressed DirectionSW ");                                           // DEBUG
    // Reverce the motor
    int val = digitalRead(8);                                                       // Check the direction of the motor
      if (val == HIGH) {                                                            // if CCW then do this
          digitalWrite(8, LOW);
          digitalWrite(DirectionLED, LOW);                                          // Turn off the led to show the motor direction
          Serial.println(" Foward ");                                               // DEBUG
      } else if (val == LOW){                                                       // 
          digitalWrite(8, HIGH);                                                    //
          digitalWrite(DirectionLED, HIGH);                                         //
          Serial.println(" Backward ");                                             //
      }  // end else if
  } else if ( (digitalRead(MotorOnOffSW) == HIGH)   && ((time - oldTime) > 500) ){          //******************* Button
      oldTime = time;                                                                                             // no comment it works..... haha
      Serial.print("Pressed MotorOnOffSW");
      if (MotorOnOff == 1) {  
          MotorOnOff = 0;    // Stop the motor
          digitalWrite(MortorStatusOnOff, LOW);
          Serial.print(" off ");
          Serial.println(MotorOnOff);
          digitalWrite(sleepPin,LOW);
          //delay(100);
      } else if (MotorOnOff == 0) {
          MotorOnOff = 1;    // Run the motor
          digitalWrite(MortorStatusOnOff, HIGH);
          Serial.print(" on ");
          Serial.println(MotorOnOff);
          //delay(100);
      } else {
      // nothing
      }
      
  }
} // END CHSW()
// *************************************************************************************

void BlinkLED1(){                    // Used to help find problems by blinking LED
     digitalWrite(LED1, HIGH);
     delay(100);
     digitalWrite(LED1, LOW);
     delay(100);
}

OK the motor will move but not spin. blip blip.....

Thanks

Dave

  dir=dir;                             // BLA

Interesting. But confusing. The comment doesn't really explain the intent of that line, which is hard to deduce just looking at it.


int time = 0;           //used to track button presses
int oldTime = 0;        //used to track button presses

...

  // Set switch delay
  time = millis();       // Set time = current mills

    if ( (digitalRead(DirectionSW) == HIGH)   && ((time - oldTime) > 500) ){          //******************* Button
    oldTime = time;                                                                 // De-bounce delay to let switch settle.

An int goes up to 32767. That will overflow after 32.767 seconds.

You want "unsigned long".

Great find.

The dir=dir was there to take a clock tick. hoping it would fix the FOR problem.

its funny when look at the longing i did notice minus numbers for time. but the step() did not work so.....

Here is what i found just minutes ago.
I changed for (int i=0;i<=steps;i++){ to a 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

Any insight on why?

Not really. Some people just insist on crossposting.

Try here

in response to: PaulS & AWOL

OK for those who don't read all the text. This is not the same post. I had to keep answering people posting a reply in the other post. Even when i stated "IN THE TITLE"

For loop not working... FIXED. thank you AWOL

So i will elaborate.

In the loop in question i was trying to send 15 pulses to the stepper via Var steps.

Well it was not working. After debugging it i found the problem must be in the loop. So i made a post. AWOL provided the answer. But the stepper still was not moving. so i posted a new POST. (i did put this in the last post in the old post) this one hoping someone would read it and help. This is the place you ask for help?

If other people are here to help then you could have help by stating the problem not making condescending replies. So if i offended people by trying to explain sorry.

Ok back to the reason for the post.

// *************************************************************************************
void step(boolean dir,int steps){          // spins the motor
  //Serial.println("Steping");             // DEBUG
//  if (MotorOnOff == 0 ) {                // If MotorOnOff = 0 don't run the motor
      dir=dir;                             // BLA
    digitalWrite(sleepPin,HIGH);           // Sleep pin held high to remove stress on motor
    // for testing 
    Serial.println(steps);                  // DEBUG
    //delay(50);                            // Pause for .05 Seconds
    BlinkLED1();                            // For DEBUG to show activity in this function
    int i=0;                                // added this to further set i
        for (int i=0; i<=steps; i++){       // start at zero ; keep looping until true ; keep adding 1 to i
            // for testing 
            //Serial.println(i);
            //CHSW();                          // take time to check the button
            digitalWrite(stepperPin, HIGH);   // Send blip to motor
            delayMicroseconds(TheDelay);      // Pause do the motor can see the blip and react
            digitalWrite(stepperPin, LOW);    // Send blip to motor
            delayMicroseconds(TheDelay);    // Send blip to motor
            digitalWrite(stepperPin, HIGH);  // Send blip to motor
            delayMicroseconds(TheDelay);    // Pause do the motor can see the blip and react
            digitalWrite(stepperPin, LOW);  // Send blip to motor
            delayMicroseconds(TheDelay);  // Pause do the motor can see the blip and react
        } // END for(int i=0;i = steps;i++)
     digitalWrite(sleepPin,LOW);              // Sleep pin held high to remove stress on motor
  // } // END Motor check
} //END step(boolean dir,int steps)
// *************************************************************************************

This is how i TEST called it

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

Thank you for the replies.

Don't forget were you came from. We all were novices once.

In the loop in question i was trying to send 15 pulses to the stepper via Var steps.

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

And, as I explained earlier, that is going to give you sixteen pulses.

Inter-Magic:
The dir=dir was there to take a clock tick. hoping it would fix the FOR problem.

The compiler will likely remove redundant code like this... The idea is flawed, FOR works.

Well it was not working. After debugging it i found the problem must be in the loop. So i made a post. AWOL provided the answer. But the stepper still was not moving. so i posted a new POST. (i did put this in the last post in the old post) this one hoping someone would read it and help. This is the place you ask for help?

That's really crossposting - if its a problem with the same set-up, you should keep to the same thread - otherwise
you've duplicated all the context needed to answer the problem - people don't want to have to wade through two
identical descriptions of a situation, you just ask further questions as each point gets fixed, people in on the thread
can contribute as appropriate. Also it means you, the original poster, don't have to track several threads in parallel.

It does mean that the thread might end up in the wrong topic occasionally...

Thanks AWOL,

The problems is that it does not go to 16. It will not count.

My output via serial (within the loop) will not run. It reacts like i <=15 so the loop is true before it gets into the loop. That's why it though maybe steps its reserved word.

I know 'steps' is not a reserved word. But i changed steps for TheDelay (TheDelay = 90) and it counted. so my new output was

15
1 2 3 4 5 6 ...... 90
15
1 2 3 4 5 6 ...... 90
so on ...

So steps would not work but another variable did.

Thanks MarkT,

I do understand, now that is was explained. In my job problems are submitted. Once that problem is fixed it's closed. Even if the fix causes a different problem. This is because the bug in the code could be from a different department.

I will change when it comes to posting in forums.

Thank you once more for replying.

One of the problems I see with your code is that you print numbers to the serial monitor, but you have no way of knowing what those numbers mean. You really should address that.

Serial.print("steps = ");
Serial.println(steps);

conveys far more information than

Serial.println(steps);
    int i=0;                                // added this to further set i
        for (int i=0; i<=steps; i++){       // start at zero ; keep looping until true ; keep adding 1 to i

At this point, there are two variables, in two different memory locations, with the same name. That is really (trust me on this) a good idea.

Oh, and the comment is wrong. It keep looping WHILE the middle clause is true, not UNTIL the middle clause is true.

            // for testing 
            //Serial.println(i);

Isn't that what we're doing here? Testing? Why is this commented out? Of course, the number by itself is useless...

What happens of you comment out (or remove) all of the lines in the step function apart from Serial.println(steps); and Serial.println(i);
Does the for loop work then ? If not then what output do you get, if any ?

Thanks UKHeliBob for the note

That did it. I found (and i did know this) step is a reserved word. Now the 'for loop' was within a function step(); so the compiler could not use it correctly.
I changed that (with the other changes) and it works.

Thank you PaulS,

Yes i misspoke on the UNTIL. good catch

At this point, there are two variables, in two different memory locations, with the same name. That is really (trust me on this) a good idea.

Which two?

Thanks

   int i=0;                                // <<------ here
        for (int i=0; i<=steps; i++){       // <<-------- and here