Help with Void Setup

I’m using two buttons to act as limit switches for two stepper motors. The idea is when the code uploads (or when the power is turned on), the first stepper motor will keep on turning until the first button is pressed. Then the second stepper will start turning until the second button is pressed. So I wrote a code for it in the Void Setup() section.

The problem with the code is that the first break statement for the first stepper is ending the entire void setup rather than just only the loop inside the void setup. That means pressing the first button ends the entire void setup without needing the second button to be pressed. Is there a way to fix this?

Here is the void setup section of my code:

// defines pins numbers
const int stepPin1 = 7;
const int dirPin1 = 6;
const int stepPin2 = 9;
const int dirPin2 = 8;
const int ledPin = 11;

//define limit switch pin
const int limitSwitch1 = 3;
const int limitSwitch2 = 5;
int limitReading1;
int limitReading2;

void setup() {
  Serial.begin(9600);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial) {
    ;
  }
  Serial.println("Welcome! :)");
  
  // Sets the two motor pins as Outputs
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);

  //setting stepper one to home position
  limitReading1 = digitalRead(limitSwitch1);
  while (limitReading1 == LOW) {
    digitalWrite(ledPin, LOW);
    digitalWrite(dirPin1, LOW);
    // Makes selected number of pulses
    for (int x = 0; x < 200000; x++) {
      //to stop motor immediately if button is pressed
      limitReading1 = digitalRead(limitSwitch1);
      if (limitReading1 == HIGH) {
        digitalWrite(ledPin, HIGH);
        break;
      }
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(300);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(300);
    }
  }

  delay(250);

  //setting stepper two to home position
  limitReading2 = digitalRead(limitSwitch2);
  while (limitReading2 == LOW) {
    digitalWrite(ledPin, LOW);
    digitalWrite(dirPin2, LOW);
    // Makes selected number of pulses
    for (int x = 0; x < 200000; x++) {
      //to stop motor immediately if button is pressed
      limitReading2 = digitalRead(limitSwitch2);
      if (limitReading2 == HIGH) {
        digitalWrite(ledPin, HIGH);
        break;
      }
      digitalWrite(stepPin2, HIGH);
      delayMicroseconds(300);
      digitalWrite(stepPin2, LOW);
      delayMicroseconds(300);
    }
  }
}

I don't see any initialization of pinMode for the limitSwitch1 and limitSwitch2 pins. Now if limitSwitch2 pin exists in a pullup-state ("HIGH") that would explain your problem...

pinMode(limitSwitch1,INPUT);
pinMode(limitSwitch2,INPUT);

... in setup(), should hopefully fix this.

How did you connect the limit buttons to the Arduino? In your code it looks as if they are set to GND (I hope, via a resistor) and get HIGH, when the button(limit switches) are pressed?

You can simplify your peripherals by just connecting one end of the limit buttons to GND and the other end(s) directly to the input pin(s). Then you set their respective internal pullup resistors HIGH and your code has then to listen to a LOW, when the button(s) get pressed.

I asked, because I couldn't see an obvious mistake in your code at first short sight - but it is not clear what your input pins "see" - HIGH / LOW or something in between...

But the above mentioned method ensures that you get undoubtable booleans at the input pins when the Arduino has booted and gets to your initial stepper setup routine.

Edit: just saw @Rupert909's post; he is pointing to the same direction

Opps. I put limitSwitch1 and limitSwitch2 as INPUT in the initialization. Now I put pullup state as LOW for both limit switch. Still have the same problem…

// defines pins numbers
const int stepPin1 = 7;
const int dirPin1 = 6;
const int stepPin2 = 9;
const int dirPin2 = 8;
const int ledPin = 11;

//define limit switch pin
const int limitSwitch1 = 3;
const int limitSwitch2 = 5;
int limitReading1 = LOW;
int limitReading2 = LOW;

//define motor speed
int motorDelay = 1200;  //in microseconds

//For resetting positions for new entry
String reset = "Y";

void setup() {
  Serial.begin(9600);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial) {
    ;
  }
  Serial.println("Welcome! :)");
  
  // Sets the two motor pins as Outputs
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(limitSwitch1, INPUT);
  pinMode(limitSwitch2, INPUT);
  
  //setting stepper one to home position
  limitReading1 = digitalRead(limitSwitch1);
  limitReading2 = digitalRead(limitSwitch2);

  while (limitReading1 == LOW) {
    digitalWrite(ledPin, LOW);
    digitalWrite(dirPin1, LOW);
    // Makes selected number of pulses
    for (int x = 0; x < 200000; x++) {
      //to stop motor immediately if button is pressed
      limitReading1 = digitalRead(limitSwitch1);
      if (limitReading1 == HIGH) {
        digitalWrite(ledPin, HIGH);
        break;
      }
      digitalWrite(stepPin1, HIGH);
      delayMicroseconds(300);
      digitalWrite(stepPin1, LOW);
      delayMicroseconds(300);
    }
  }
  delay(250);

  //setting stepper two to home position
  while (limitReading2 == LOW) {
    digitalWrite(ledPin, LOW);
    digitalWrite(dirPin2, LOW);
    // Makes selected number of pulses
    for (int x = 0; x < 200000; x++) {
      //to stop motor immediately if button is pressed
      limitReading2 = digitalRead(limitSwitch2);
      if (limitReading2 == HIGH) {
        digitalWrite(ledPin, HIGH);
        break;
      }
      digitalWrite(stepPin2, HIGH);
      delayMicroseconds(300);
      digitalWrite(stepPin2, LOW);
      delayMicroseconds(300);
    }
  }
}

Oops oops.

After:

  pinMode(limitSwitch1, INPUT);
  pinMode(limitSwitch2, INPUT);

insert the following lines: limitSwitch1 = LOW; limitSwitch2 = LOW;

If that doesn't work we should talk about your wiring of the buttons.

rpt007: How did you connect the limit buttons to the Arduino?

I connected it just like the image in Arduino tutorial page on button: https://www.arduino.cc/en/Tutorial/Button except I only have a 1K ohm resistor instead of a 10k ohm resistor. Also the 5V pin on the arduino is shared between the two buttons.

rpt007: insert the following lines: limitSwitch1 = LOW; limitSwitch2 = LOW;

That won't work. Those variables are const:

const int limitSwitch1 = 3;
const int limitSwitch2 = 5;

You mean:

digitalWrite (limitSwitch1, LOW);
digitalWrite (limitSwitch2, LOW);

However LOW is the default state for all pins.


I don't see any initialization of pinMode for the limitSwitch1 and limitSwitch2 pins.

The default mode for all pins is input.

rpt007: After:

  pinMode(limitSwitch1, INPUT);
  pinMode(limitSwitch2, INPUT);

insert the following lines: limitSwitch1 = LOW; limitSwitch2 = LOW;

That will prevent it from working for sure - this will set the two variables that you're using to refer to the pin number equal to LOW (ie, 0), so they will no longer be pointing at the right pins. Or that's what would happen, except the compiler would throw an error here, because limitSwitch1 and limitSwitch2 are declared as const - so assigning a new value to them is an error.

Maybe you meant to digitalWrite() the pins LOW? But you don't need to do that - setting the pinMode() to INPUT will do that anyway.

How's it wired?

Hand drawn schematic is fine (no fritzing, we hate that here)

Nick, you are right.

It is quite late here in Europe so I might better go to sleep to avoid more LOWs from my brain ;-)

But I can't avoid to state that I can't understand why people don't use the internal pullups. It makes the wiring so much easier. Or is there a technical reason not to use the internal pullups?

The first break is unnecessary I think. You do not need a break statement in an if statement, it will just automatically exit the if statement. Break would exit the function, in this case setup(). This is the case for both your if statements.

Let me know if this helps.

tyler_newcomb: The first break is unnecessary I think. You do not need a break statement in an if statement, it will just automatically exit the if statement. Break would exit the function, in this case setup(). This is the case for both your if statements.

Let me know if this helps.

I would expect the break to exit the FOR loop....

Why don't you print some debug statements to serial so you know what it's doing when it fails?

I solved the problem! Button two was wired wrong! (SMH) It was hard to tell with all the wiring around my breadboard.

I was able to figure out the problem by placing a bunch of Serial.println() in different areas of the code. It was deduced to the fact that button two was always HIGH so it immediately ended the second loop just as it began. So I looked at the second button and noticed it was connected wrong.

Thanks for all your guys' help! :)

tyler_newcomb: The first break is unnecessary I think. You do not need a break statement in an if statement, it will just automatically exit the if statement. Break would exit the function, in this case setup(). This is the case for both your if statements.

Let me know if this helps.

return exits a function. break just exits a for or while loop.

DrAzzy: I would expect the break to exit the FOR loop....

Why don't you print some debug statements to serial so you know what it's doing when it fails?

I stand corrected, the break would exit the FOR loop (at least I am pretty sure). But either way it is unnecessary correct? Anyways, glad you solved the problem!

rpt007: Or is there a technical reason not to use the internal pullups?

The internal pull-ups are quite weak (around 50 k) - with short wiring runs they are probably fine, but might be subject to noise if you tried to use them for a longer distance.

I usually use them myself, if the switch is near to the pin.

Thanks Nick,

that information is valuable for me. Normally I have only short wires (10 .. 30 cm) in my projects. But when I should have to use longer wires I hopefully remember your explanation, when I am faced with unexpected reactions of my project.

Schematics is one thing, electrical laws tend to add "invisible" devices to it if you don't take them into account.