I am trying to stop further execution of code once a button is pressed. I have written a function which states that once the button is pressed, enter a never ending loop which tells the Arduino to wait. I am having difficulty implementing this function into the code. I want it so that if the button is pressed while the code is running, the stop function should run and the code should stop running and just wait. How should I do this? [Button 4]
//Checking which button is pressed
void ButtonPress()
{
keyPressedPrevious= keyPressed;
keyPressed= btn.pressed();
if(keyPressedPrevious != keyPressed)
{
if(keyPressed == KEY_1)
{
z += 1;
SerialPrint();
}
if(keyPressed== KEY_2)
{
GoIndicator= true;
}
if(keyPressed == KEY_3)
{
z += -1;
SerialPrint();
}
if(keyPressed == KEY_4)
{
RtrnOrigin();
DoneIndicator=true; //Set the indicator to true, to signal that program is done
do
{
delay(5000); //Continuously wait unitl the power is cut off
}
while(DoneIndicator==true);
}
}
}
Placing it at the top of the loop would work, but ideally I would like it to stop close to when the button is pressed, because the loop will often take upwards of 10 minutes to complete.
GregBlosh:
Placing it at the top of the loop would work, but ideally I would like it to stop close to when the button is pressed, because the loop will often take upwards of 10 minutes to complete.
The loop function should take no more than a few milliseconds to execute. This is a big design no-no.
ten minutes ! wow - what do you do in there... delays everywhere or large computation?
Often a slow low is an indication of a "wrong" code design (wrong in the sense that it is not easily adaptable to specific needs like the one you want now)
long loops I've seen are when coders did not want to code a good state machine for example
It is a combination of both, large computations which require delays, the delays added together can be substantial. It is automating a process that can take almost 8 hours, so I think the code is working properly.
GregBlosh:
It is a combination of both, large computations which require delays, the delays added together can be substantial. It is automating a process that can take almost 8 hours, so I think the code is working properly.
OK - if you can't / don't want to redesign your code as a state machine (there should not be a need for delays in the code, the processor ends up doing nothing) then what you could do is write your own myHackedDelay() function which just waits for the right amount of time using millis() instead of calling delay() and checking your button at the same time in this function... add also if necessary a few myHackedDelay(0); here and there if you have long computations.
not perfect of course but you will check this way more often than just at the top of the loop and won't be stuck in delay() calls
I added an attachInterupt statement to my setup function. When the serial port is disconnected it works exactly how I want it to, but when the serial port is connected the interrupt does not work. I need to attach the serial for data collection, how do I fix this?
Just Added the relevant code, SerialPrint() is a function to print to the LCD Screen.
void setup()
{
pinMode(dirPin, OUTPUT);
pinMode(stpPin, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(stpPin2, OUTPUT);
uint32_t currentFrequency;
attachInterrupt(0, ButtonPress, FALLING); //Checks for button presses all the time
mySerial.begin(9600); //Starts talking with the LCD
ina219_A.begin(); //Starts reading from Chip A
ina219_B.begin(); //Starts reading from Chip B
SerialPrint();
do
{
ButtonPress();
delay(1);
}
while (GoIndicator != true);
ReadChip();
Cutoff = Pout / 2; //Sets the Cutoff from the readings at the starting origin
FindOrigin();
}
void ButtonPress()
{
keyPressedPrevious = keyPressed;
keyPressed = btn.pressed();
if (keyPressedPrevious != keyPressed)
{
if (keyPressed == KEY_1)
{
z += 1;
SerialPrint();
}
if (keyPressed == KEY_2)
{
GoIndicator = true;
}
if (keyPressed == KEY_3)
{
z += -1;
SerialPrint();
}
if (keyPressed == KEY_4)
{
RtrnOrigin();
DoneIndicator = true; //Set the indicator to true, to signal that program is done
do
{
delay(5000); //Continuously wait unitl the power is cut off
}
while (DoneIndicator == true);
}
}
}
Nope, not just what you think is relevant. Post the whole thing. In order for you to be completely sure what's relevant you'd have to know what was wrong. If you knew that you wouldn't be asking.
I'm particularly interested in variable definitions.
You've got some non-standard libraries in there. I see a MeSerial and Me4Button. Looks like a MeOrion library whatever that is. ANd the Adafruit library you're using. Post links to those.
(Would save a lot of time if you'd read the posting guidelines in How to Use This Forum)