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).
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?
#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:
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?
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.