Hey guys, first time using Arduino and I'm running into some problems. I have a stepper motor attached to a lead screw to make something rise up. The goal is to push a button, the unit rises up, stays up, and then when the button is pushed again, the unit lowers back down to its home position. But, when I pushed the button nothing happened. My code is as followed. Please advise.
#include <AccelStepper.h>
#define button 2
const int dirPin = 2;
const int stepPin = 3;
// Define motor interface type
#define motorInterfaceType 1
#include <Stepper.h>
const int stepsPerRevolution = 42;
// number of steps per revolution
// bipolar stepper motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
int button_state = 0;
int table_state = 0;
void setup() {
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// initialize the serial port:
Serial.begin(9600);
pinMode(button, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Small delay for computer rest (Delays are in milliseconds)
delay(50);
button_state = digitalRead(button); // Checking if the button is pressed
// Loop to raise table
if (button_state == HIGH && table_state == 0) { // If the button is pressed and the table is in down position...
// Moving the vertical motor
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
// Spin motor slowly
for(int x = 0; x < 42*20; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
\
table_state = 1; // Telling the program that the table is raised
}
button_state = digitalRead(button); // Checking if the button is pressed
// Loop to lower table
if (button_state == LOW && table_state == 1) { // If the button is pressed and the table is in up position...
// Moving the vertical motor
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
table_state = 0; // Telling the program that the table is lowered
}
else if (button_state == HIGH) {
delay(10); // (Almost non-existant delay, but give something for the computer to run if the button isn't pressed)
}
}
}