^yo what’s up guys i want to press a button to start the void loop in my program and begin the cycle and if the button is not pressed i want nothing to happen, i tried to put an if inside the void loop but it stops after, this is my code
#include <elapsedMillis.h>
#include <StepperDriver.h>
int motor_steps = 200; //
int step_divisition = 1;
int en_pin = 6;
int cw_pin = 4;
int clk_pin = 5;
unsigned int period =4000;
unsigned int period2 = 7000;
const int buttonPin = 8;
int buttonState = 0;
//long tempsmouv =
//long tempsarret =
StepperDriver ss(motor_steps, step_divisition, en_pin, cw_pin, clk_pin);
void mouvement(){
ss.setSpeed(150);
ss.step(300);
delay(400);
ss.step(-300);
delay(400);
}
void arret(){
ss.setSpeed(0);
}
void setup() {
Serial.begin(9600); // put your setup code here, to run once:
pinMode(buttonPin, INPUT);
}
void loop() {
elapsedMillis timeElapsed;
while(timeElapsed < period ) {
Serial.println("test1");
mouvement();
}
elapsedMillis timeElapsed2;
while(timeElapsed2 < period2 ) {
Serial.println("test2");
arret();
buttonState = digitalRead(buttonPin);
}
}
Assuming you've setup this button to pull the pin low when pressed, something like this:
void setup()
{
Serial.begin(9600); // put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
while( digitalRead( buttonPin ) == HIGH );
}
What you posted isn’t the same as what I did. Can you see the difference?
Hint: In your while loop do you ever update the value of buttonState?
Also, how is your button wired? When you press it does it ground the pin? If so, do you have a pull-up resistor on the pin? If not, you need to specify “INPUT_PULLUP” in the pinMode call or the pin will always read low.
the button works fine when i use it for other sketch it's wired correctly
i think there is something wrong in my code, what i want to do is to press the button to start the loop, i want one press and then the loop run to infinity, i don't want to plug usb for run or the alim for motor i want press button to start void loop whithout pressing button again, i hope im clear my english is not that good
and yes my button is connected to ground and have a resistor
sir i swear as soon as i plug the usb the motor starts runing the loop starts and i didn’t even press the button i did exactly what you told me take a lookl;
void setup() {
Serial.begin(9600); // put your setup code here, to run once:
pinMode(buttonPin, INPUT);
buttonState = digitalRead(buttonPin);
while( buttonState == HIGH )
buttonState = digitalRead(buttonPin);
}
void loop() {
elapsedMillis timeElapsed;
while(timeElapsed < period) {
Serial.println("test1");
mouvement();
}
elapsedMillis timeElapsed2;
while(timeElapsed2 < period2 ) {
Serial.println("test2");
arret();
}
}]
CactusJack:
sir i swear as soon as i plug the usb the motor starts runing the loop starts and i didn't even press the button i did exactly what you told me take a lookl;
I believe it. It's happening because you're not doing as I suggested. Look carefully at what I posted and what you have.
They're different in a very important and obvious way.
sir i think i will go crazy i did exactly what you said i even copy paste please check my code
void setup() {
Serial.begin(9600); // put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
while( digitalRead(buttonPin) == HIGH );
}
void loop() {
OP I took your code from reply #13 and added some prints, and an led to mimic your motor. The led is made off in setup() and only supposed to come on in loop().
Once the Uno reset, I counted up to ten and hit the button. The led came on, and the monitor showed that some 12 seconds had elapsed, while it sat in setup() waiting for the button.
Here's the output:
Waiting for button at millis= 0
Button was pressed at millis= 11676
Here's the code, which remember is basically yours from #13:
byte buttonPin = 5;
byte ledPin = 4;
void setup()
{
Serial.begin(9600); // put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.print("Waiting for button at millis= ");
Serial.println(millis());
while ( digitalRead(buttonPin) == HIGH );
Serial.print("Button was pressed at millis= ");
Serial.println(millis());
} //setup
void loop()
{
digitalWrite(ledPin, HIGH);
}//loop
Blackfin:
Assuming you've setup this button to pull the pin low when pressed, something like this:
void setup()
{
Serial.begin(9600); // put your setup code here, to run once:
pinMode(buttonPin, INPUT_PULLUP);
while( digitalRead( buttonPin ) == HIGH );
}
You want the button pressed and released unless it should remain pressed when void loop() starts running.
while( digitalRead( buttonPin ) == HIGH ); // press to end this loop
delay( 20 ); // because I am NOT going to explain debounce here
while( digitalRead( buttonPin ) == LOW ); // release to end this loop
GoForSmoke:
You want the button pressed and released unless it should remain pressed when void loop() starts running.
Not necessarily: only a problem if you're actually going to be using the same button out in loop(), where you will be doing another read of it, and yes the continued holding down would be seen as a press in loop() and casue some activity there.
But for a simple start button with no other purpsoe, no need to look for a press and release, or even to debounce. The first closure, even if followed by a zillion bounces, takes you out to loop() and the button isn't read any more.
here is the pic attached of my wiring, im still having problems when i use prints and press the button, the prints show slowly because of the delay in my “mouvement” fonction but the prints work fine with my “arret” fonction, i think the problem is from my “mouvement fct” i will put my code again if you guys can run some tests, i didn’t find any solutions pls help:
#include <elapsedMillis.h>
#include <StepperDriver.h>
int motor_steps = 200; //
int step_divisition = 1;
int en_pin = 6;
int cw_pin = 4;
int clk_pin = 5;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned int period =5000;
unsigned int period2 = 4000;
const int buttonPin = 8;
//long tempsmouv =
//long tempsarret =
StepperDriver ss(motor_steps, step_divisition, en_pin, cw_pin, clk_pin);
void mouvement(){
ss.setSpeed(150);
ss.step(300);
delay(400);
ss.step(-300);
delay(400);
}
void arret(){
ss.setSpeed(0);
}
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
while( digitalRead(buttonPin) == HIGH );
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
buttonPushCounter++;
}
// Delay a little bit to avoid bouncing
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter %2 != 0) {
elapsedMillis timeElapsed;
while(timeElapsed < period) {
Serial.println("test1");
mouvement();
}
elapsedMillis timeElapsed2;
while(timeElapsed2 < period2 ) {
Serial.println("test2");
arret();
}
}
}