Hey, I'm having trouble reading from a push button while also running a stepper motor. I'm using the Stepper.h library. I am trying to have the stepper motor run the opposite way when I push the button. I appreciate any help you can give to me.
Welcome to the arduino forum,
You may want to check this.
Are you sure that your connections are correct?
Can you post your code?
The stepper.h is a blocking library and not suited for your needs.
Try using a non blocking lib like AccelStepper ir my MobaTools lib. Than you can check your button while the stepper is running.
And please post your sketch as it is so far and tell a little bit more about your project, so we can assist more.
Technically, you could use that library as long as you only ask it to do one step at a time. If you're going to do that though, there's really no need for a library at all.
#include<Stepper.h>
int in1=8;
int in2=9;
int in3=10;
int in4=11;
int SPR=2048;
int RPM=10;
int bPin=3;
int bVal;
int oldState;
int newState;
int unimportant=0;
Stepper myMotor(SPR,8,10,9,11);
void setup() {
// put your setup code here, to run once:
pinMode(bPin,INPUT);
digitalWrite(bPin,HIGH);
Serial.begin(2000000);
myMotor.setSpeed(RPM);
}
void loop() {
// put your main code here, to run repeatedly:
while(unimportant==0){
newState=digitalRead(bPin);
Serial.println(newState);
oldState=1;
if (oldState==1&&newState==0){
if(newState==0){
myMotor.step(SPR);
}
if (bVal==1){
SPR=SPR*-1;
}
else {
SPR=SPR;
}
}
oldState=0;
if(oldState==0&&newState==1){
myMotor.step(-SPR);
}
newState=oldState;
}
}
This is my code
Code should be posted as code using </> button so the formatting isn't trashed please. If you haven't read the how to post thread(s) I'd advise it.
#include<Stepper.h>
int in1=8;
int in2=9;
int in3=10;
int in4=11;
int SPR=2048;
int RPM=10;
int bPin=3;
int bVal;
int oldState;
int newState;
int unimportant=0;
Stepper myMotor(SPR,8,10,9,11);
void setup() {
// put your setup code here, to run once:
pinMode(bPin,INPUT);
digitalWrite(bPin,HIGH);
Serial.begin(2000000);
myMotor.setSpeed(RPM);
}
void loop() {
// put your main code here, to run repeatedly:
while(unimportant==0){
newState=digitalRead(bPin);
Serial.println(newState);
oldState=1;
if (oldState==1&&newState==0){
if(newState==0){
myMotor.step(SPR);
}
if (bVal==1){
SPR=SPR*-1;
}
else {
SPR=SPR;
}
}
oldState=0;
if(oldState==0&&newState==1){
myMotor.step(-SPR);
}
newState=oldState;
}
}>
is that better?
SPR=SPR;
I know that this is unnecessary but it is just in there to make sure.
My current problem is that I cannot read from the pushbutton while the stepper motor is running
When will unimportant != 0 ?
never. I was just trying something that I thought would work. That is also un needed
Again, I am trying to read from the button while the motor is running
If unimportant==0 is always true, when does the code quoted above run ?
The entirety of the void loop is in the while loop. So that code would run when oldState is 0 and newState is 1
new code without unneeded code
This is unnecessary (to the next post) Can you help me with my problem?
#include<Stepper.h>
int in1=8;
int in2=9;
int in3=10;
int in4=11;
int SPR=2048;
int RPM=10;
int bPin=3;
int bVal;
int oldState;
int newState;
int unimportant=0;
Stepper myMotor(SPR,8,10,9,11);
void setup() {
// put your setup code here, to run once:
pinMode(bPin,INPUT);
digitalWrite(bPin,HIGH);
Serial.begin(2000000);
myMotor.setSpeed(RPM);
}
void loop() {
// put your main code here, to run repeatedly:
newState=digitalRead(bPin);
Serial.println(newState);
oldState=1;
if (oldState==1&&newState==0){
if(newState==0){
myMotor.step(SPR);
}
if (bVal==1){
SPR=SPR*-1;
}
else {
SPR=SPR;
}
}
oldState=0;
if(oldState==0&&newState==1){
myMotor.step(-SPR);
}
newState=oldState;
}
In the Arduino IDE, use Ctrl T or CMD T to format your code then show us the complete sketch again.
Hey, so can you help me? I have a new problem. I'm trying to have a pushbutton change the direction of the stepper motor instantaneously with stepper.h