Press Button and stepper motor!

Hi all!

i used this tutorial: (or the code longer down in message)

and the "Moving when a button is pressed" example, and it works fine!!

BUT.. i want the motor to move a spesific amount of steps when i press the button once!..
now the motor moves infinitly if i hold the button down..

any help for this? tips?

Here is the code:

#define DISTANCE 10

int StepCounter = 0;
int Stepping = false;

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);

pinMode(3,INPUT);
}

void loop() {
if (digitalRead(3) == LOW && Stepping == false)
{
Stepping = true;
}

if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}

any help for this?

If you want to do something when the pin the button is sewn onto changes state, then looking at the state change detection example seems like a reasonable thing to do.

It's just doing what you asked. When the step counter equals DISTANCE it sets Stepping false then goes to the top of the loop and immediately sets Stepping true again if the button is still pressed.

You need to be looking for the button changing to pressed not just being in the pressed state. Have a look at the Digital / StateChangeDetection example sketch in the IDE.

Steve

I was looking at it now..

im not a coder (im learning), so i need some time to get into it i think..

wow.. i cannot figure out how to implement the loop into my code...

sorry for noobing!

djheinz:
wow.. i cannot figure out how to implement the loop into my code...

Post the program that represents your best attempt and tell us in as much detail as possible what it actually does.

It is much easier to help when we can see what you are trying.

...R
Stepper Motor Basics
Simple Stepper Code

#define DISTANCE 200

int StepCounter = 0;
int Stepping = false;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);

pinMode(3,INPUT);
Serial.begin(9600);

}

void loop() {
if (digitalRead(3) == LOW && Stepping == false)
{
Stepping = true;
}

if (Stepping == true)
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}

The code in Reply #6 won't work because there is a variable called buttonPin that has not been defined.

To make it easy for people to help you please use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

i have fixed that now..

but will this work?

#define DISTANCE 200

int StepCounter = 0;
int Stepping = false;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {                
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

  pinMode(3,INPUT);
  Serial.begin(9600);
  
}

void loop() {
  if (digitalRead(3) == LOW && Stepping == false)
  {
    Stepping = true;
  }

  if (Stepping == true)
  buttonState = digitalRead(3);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  {
    digitalWrite(9, HIGH);
    delay(1);          
    digitalWrite(9, LOW); 
    delay(1);

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE)
    {
      StepCounter = 0;
      Stepping = false;
    }
  }
}

What happened when you tried it?

Referring to Reply #8 ...

The code in lines 29 to 46 seems to have no relationship with any other part of the program

It would have been better to keep the variable buttonPin but define it to be 3. Code such as buttonState = digitalRead(buttonPin); makes more sense than buttonState = digitalRead(3);. And if the value is just defined in one place there is much less scope for silly typos that the compiler cannot detect.

...R