Programing help with super simple program

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)
     }   

    }

}

To step the motor the step pin must go high and then back to low (one step).
Have a look at Robin2's simple stepper code.
Also look at the examples that come with the Stepper library to see how to use the library functions.

Also Robin2's stepper basics could be of interest.

You are using the Stepper library for the stepper, why include Accelstepper library.

Please provide us with a schematic. Hand drawn, photographed and posted is fine.

a stepper-motor that is driven by a stepper-driver-modul with direction-input and step-input needs a signal train. Which means a pulse LOW-HIGH-LOW for each step

The state of the direction-input beeing LOW or HIGH determines the rotation-direction
clockwise or counter-clockwise

The step-input needs pulses to know when the next pulse shall occur
a simple switching HIGH does not deliver the information when to do the next step

There are stepper-motor-libraries that reduce the coding-effort for let do the stepper-motor a number of steps to a single line of code

myStepper.doSteps(500);

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.