I need to use a switch to start my stepper motor turn it 3 revolutions then stop

Hello

Complete new person here. I need to use a foot switch (on a pull up resistor on A0) to start a program that will rotate my stepper motor 3 revolutions, then stop and wait for another push of the foot switch.

The foot switch is on A0
The controller is a Tb6600-4.0 set to 1 on microstep and 2.5 amps
The stepper is a zyltech 23hd76002Y-2113

The pul - goes to digital pin 7
The dir - goes to digital pin 6
No enable set up

I'd like to turn relatively fast

I can use various stepper examples to get the motor running and it does, but I have NO clue how to put this into a loop to turn the motor x amount of steps then wait for another push of the foot switch.

Any ideas for this clueless old guy?

BBWOLF

Please attach code using code tags and wiring diagram.
Your Shakespeare type wirings will not show what might be missing.

Start by getting the Arduino cookbook and reading it. Also on line there are many videos etc showing the Arduino and how to do what you want.

BBWOLF:
Complete new person here. I need to use a foot switch (on a pull up resistor on A0) to start a program that will rotate my stepper motor 3 revolutions, then stop and wait for another push of the foot switch.

Think about the problem as separate pieces and it becomes easier.

  • Use the foot switch to change a variable - perhaps called motorStart - to true
  • Only operate the motor if that variable is true.
  • When the motor has completed 3 revolutions set that variable back to false

...R
Stepper Motor Basics
Simple Stepper Code

Robin2

THANK YOU!!

Using your insightful recommendations, I cut up droneworkshop’s code to do the 3 revolutions (I have it geared down therefore the 11 revolutions of the motor), then I went to examples and found BUTTON. Cut that up for my purposes did a nested loop (something I seem to remember from 1970 in a long forgotten FORTRAN class using punch cards) and actually made it WORK!

Gratefully I didn’t have to tackle debounce

Below is the spaghetti code, but it works. Please ignore most of the comments as I didn’t change them.

/*
  Stepper Motor Demonstration 4
  Stepper-Demo4.ino
  Demonstrates NEMA 17 Bipolar Stepper with A4988 Driver

  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/

// Define Constants

// Connections to A4988
const int dirPin = 6;  // Direction
const int stepPin = 7; // Step
const int buttonPin = 2;
int buttonState = 0; 

// Motor steps per rotation
const int STEPS_PER_REV = 200;
 
void setup() {
  
  // Setup the pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
    
  // Set motor direction clockwise
  digitalWrite(dirPin,LOW); 

 // Read state of buttonPin
buttonState = digitalRead(buttonPin);
 if (buttonState == LOW) {   
  
  // Spin motor two rotations quickly
  for(int x = 0; x < (STEPS_PER_REV * 11); x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(750);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(750);
  }
 
 digitalWrite(buttonPin,HIGH); 
 }


}

Any code doing what You want is good code!
What would beautiful code look like? Compressed, optmized, advanced to a level wheere "nobody" could understand it?

I also started programming using punched cards. First it was a programmable desktop(?) calculator, then Fortan. Have You tampered with the Watfive compiler?

Keep up the steam! Old knowledge nerver dies, it only falls asleep now and then!

Robin2

Now I am dangerous. What i asked for turned out not to be what I needed. So I did a little checking and found a While loop.

Now the motor turns only when the foot switch is pressed. Again thankfully debounce has not reared its ugly head.

Thanks again

The attached is the code for the while loop.

/*
  Stepper Motor Demonstration 4
  Stepper-Demo4.ino
  Demonstrates NEMA 17 Bipolar Stepper with A4988 Driver

  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/

// Define Constants

// Connections to A4988
const int dirPin = 6;  // Direction
const int stepPin = 7; // Step
const int buttonPin = 2;
int buttonState = 0; 

// Motor steps per rotation
const int STEPS_PER_REV = 200;
 
void setup() {
  
  // Setup the pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
    
  // Set motor direction clockwise
  digitalWrite(dirPin,HIGH); 

 // Read state of buttonPin
buttonState = digitalRead(buttonPin);

 while (buttonState == LOW) {   
  
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(750);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(750);
    buttonState = digitalRead(buttonPin);
  }
 

 }