Join stepper motor and button code.

Hi.

I just got my stepper motor running with this code.

/********************************************************
**         More info about the project at:             **
**  http://lusorobotica.com/index.php?topic=106.0  **
**   by TigPT         at         [url=http://www.LusoRobotica.com]www.LusoRobotica.com[/url]  **
*********************************************************/
int dirPin = 2;
int stepperPin = 3;

void setup() {
  pinMode(dirPin, OUTPUT);
  pinMode(stepperPin, OUTPUT);
}

void step(boolean dir,int steps){
  digitalWrite(dirPin,dir);
  delay(50);
  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepperPin, LOW);
    delayMicroseconds(100);
  }
}

void loop(){
  step(true,1600);
  delay(500);
  step(false,1600*5);
  delay(500);
}

Now I want to join the button code. So I can controll the motor. Ex. One push one relovution.
But I dont really know how I’m gona manage this with the code abowe.

I thought I would be pretty simple, my “solution”;

const int buttonPin = 7;     // the number of the pushbutton pin
int dirPin = 2;
int stepperPin = 3;

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  pinMode(dirPin, OUTPUT);
  pinMode(stepperPin, OUTPUT);
  pinMode(buttonPin, INPUT);     
}

void step(boolean dir,int steps){
  digitalWrite(dirPin,dir);
  delay(50);
  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepperPin, LOW);
    delayMicroseconds(100);
  }
}

void loop(){
  buttonState = digitalRead(buttonPin);
    
  if (buttonState == HIGH) {    
      step(true,1600);
  } 
  else {
      step(false,1600);
  }
  
}

Thanks for your help! KCCO

But I dont really know how I’m gona manage this with the code abowe.

Do you KNOW what that code does? Have you made changes and observed the effects?

If so, making the motor step once each time you press the switch is trivial.

You are stepping one way while the switch is held down (assuming you wired the switch correctly) and stepping the other way when the switch is released. Is that what you want?

What does that code actually do, and how does it differ from what you want it to do?

Just so behaves stepper motor now. What I want is that the stepper motor to be disabled. Then press the button and release it spins the engine one turn.

I did not know where I would start, hence I took the code above.

What I want is that the stepper motor to be disabled. Then press the button and release it spins the engine one turn.

You need to detect when the switch transitions from released to pressed, and from pressed to released. This requires, of course. keeping track of the previous state of the switch.

int prevState = LOW;
int currState;

void loop()
{
   currState = digitalRead(somePin);
   if(currState != prevState)
   {
      // A transition occurred
   }
   prevState = currState;
}

Where the transition occurs, the current state tells you whether the transition was from released to pressed or from pressed to released. You can then step when the switch is pressed or when it is released. Your choice.

Thanks!

I found this code that also works great. I hope it is easer to implement the (new)stepper code in this.

int dirpin = 2;
int steppin = 3;
int button = 7;
int val = 0;


void setup() {


  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(button, INPUT);

}
void loop()
{

  val = digitalRead(button);

  if (val == HIGH)
  {
    int i;

    digitalWrite(dirpin, HIGH);   
    for (i = 0; i<1; i++)
    {
      digitalWrite(steppin, HIGH);  
      digitalWrite(steppin, LOW);
      delayMicroseconds(200);
    } 
  }
}

I hope it is easer to implement the (new)stepper code in this.

That also steps for as long as the switch is pressed, in a very dumb way.

How many times will that loop execute?

It depends on how often it receives the command from outside.

What I build is a cat feeder. I will have an ethernet shield that reads a feeding schedule from my server. Then on the cat feeder, it will be a push button to force feeding.

Right now I'm just trying to get the foundation of the system work.

It depends on how often it receives the command from outside.

There are two pronouns in that sentence that have no referents. As is, it makes no sense.

Quoting what you are replying to might have resolved one of the referents. Though, not likely.

it will be a push button to force feeding.

So, you want the Arduino to do something, once, when the switch is pressed (and held) or continuously as the switch is held.

You need to clarify your requirements.

I do not know how more clear I can be but I will try again.

Press the button and release. The stepper motor turns one revolution.

So, 1) wait for a "button down" transition. 2) wait for a "button up transition. 3) rotate motor.

I got it to work now. But is this the proper way to do it?

int dirpin = 2;
int steppin = 3;
int button = 7;
int val = 0;

void setup() {
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(button, INPUT);
}

void step(boolean dir,int steps){
  digitalWrite(dirpin,dir);
  delay(50);
  for(int i=0;i<steps;i++){
    digitalWrite(steppin, HIGH);
    delayMicroseconds(500);
    digitalWrite(steppin, LOW);
    delayMicroseconds(500);
  }
}

void loop()
{

  val = digitalRead(button);

  if (val == HIGH)
  {
    int i;

    digitalWrite(dirpin, HIGH);   
    for (i = 0; i<1; i++)
    {
      digitalWrite(steppin, HIGH);
      step(true,1600);
      digitalWrite(steppin, LOW);
      delay(10000);
    } 
  }
}

But is this the proper way to do it?

No, it isn't. Hold that switch down. Does the stepper stop after one step? No, it does not. It steps as long as the switch is held down.

How many times does that for loop iterate?

Why are you using a for loop to iterate once?

for (i = 0; i<1; i++)

If you’re only ever going to do it once, there’s no need for a loop.

PaulS:

But is this the proper way to do it?

No, it isn't. Hold that switch down. Does the stepper stop after one step? No, it does not. It steps as long as the switch is held down.

How many times does that for loop iterate?

Why are you using a for loop to iterate once?

Thats the idea, that the stepper runs for one revolution just pressning the button down/release.

Thanks. I removed the loop.