Go Down

Topic: How to add one pushbutton to control stepper motor? (Read 214 times) previous topic - next topic

Richard905

When I test following code, I hope it start to run after push button is pressed, but it always ran, means   
push button didn't work at all? Can somebody check the code for me?

Thanks!

Code: [Select]


#define X_DIR     3       
#define X_STP     4       
int pushbuttonpin = 7;    //Pin for momentary push button switch or sensor
int val=0;

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

  Serial.begin(9600);
  pinMode(pushbuttonpin, INPUT);   //Declare push button switch pin as input
 
  pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
   
}
void loop(){
  val=digitalRead(pushbuttonpin);
  if(val==HIGH)
  {
  step(true, X_DIR, X_STP, 800);
    delay(1000);
  step(false, X_DIR, X_STP, 400);
 
}
}

Delta_G

Code: [Select]
pinMode(pushbuttonpin, INPUT);

You're not using the internal pull-up or pull-down resistors.  Do you have external ones?  Or do you have floating inputs?

Code: [Select]
if(val==HIGH)

You seem to have the switch wired backwards so that it reads HIGH when pressed.  Normally one would wire the switch to read LOW when pressed so one could take advantage of the internal pull-up resistors built into the chip and avoid the need for external resistors. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Richard905

yes, I have external resistor, How to change code? Thanks!

Delta_G

yes, I have external resistor
Show how you have them wired. 

How to change code?
Who knows, we still can't see the circuit that goes with it.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

arduino_new

#4
Jun 25, 2018, 02:39 am Last Edit: Jun 25, 2018, 02:40 am by arduino_new
Have you made sure that the button is not in floating state?

You have many delay() in your code. To allow the program to respond to the button pushed any time you must get rid of delay() and use millis() for timing

Richard905

same as following example,
https://www.arduino.cc/en/Tutorial/Button

Delta_G

same as following example,
https://www.arduino.cc/en/Tutorial/Button

Then it would work.  Show what you actually did, not the example you followed.  Unless you just want me to try to debug the example.  (nothing is wrong with it so I'm done in that case.)
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Richard905

Problem is solved! Connect GND of Arduino board to push button GND.

Thanks !

Go Up