Hello everyone,
I have been working on a program that will activate a stepper motor when a push button is pressed. The program itself works without the if else statement for the push button. I will attach the code below. I would appreciate it if someone could explain what I am doing wrong when I add the push button into the program.
It is easier to view your code if you post it in Code tags, rather than making anyone who wishes to view it download it.
But thank you for posting your code.
/******************************************
PURPOSE: Running a stepper motor with the Arduino
Created by Rudy Schlaf after a sketch by Sam Leong
DATE: 1/2014
*******************************************/
#define pin1 8//these are the Arduino pins that we use to activate coils 1-4 of the stepper motor
#define pin2 9
#define pin3 10
#define pin4 11
#define PB1 4 //this id the aarduino pin we are using that connects the pushbutton to the circuit//
#define delaytime 8 //delay time in ms to control the stepper motor delaytime.
//Our tests showed that 8 is about the fastest that can yield reliable operation w/o missing steps
void setup() {
// initialize the 8 pin as an output:
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(PB1, INPUT);
}
void loop(){
if (PB1 == HIGH)
{
int numberOfSteps = 64;
step_OFF(); //turning all coils off
while(numberOfSteps>0){
forward(); //going forward
numberOfSteps -- ;//counting down the number of steps
}
delay(2000);
step_OFF(); //turning all coils off
numberOfSteps = 64;
while(numberOfSteps>0){
backward(); //going backward
numberOfSteps -- ;//counting down the number of steps
}
delay(2000);
}
else
{
}
}
//these functions set the pin settings for each of the four steps per rotation of the motor (keepp in mind that the motor in the kit is geared down,
//i.e. there are many steps necessary per rotation
void Step_A(){
digitalWrite(pin1, HIGH);//turn on coil 1
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
}
void Step_B(){
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);//turn on coil 2
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
}
void Step_C(){
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH); //turn on coil 3
digitalWrite(pin4, LOW);
}
void Step_D(){
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH); //turn on coil 4
}
void step_OFF(){
digitalWrite(pin1, LOW); //power all coils down
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
}
//these functions run the above configurations in forward and reverse order
//the direction of a stepper motor depends on the order in which the coils are turned on.
void forward(){//one tooth forward
Step_A();
delay(delaytime);
Step_B();
delay(delaytime);
Step_C();
delay(delaytime);
Step_D();
delay(delaytime);
}
void backward(){//one tooth backward
Step_D();
delay(delaytime);
Step_C();
delay(delaytime);
Step_B();
delay(delaytime);
Step_A();
delay(delaytime);
}
It is usually easiest to use pinMode(pin, INPUT_PULLUP); and connect the switch between the pin and GND. But then you get a LOW when the button is pressed.
I have a push button wired with a separate power supply. The output of the button is located in pin 4 of the arduino board. I ran the program and when I pressed the button (made it HIGH) nothing happened. If I were to remove the if statement for the push button the program turns the motor the way I want it to. I cannot find the way to add the push button into the code. In regard to the delays, they work fine and are not the problem. Thanks to everyone who is responding. I really appreciate it.