Button pressed - onestep forward

Hi,

I need to do a 180° on a stepper but rotating around 6° everytime I press a button - going back 6° if i press another button (it’s for stop-motion).

My problem is that it does not just go one step forward, it will go that direction but will stop as soon as I release, and will continue further if I stay longuer than a quick press.

I have been going around the forum and read about debouncing (i feel this is what i need to put in place ?) but have trouble putting it together. I’m a beginner, obvisouly.

If someone could help me, i’d be very glad.

Thank you

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <AccelStepper.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 



int LED = 13;
int LEDjaune = 12;
int BUTTON = 2;
int BUTTONjaune = 3;


Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup(){

 
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin();
myMotor->setSpeed(1);  // 10 rpm  


pinMode(LED,OUTPUT);

pinMode(BUTTON,INPUT);

pinMode(LEDjaune,OUTPUT);

pinMode(BUTTONjaune,INPUT);




}

void loop(){

if(digitalRead(BUTTON) == HIGH){

digitalWrite(LED,1);
Serial.println("Microstep steps");
  myMotor->onestep(FORWARD, MICROSTEP);
  myMotor->release();
    //delay(10000);



}else{

digitalWrite(LED,0);
 
}

if(digitalRead(BUTTONjaune) == HIGH){

digitalWrite(LEDjaune,1);
Serial.println("Microstep steps");
  myMotor->onestep(BACKWARD, MICROSTEP);
  myMotor->release();


}else{

digitalWrite(LEDjaune,0);
 
}


}

Hi tiffany,
Before you post code, hit CTRL T in the IDE to autoformat. It makes the code much easier to read.
Also, I personally prefer less white space.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <AccelStepper.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

int LED = 13;
int LEDjaune = 12;
int BUTTON = 2;
int BUTTONjaune = 3;

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  AFMS.begin();
  myMotor->setSpeed(1);  // 10 rpm
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
  pinMode(LEDjaune, OUTPUT);
  pinMode(BUTTONjaune, INPUT);
}

void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    digitalWrite(LED, 1);
    Serial.println("Microstep steps");
    myMotor->onestep(FORWARD, MICROSTEP);
    myMotor->release();
    //delay(10000);
  } else {
    digitalWrite(LED, 0);
  }
  if (digitalRead(BUTTONjaune) == HIGH) {
    digitalWrite(LEDjaune, 1);
    Serial.println("Microstep steps");
    myMotor->onestep(BACKWARD, MICROSTEP);
    myMotor->release();
  } else {
    digitalWrite(LEDjaune, 0);
  }
}

Yes, your code looks like it only acts when either button is high, moving a single microstep.
Do you know how many microsteps it takes to move 6 degrees?

Hi, thanks a lot.

Sorry about the CTRL T.

it’s a NEMA 17 with 200 steps per revolution so between 3 to 4 normal steps (one step being 1.8°)
I don’t know how it translates to microsteps but I can manage if it’s more or less. I’m fine with that, i don’t really need the 6° precision, rather keeping the same distance.

My real problem is the stepping not stopping clearly or not completing fully.

Thanks for trying to help.

My problem is that it does not just go one step forward, it will go that direction but will stop as soon as I release, and will continue further if I stay longuer than a quick press.

My real problem is the stepping not stopping clearly or not completing fully.

I'm unclear about what you what to happen. Do you want to move a fixed number of steps on a button press independent of the time it is held down? or, do you want to continue to move as long as the button is held down?

Hi,

First one : I need it to move a certain specific amount of steps on a button press independant of the time it is held down.

Currently :

when I press fast, it starts the onestep but doesn't complete.
if I press 1 second, it will do the onestep, slow down (i guess the release()), and start another one and so on

That is why i said it doesn't complete fully or doesn't stop after that single step

The key is recognizing the state change. The Arduino is infinitely faster than you are. It can read the button condition thousands of times per second.

Now just one of those times, the button is pressed where it wasn't previously pressed. Then you want to move the motor by the defined number of steps.

Look in the Arduino examples, under "Digital" you will find StateChangeDetection. Try to modify that to do what you want to do.

Alright thanks will do.