im building a cabinet with a fingerprint sensor for security. i have a loop for adding prints and a loop for normal operation. i need to run a line of code at the beginning of each loop once. i have a switch to select either enrollment mode or normal opperation. im not sure how to do it. i have a basic code to show what i need.
I hope this makes sense!! let me know if you need clarification!!
const int Key = 4;
int Key_State;
int Thing_1;
int Thing_2;
void setup() {
pinMode(Key, INPUT_PULLUP);
}
void loop() {
Key_State = digitalRead(Key);
if (Key_State == HIGH); {
(Do "Thing_1" once);
Normal(); // loops until Key_State = LOW;
}
if (Key_State == LOW); {
(Do "Thing_2" once);
Enroll(); // loops until Key_State = HIGH;
}
}
void Enroll() {}
void Normal() {}
You should also take a look at the State Change Detection example (File->examples->02.Digital->StateChangeDetection) so see a better way to detect when a key is presses/released. At the very least, you will want to debounce that key press.
You also do not need to if() statements. If the first if() is true (Key_State == HIGH), the second if() statement can never be true. It should just be the else() clause
const int Key = 4;
int Thing_1;
int Thing_2;
void setup() {
pinMode(Key, INPUT_PULLUP);
}
void loop() {
int Key_State = digitalRead(Key);
if (Key_State == HIGH) {
//(Do "Thing_1" once);
Normal(); // loops until Key_State = LOW;
}
else {
//(Do "Thing_2" once);
Enroll(); // loops until Key_State = HIGH;
}
}
void Enroll() {}
void Normal() {}
awesome!! thank you so much!! i see where youre going here....i know i need to define "Thing_1" and "Thing_2" do i do that in loop?? and how to i tie that to the Bools?
What? They are defined here near the top of the code:
int Thing_1;
int Thing_2;
but I see that they're never used for anything... actually, what are they supposed to be used for? They're weirdly named 'int' variables, that's all I can tell...
is where you can put any code that needs doing once. Same with the other, It’s already “tied in”.
Just put a print statement there and you’ll see it working
Serial.println(“ Doing thing 1”);
and similarly for thing 2.
Make sure you leave the lines that set the bools inside that section where they are, that’s what keeps it from running the next time. And resetting for next time in the other mode…
You should rename your button functions, the current names obfuscate the purpose and behaviour. Something like 'waitForEnrollKey()'.
Later on, you may want to add some device or input that changes while functions like Enroll() are running. It will fail because Enroll() stops all processing while it polls the button. Then you will have to completely restructure your program.
I thought enroll() and normal() are being called once per loop(), so there is plenty of attention available up there.
And that when switching between the two modes, something has to be done once per entry of the other mode.
Otherwise the OP’s problem wasn’t a problem of any kind. The “start up” once only code could just have been at the top of the normal() and enroll() functions.