1

In the code below I would like the stepper to bounce back and forward between two limit switches and while it is doing this it should turn and than wait for e.g. 1 second.

I thought I could achieve this with the if ((unsigned long)(currentMillis - previousMillis) >= interval) part, but it doesn't work.It won't turn. Any help would be great. Thank you.

// defines pins numbers
const int stepPin = 5;
const int dirPin = 4;
const int enPin = 12;
const int homeSwitchPin = 9;
const int homeSwitchPin2 = 11;

unsigned long interval=1000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

void setup() {

   Serial.begin(9600);

  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(homeSwitchPin , INPUT);
  pinMode(homeSwitchPin2 , INPUT);
  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);

  // Set Dir to Home switch
  digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction


}
void loop() {

    int homeSw = digitalRead( homeSwitchPin);
    int homeSw2 = digitalRead( homeSwitchPin2);

    if( homeSw == HIGH && (digitalRead(dirPin) == LOW) ){
        unsigned long currentMillis = millis();

        if ((unsigned long)(currentMillis - previousMillis) >= interval) {
        motorStep(1);
        previousMillis = millis();
        }
    }
    else if( homeSw == LOW && (digitalRead(dirPin) == LOW) ){
          digitalWrite(dirPin,HIGH);
          delay(2000);
    }


    if( homeSw2 == HIGH && (digitalRead(dirPin) == HIGH)){

        motorStep(1);
        // motorStep(32000);
        //digitalWrite(dirPin,LOW);

        //delay(2000);
    }
    else if( homeSw2 == LOW && (digitalRead(dirPin) == HIGH) ){
          digitalWrite(dirPin,LOW);
          delay(2000);
    }

}
void motorStep( int MAX){

   for(int x = 0; x < MAX; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(100);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(100);
      }
}
6
  • Do the home switch pins have pull-up resistors on them? Your current code only makes the motor move one single step every second. So it's hardly moving. Are you sure it's not moving very slowly? – Gerben Feb 20 '19 at 15:33
  • how many times do you want the motor to bounce back and forward between waits? ...... your description is unclear – jsotola Feb 20 '19 at 18:45
  • Endless bounce back and forward, within that it should do other routines. @gerben maybe, but my question is how I can change that so it moves according to my routine. I need additional routines and therefore it would be great to know how to set this up. Thanks. – digit Feb 21 '19 at 5:31
  • Have you tried spamming your code with print()s to determine if you are actually hitting the contents of the if() statement? – Tom Feb 21 '19 at 10:36
  • 1
    I would like to solve this with millis() because I might have to integrate more than one stepper and for this using millis() is recommended even by Arduino. This is an excellent example of something I would call, in the lack of a better word, "best practices algorithms". I understand most of the lower level function, but lack the knowledge for these higher level algorithms and how they should be used. I would like to know where I can learn these most needed higher level algorithms for Arduino. Apart from asking on these forums. – digit Feb 25 '19 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.