int switchTare = D1; // select the input pin for the switch
int switchTareValue = 0;
Void loop()
if (digitalRead(switchTare) == HIGH) {
int startTime = millis();
while (digitalRead(switchTare) == HIGH) {
if (millis() - startTime > 7000) {
//tare(); // call function tare
Serial.println("Calibrate millis");
break;
}
}
}
I made this like a function because I have 4 switches (read relays).
tare
calibrate
alarm
reset
All 4 is functions, all switches except alarm is NO
Example
int switchTare = D1; // select the input pin for the switch
int switchTareValue = 0; // variable to store the value coming from the sensor
void setup(){
pinMode(switchTare, INPUT);
}
void loop() {
// check if switchTare is pressed
if (digitalRead(switchTare) == HIGH) {
buttonWait(switchTare, buttonPressTime, functionTare, HIGH); // wait for HIGH state
}
//Function
void buttonWait(int switchPin, int waitTime, void (*function)(), bool state) { // true for HIGH, false for LOW
pinMode(switchPin, INPUT);
if (digitalRead(switchPin) == state) {
int startTime = millis();
while (digitalRead(switchPin) == state) {
if (millis() - startTime > waitTime) {
(*function)(); // call the function pointed to by the function pointer
Serial.println("tare mills");
break;
}
yield(); // allow other tasks to be executed
}
}
}
it's up to you but
a) I suspect your sketch will not compile - {} are not correct, missing variables, ...
b) your code is still blocking. Do a refactoring of your code and get rid off your while
may be you should google for "Finite State Machine" and apply that to your program.
The code is part of a bigger project so there can be missing parts in my function sample.
I have built a bee monitor, 150 kg scale (with auto calibration), temp, humidity sent over LoRa to a receiver (ttgo lora oled) that again sends data to cayenne and to a web server.
My sketch it compiles and when I press button for 5 sec the function is running and then goes back to the void loop.
I'm not an expert, a bit google, a bit old basic and fortran 30-40 years ago and a skilled son. And I have time, started on this code 1 year ago
I'm sure the code can be optimized, but that is a level I'm not capable off.
I'm happy as long as it runs, what it does per today!
If you have specifics in code I appreciate your effort to help me further.