Hey i am new to arduino world. I have been using them for years in the 3d printer area but never have i fully written a program for one. A lot more dificult than i thought.
So i am using an arduino based board to read a switch and move a stepper. I can't seem to get the code i write to work.
I am trying to read the switch
if the switch reads low move the stepper # of steps
now the thing is in the application for this ( the use not program) the device will start with a high reading i am looking for a change in that reading once the change happens move the stepper and vise versa. Looking for a state change after the initial setup. and then continuing to monitor that state for a change and move the stepper accordingly.
Right now my code either move stepper with no response to switch or or does nothing. Kind of lost.
I am basing everything off the digital pin push button example and the stepper one revolution example it is sort of combination of the two any help would be appreciated
I do not believe that altering the steps per revolution has anything to do with taking those steps. [ probably an incorrect statement, stepsperrevluiton is how you send steps ]
there should be a place to tell it to make steps.
you cannot say
if the switch is pressed, then the value of the variable is that the button is pressed.
nor
if the button is pressed, then the value is that the button is pressed
if the button is not pressed, then the button is not pressed.
you did not post your code, nor did you follow the instruction #7 in how to use this forum, that is located as one of the first posts on every forum.
if the button is not pressed, you probably do not want to do anything.
if only one button is pressed, then you could make the stepper take steps
(you are currently not ever telling the stepper to take steps. )
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
myStepper.step(stepsPerRevolution);
}
this would use one switch and if stepsPerRevolution is how the library is instructed to take steps, then it will take steps as long as the button is pressed
if you have a SECOND button...
buttonState1 = digitalRead(buttonPin); // only need to read this one time in the loop
buttonState2 = digitalRead(buttonPin); // this is the second button, to step in reverse
if (buttonState1 == HIGH) {
myStepper.step(stepsPerRevolution);
}
if (buttonState2 == HIGH) {
myStepper.step(-stepsPerRevolution);
}
if you do not want to confuse the issue AND you want one to take precedence,
buttonState1 = digitalRead(buttonPin); // only need to read this one time in the loop
if (buttonState1 == HIGH) {
myStepper.step(stepsPerRevolution);
}
if(buttonState1=LOW){ // this will only allow the program to see the second button if the first one if low
if (buttonState2 == HIGH) {
myStepper.step(-stepsPerRevolution);
}
}
prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && prevButtonState == HIGH)
{
myStepper.step(nnn); // nnn is the number of steps to move
}
Don't overcomplicate things.
Only read a button once in every iteration of loop() - otherwise you just get confused.
#include <Stepper.h>
const int buttonPin = 2; // the number of the pushbutton pin
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
int ButtonState = 0;
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int prevButtonState =0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
}
void loop() {
// put your main code here, to run repeatedly:
prevButtonState = 1;
ButtonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin); // only need to read this one time in the loop
buttonState2 = digitalRead(buttonPin); // this is the second button, to step in reverse
if (buttonState1 == HIGH) {
myStepper.step(stepsPerRevolution);
}
if (buttonState2 == LOW) {
myStepper.step(-stepsPerRevolution);
}
}
Based on this. I have been working on this for days its the multiple different iterations only reason I am here asking for help because I don't know.
prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && prevButtonState == HIGH)
{
myStepper.step(nnn); // nnn is the number of steps to move
}
if (buttonState == HIGH && prevButtonState == LOW)
{
myStepper.step(-nnn); // nnn is the number of steps to move
}
To my mind you have been overcomplicating it again.
The line in your program
prevButtonState = 1;
will do nothing useful - it is not actually capturing the previous state.
Why are you reading the same button twice a few microseconds apart and saving the result in two different variables?