Hello,
I am trying to run a subprogram whenever the button is depressed. The way it will work is wait for button press->oh boy, time to start->run the entire program->return to waiting to start again.
The entire code is a lot more lengthy than this, but i have included the parts that i feel are relevant to the problem at hand. I have excluded the pin declarations and other iterations of the timing scheme i am looking to follow.
pinMode(button_pin,INPUT);
}
void loop() {
buttonState=digitalRead(button_pin);
winding();
}
void winding(){
/*this subprogram should run through until the end, regardless of current button state
* It should be initiated by a single button press and continue to the end
*/
if (buttonState==HIGH){ //if button has been pressed, proceed
Serial.println("Pressed");
currentMillis=millis();
digitalWrite(ext_ena,LOW);
digitalWrite(wind_ena,LOW);
digitalWrite(mate_ena,LOW);
digitalWrite(main_ena,LOW);
digitalWrite(heat_ena,LOW);
previousMillis=currentMillis; //get this part to work
Serial.println("MAde");
if (currentMillis-previousMillis<=10000){//we'll run them all
Serial.println("IT");
currentMicros=micros();
//see if it's time to make another step
if ((currentMicros - previousMicros_ext)>=microsBetweenSteps_ext){
previousMicros_ext += microsBetweenSteps_ext; //add time
digitalWrite(ext_pul,HIGH);//pulse the stepper
digitalWrite(ext_pul,LOW);
currentMillis=millis();//get new time, necessary?
}
if (buttonState == pushed) {
do_something();
buttonState = not_pushed;
}
Thank you for the reply. I am wondering where this logic should go? When it is placed in the loop, it performs the same as before(motors only on when button is depressed). When I call the function from the setup(), it only runs for one iteration then stops.
When I call the function from the setup(), it only runs for one iteration then stops.
That is how setup() is intended to work.
Post the revised code, with the control function in loop(), using code tags.
void loop() {
buttonState=digitalRead(button_pin);
if(buttonState==HIGH){
winding();
buttonState=0;
}
void winding(){
Serial.println("IN");
/*this subprogram should run through until the end, regardless of current button state
* It should be initiated by a single button press and continue to the end
*/
// buttonState=digitalRead(button_pin);
// if (buttonState==HIGH){ //if button has been pressed, proceed
currentMillis=millis();
previousMillis=currentMillis; //get this part to work
if (currentMillis-previousMillis<=10000){//we'll run them all
digitalWrite(ext_ena,LOW);
digitalWrite(wind_ena,LOW);
digitalWrite(mate_ena,LOW);
digitalWrite(main_ena,LOW);
digitalWrite(heat_ena,LOW);
currentMicros=micros();
//see if it's time to make another step
if ((currentMicros - previousMicros_ext)>=microsBetweenSteps_ext){
previousMicros_ext += microsBetweenSteps_ext; //add time
digitalWrite(ext_pul,HIGH);//pulse the stepper
digitalWrite(ext_pul,LOW);
currentMillis=millis();//get new time, necessary?
}
I am trying to run a subprogram whenever the button is depressed.
The program is working correctly, according to that specification.
However, if you want the function to be executed only once per button press, you have to wait until the button is pressed, then released before calling that function. You may need to debounce the button.
I just found my issue. I was using an if() statement instead of a while() loop. I am frustrated that it took so long for such a trivial issue, but at least the solution has been found. I also added a debounce per your recommendation for good measure. Thank you for your guidance!