Calibration setup help please

Hello,
I am in the final stages of setting up my frameless laser harp. I am using the 28byj-48 stepper motor with an arduino uno.

I need to set the motor at a "home" position before running the loop, so I would simply like the motor to spin in one direction until a pushbutton is pressed, stopping the motor and sending it the the loop. I made the following code, however, when I place this in the void setup section, it seems to just skip over it when I go to run it. (The PB is connected to A1 as all of my digital I/O's are taken up by other components). Where am I going wrong? Also, how do I make the motor turn indefinitely until the button is pressed? ( I know I need to change 16 to something else).

if (digitalRead(button1) == LOW)
  {
    digitalWrite(13, HIGH);
    myStepper.step(16);
  } 
  else (digitalRead(button1) == HIGH);
  {
    delay(1000);
    digitalWrite(13, LOW);
    goto Initialize;
  }

Thank You!

So basically I just want the stepper motor to rotate in one direction continuously until I press a button on A1, then after that go to the loop program. I'm not sure how to get the motor to step while looking for the button press?

I'm not sure how to get the motor to step while looking for the button press?

One way:
The operative word in your question is "while".

while
https://www.arduino.cc/en/Reference/While

The simplest idea would be to use a while loop at the end of setup. While the button is not pressed, step the motor.

Assuming active LOW button:

while(digitalRead(buttonPin) == HIGH) {  // while button not pressed
   // take one step with the motor
}

@LarryD with the ninja post. So there's two votes for that solution.

Thanks both of you. So like this??

#include <Stepper.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 7, 5, 4, 3, 2);


#define NOTE_C7  2093  //BEAM 1
#define NOTE_D7  2349  //BEAM 2
#define NOTE_E7  2637  //BEAM 3
#define NOTE_FS7 2960  //BEAM 4 
#define melodyPin 6

const int stepsPerRevolution = 16;  // change this to fit the number of steps per revolution for the 28byj-48 stepper motor.
int LaserPin = 13;                  // tells the Arduino that the laser is on pin 13
int sensor = 500; // change this value to calibrate your harp's sensor
int calPin = A1;
int direction = 1;

int note4 = 0x70;
int note3 = 0x71;
int note2 = 0x40;
int note1 = 0x47;

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);  //Step sequence

int stepCount = 0;         // number of steps the motor has taken
int a, b, c, d = 0;        // Initating the note status markers.

void setup() {
  pinMode(LaserPin, OUTPUT);
  pinMode(calPin, INPUT);
  int button1 = digitalRead(A1);

  digitalWrite(A1, LOW);
  digitalWrite(13, HIGH);

  while(digitalRead(calPin) == HIGH) {  // while button not pressed
   myStepper.step(1);
  }

Initialize:

Oh and AI button is active LOW, so same code as above, but with == LOW instead of HIGH in the while statement.

You'd use ==LOW in the while if your button was active HIGH (ie pin goes HIGH when button pressed). If the button is active LOW (ie pin goes LOW when button pressed) then you want ==HIGH in the while. Whichever one means a not pressed button.

Everything looks like its working right! the only thing left is that the button seems to give a HIGH sometimes when its not pressed. Does it need to be debounced? Isn't there something built in to fix this?

jland85:
Everything looks like its working right! the only thing left is that the button seems to give a HIGH sometimes when its not pressed. Does it need to be debounced? Isn't there something built in to fix this?

Does it have a pulldown resistor? If not it is floating when not pressed and can read whatever it wants to HIGH or LOW.

Yes there is a built in solution. The chip has builtin pullup resistors. You can use pinMode with INPUT_PULLUP to enable them. But this means that the button must connect to ground when pressed and therefore pressed buttons are LOW and unpressed are HIGH. Active low buttons like that are a better design anyway.

Thanks again Delta_G. I figured out that with the last code I posted, I have to hold the button down for it to read LOW, then release to give it a HIGH. Does that seem correct? I thought it would have the opposite effect. I do have it connected to ground, maybe thats why...

I can make it work like that though. I'm curious if I'm supposed to have it connected to Vcc instead though for the effect I want.

Many thanks!

-James

It doesn't matter as long as your code knows which is which. The better method is for the button to be between the pin and ground so it reads LOW when pressed like you say you have it. That also allows you to use the internal pullup resistors. It just tends to confuse n00bs who seem to need HIGH to be ON and PRESSED and LOW to be OFF and UNPRESSED so you see a lot of code written for beginners using active HIGH buttons. But if you'll notice, most of the code written by the seasoned vets use active LOW buttons. If you go look at any of my code you'll find that a pressed button always reads LOW and unpressed is HIGH.