In a project I am working on, I write some data from some sensors to a SD card file when a button is pressed. This works sucessfully, however when I go above the 4 instances (i.e., I hit the button a 5th time) then the system just outputs a long list of numbers. I don't understand this because the value being read out is the progCounter and it is getting incremented only if the button is pressed for 3 cycles. At least this is my intentions. Clearly it isn't working. Below is my code and the Serial Monitor Output associated.
@alto777 that should just put it in an infinite loop "stuck" effectively.
However I guess my question isn't quite correct. How is the code incrementing and printing when the conditions are not met by the states? I have two if statements that should prevent both actions.
if(runLoop) {... if((buttonState==HIGH) && ...
These to lines should limit what the void loop() can even do. Like how is it getting inside the if(runLoop... and how is it getting is side the if((buttonstate==high... loop to increment the counter when the buttons hasn't been pressed?
is the only place that can be increasing progCounter. Therefore we conclude it is being executed.
So there is an error or bad assumption about the logical expression.
I would run your code and do it, but there is too much extra stuff to account for so you should print out the values of those variables that inform the logical choice to execute that code.
BTW Seeing both prevButtonState and prevPrevButtonState in the logic makes me wonder if you are creativley compensating for contacy bounce on the button... did you have some trouble with double or multiple actions with a "single" press?
I replaced your button handler with a standard pattern timed debounce algorithm.
Pressing the button stops a running "program". Pressing the button when none is running runs the next "program".
I just guessed that's what you were going for. I did try to understand and identify the problem with your button handling, I could make neither heads nor tails of it in the time alloted.
// https://wokwi.com/projects/360769786667890689
// https://forum.arduino.cc/t/entering-if-loop-despite-not-meeting-requirements/1109505
bool runLoop = false; // set state based loop to write to SD card
const int buttonPin = 7;
int buttonState = LOW;
int prevButtonState = LOW;
int prevPrevButtonState = LOW;
int progCounter = 0;
/*
const float maxVoltage = 3.3; //ESP32 max voltage on ADC pins
const int maxDigital = 4095; //max digital pin value
const float adcSlopeConv = maxVoltage / maxDigital; //.000805 V/DigitalValue
const int staticGain = 300; //static sensitvity is 300mV/g
const float zeroG = 1.65; //ADXL335 3-axis accel. on GY-61 dev board centers at 1.65V
const int zero = (maxDigital / 2);
*/
# define SD 777
// fake writeFile function
void writeFile(int aNumber, char *msg)
{
static unsigned int counter;
Serial.print(counter); counter++;
Serial.print(" ");
Serial.println(msg);
}
void setup(){
Serial.begin(115200);
Serial.println("hello there world!\n");
//...
pinMode(buttonPin, INPUT_PULLUP);
}
void loop(){
if(runLoop) {
if(progCounter==1){
writeFile(SD, "Button1.txt");
runLoop = false;
}
if(progCounter==2){
writeFile(SD, "Button2.txt"); delay(100); // so be slow on the pushbutton...
// runLoop = false;
}
if(progCounter==3){
writeFile(SD, "Button3.txt");
runLoop = false;
}
if(progCounter==4){
writeFile(SD, "Button4.txt");
runLoop = false;
}
if(progCounter>4){
Serial.println(progCounter);
//while (1);
}
}
buttonState = digitalRead(buttonPin);
static unsigned long lastButtonTime;
if (millis() - lastButtonTime > 20) {
lastButtonTime = millis();
if (buttonState != prevButtonState) {
if (!buttonState) {
if (runLoop) {
runLoop = false;
}
else {
progCounter++;
runLoop = true;
}
}
prevButtonState = buttonState;
}
}
}
Thank you too both @alto777 and @qubits-us for you help.
It seemed to be a combination of your suggestions.
I stripped the code back to a more basic setup, so I could analyze actually results and states. It appears the voltage on the pin connected to the button occasionally triggered a false press so by using the code below. I settled this problem. This made the code work well.