Stepper program single trigger program help request

Good morning ladies and gent. I have taken Robin2 code for controlling a stepper and tried to modify it. Attached here is the modification. It is on a Arduino Uno and yes i am using pull up resistor. What i want to happen is when the clockwise button is pressed it will run the stepper output portion of the program and cycle it 80 times or 80 pulses, then will stop. However the issue i am having is when i press the button, it does run the 80 cycles but then it just keeps repeating the cycle as long as the button is pressed. I was trying to use P as my lock out so that button is pressed and it will only do the cycle 1 time till the button is released and repressed. I have put the scope on my input to make sure it was not toggling, i even tried connecting the input directly to ground. But alas nothing. Maybe there is a better way to accomplish this, like i said i am fairly new to this and figured it cant be much more difficult than PLC. So anyway without further ado here is my code.

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;
int p = 0
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds

void setup() {

  Serial.println("Starting Stepper Demo with millis()");

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonCWpin, INPUT_PULLUP);
  pinMode(buttonCCWpin, INPUT_PULLUP);

void loop() {
	curMillis = millis();

void readButtons() {
	buttonCCWpressed = false;
	buttonCWpressed = false;
	if (digitalRead(buttonCWpin) == LOW and p == 0) {
		buttonCWpressed = true;
	if (digitalRead(buttonCWpin) == HIGH and p == 1) {
		p = 0}
	if (digitalRead(buttonCCWpin) == LOW) {
		buttonCCWpressed = true;

void actOnButtons() {
	if (buttonCWpressed == true) {
		digitalWrite(directionPin, LOW);
	if (buttonCCWpressed == true) {
		digitalWrite(directionPin, HIGH);

void singleStep() {
	for(int cnt = 0; cnt < 81 ; cnt++){
	if (curMillis - prevStepMillis >= millisBetweenSteps) {
		prevStepMillis += millisBetweenSteps;
		digitalWrite(stepPin, HIGH);
		digitalWrite(stepPin, LOW);

I think you have adapted the code in an inappropriate way. Rather than change the singleStep() function to do multiple steps you should have a piece of code to call singleStep() multiple times. A simple way may be to have a variable that counts the steps. Something like this

void multiStepCW() {
    if (stepCount < stepMax) {
       stepCount ++;
       digitalWrite(directionPin, LOW);

Then, when you want to cause another set of steps all you need to do is set stepCount to zero.

I strongly recommend the use of meaningful variable names such as stepsCompleted rather than P


Ok well that i am sure will make the steps better but how in your opinion would it be better to do what i will refer to as a 1 shot. Or trigger in rising edge.

As I said i want to only run the code 1 time, and that is on a rising edge. SO long as the button stays pushed the code will execute and run the stepper 80 pulses and then wait for the next rising edge.

Is that possible with the Arduino? I do hope that i have made myself clear here.

Thanks again.

Of course it is possible to have a one-shot system.

What I have in mind is that it will more obvious how to implement it if you go about the project as I suggested. When I looked at your program I was finding it very difficult to see the wood for the trees.

Try moving p=1 to here

if (buttonCWpressed == true) {
		digitalWrite(directionPin, LOW);