Why is the function ignored in setup?

When running this sketch the stepperHome() function request is completely ignored in setup() and goes straight to loop, any ideas why? Am new and spent all day reaching this conclusion after dissecting this code…

Any ideas?

#include <AccelStepper.h>

AccelStepper stepper1(1, 9, 8);

const int homeButton = 2;
const int ledPin = 13;
byte hBval;


void setup(){
  stepper1.setMaxSpeed(500); //nice and slow for testing
  stepper1.moveTo(-3200);
  stepper1.setAcceleration(500);
  pinMode(homeButton, INPUT);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(5000); // random position to end for testing
  stepper1.runToPosition();
  delay(1000);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(1000);
}
//contributed by Runaway Pancake 9/2/13
void stepperHome(){ //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-3200);
    stepper1.run();
    digitalWrite(ledPin, LOW); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}

moderator: added code tags == # button above smileys

add some serial print debugging:

void setup()
{
  Serial.begin(115200);
  stepper1.setMaxSpeed(500); //nice and slow for testing
  stepper1.moveTo(-3200);
  stepper1.setAcceleration(500);
  pinMode(homeButton, INPUT);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void stepperHome(){ //this routine should run the motor
  hBval = digitalRead(homeButton);
  Serial.print("sensor reads:");
  Serial.println(hBval==HIGH? "HIGH" : "LOW");
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-3200);
    stepper1.run();
    digitalWrite(ledPin, LOW); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}

Hi sgilkes

hBval = digitalRead(homeButton);
while (hBval == HIGH)

What kind of switch are you using? How is the switch connected?

Regards

Ray

add some serial print debugging:

.... and some code tags so that

AccelStepper stepper1(1, 9, 8);

... doesn't appear as

AccelStepper stepper1(1, 9, 8);

Hi Ray,

Its a three pin long lever bump switch, see below,

http://www.onlinecomponents.com/omron-electronics-v1531c25.html?p=12057576

Its connected to digital pin 2 and gnd.

If I remove everything from the void loop() and place stepperHome() on its own to test, the call is made and the switch works as planned.

Best regards Simon

Its connected to digital pin 2 and gnd.

Are you using the "normally open" pair of contacts, so that pin 2 becomes connected to GND when the switch is operated?

If so, do you have a pullup resistor fitted between pin 2 and +5V?

If not, try changing the pinMode statement to this:

pinMode(homeButton, INPUT_PULLUP);

Hi Hackscribble,

Perfect! Thank you so much.

Thank you to all that helped, very much appreciated.

Best regards
Simon