Preprogrammed stepper movement with trigger input

the project is to take a laser controller (which only has on/off input) to trigger a preset movement on a stepper motor for a pen plotter (move down 1/2" when turned on, move up 1/2" when turned off)

I was able to put together something that seem to work on Wokwi, but when tested the motor would just continuously move back and forth. At times on Wokwi simulation, when triggered, the motor would move back and forth, and the feedback from the printIn seem to indicate a dirty signal. is this the case with real world application? or is there a flaw in how the program was written?

code below:

/*   
 *   Basic pre-programmed code for controlling a stepper without library
 *      
 *   by Eric
 */

// defines pins
#define stepPin 2
#define dirPin 5 
#define bttnPin 8
 int bttn_st=0;
 int lastButtonState=0;


void setup() {
  Serial.begin(115200);
  Serial.println("Hello Arduino\n");
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(bttnPin, INPUT);
  pinMode(9, OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedly:

// read the pushbutton input pin:
  bttn_st = digitalRead(bttnPin);

  // compare the bttn_st to its previous state
  if (bttn_st != lastButtonState) {
    // determine state change HIGH or LOW
    if (bttn_st == HIGH) {
      // if the current state is HIGH then the button went from off to on:
    digitalWrite(dirPin,HIGH); // Enables the motor to move in a HIGH direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(700);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(100); 
      Serial.println("ON");
    } 
  
    } else {
      // if the current state is LOW then the button went from on to off:
       digitalWrite(dirPin,LOW); // Enables the motor to move in a LOW direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(700);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(100); 
    Serial.println("OFF");
    }
    } 
  } else {
  digitalWrite(stepPin,LOW); 
  }
  
  // save the current state as the last state, for next time through the loop
  lastButtonState = bttn_st;

  }

Maybe it is the sequence of steps? Here is a Wokwi, no-code stepper command. To make the motor move in one direction, alternate switch input (on, on, off, off, on, on, off, off, et c.)

Thanks xfpd,

to clarify, when wired up with the motor, the motor is able to move forward or backward completing the required sequence (moving 1/2"), just that it would action on both sequences while input is ON, which indicates that the signal input is on and off even though its supposed to be constantly ON?

Your sketch looks like your button is nominally LOW, but do you have a pulldown resistor on the bttnPin side? Without the pulldown, the bttnPin floats until the button is pressed. The floating could cause false "HIGH" to register in the digitalRead(bttnPin);

Side note: What is on "9" that appears as an output, but not used? (remove it?)

@bluelan8348 - Here is your code in a simulator. I added a "pulldown" resistor to the button and changed two lines for easier Serial Monitor reading.

I didn't put a pulldown resistor because the button was just a simulation, but the real source of input is the digital signal from the laser controller - but I think you are correct, because when I was testing it wired, I was using a simple button contraption, which seems to send false HIGH. I actually just wired it with the laser controller and it works perfectly without false signals. I guess that answered the question...

BTW pin 9 was wired to an LED to test if the button works because I was learning how to code for buttons lol

For future button projects, try Button.h for easier button reading, and try some of the examples.

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