I wanted to sanity check myself on some code I am writing. C++ is far from my strong suit so I wanted to confirm if my understanding is correct. I am writing a function to time how long a button is pressed as part of an arming sequence for the control box of a model rocket project. See code below for the arming sequence. For now it's not linked to any functions but just some print outs to help clarify if it's functioning correctly.
I was curious as to how the millis() function behaves in a while loop. Specifically in the timePress() function I've created. When the variable is set to millis() would that record the time when the condition became true or would it continue updating the variable until the condition is no longer true... essentially will this function work to to time the button presses in and in the context I'm attempting to apply it in?
I'm currently awaiting a PCB and lack sufficient jumper cables throw together a breadboard and test so any advice ahead of time would be amazing.
Here is the full code. I didn't include it because it contains a lot of unfinished bits. I tend to jump between functions and leave things half finished so I figured refining it to that section might've been an easier read.
This unit will be used as a Universal controller amongst future projects too so I will be incorporating ESP-NOW Comms, NRF24L01 and BLE for wireless comms hence a few references sprinkled around.
EDIT Any tips for formatting Serial.print()s would be greatly appreciated. The printAll() function is very messy and irritating me!
From what I can see, you are making rather gratuitous use of boolean variables. An 'if' statement by itself, is adequate to test a condition and execute some code. You don't have to create a variable for the sole purpose of testing. I highlighted the last example in loop(), I think it's not the only case, there are more... but for example the above code doesn't use 'takeoff' for any other purpose, and so should be expunged because it's non-standard coding.
Thanks for the advice. You're right, the takeOff bool was useless. I have removed it to streamline the code. I'm almost certain the code is riddled with inefficiencies like this. I intend to get a functional set of functions working then hopefully take the time to dive deeper into C++ and make the code 'Prettier', simpler and more efficient.
It's not actually inefficient. The compiler would optimize that to the same target code. It's just not the best approach from the point of view of source code quality.
Very basic question. If I've stored the states as int would this work? Does C++ recognise 0 as LOW? For example the variable for the state of the Joystick Button (JoyPress1) is an int so I assumed it would require me to use the binary terms.
Yes, you can store an acquired pin state in an 'int' variable. So
if (Switch1 == LOW) {
is perfectly okay.
I just noticed, the definition of Switch1 is missing from your sketch. Please update the sketch so it's complete. Actually, better to re-post in a new message.
I've burned a bit more grey matter on this, perhaps logiced my way to insanity with this one. Very long winded but I think this should fundamentally return the length of time the button was pressed for...
So the timer should reset so as not to end up with an ever growing end time . I was considering adding previous and current state tracking to trigger the timing but not sure if that's necessary... Does this make any sense or was the use of a while loop more applicable to this?
write code for one single thing. Test it throuroughly and if it works reliably add another small function.
By coding this way the number of lines where the bug could sit is almost everytime small.
If you write a lot of code before start testing it there are exponential more places where the bug could sit. And it will take much longer to find the bug.
If you want to use waiting time. Do experiments.
for example write a test code that does check and analyse this:
You might feel "moving forward" by writing all this code without testing it. But in fact you are creating more work to test.
Well if it is irritating you. If you don't write what you ideally expect it to look like How should somebody else make a suggestion?
As a main idea:
put the title to the left and the value to the right.
group lines that belong together
insert a empty line before and after each group
keep the function printall() but use more **sub-**functions where each sub-function prints some data title and value together that belong together.
As you are using standard things like pots and buttons you could test your code with the wokwi-simulator
Something I noticed - it may or may not matter to you at this point. Your button code blocks while the switch is held down. This means nothing else can happen. In your case, it might not be a problem but in future, you may want to keep other parts of the program running while you poll buttons, so you would need to re-think the way you do that.
I'm not completely sure about your case, but often this happens because people start coding before they have a solid plan for how the program should be structured. The code lulls you into the belief that you're planning as you go along. In fact, it's getting in the way.
millis() is the number of milliseconds your code has been running when you call it, the duration is the larger, later number minus the smaller, earlier number.
It was a late problem for me tinkering with your code: I read past it at least twenty times, so.
Don't feel bad, the first time I saw it I thought "What the heck (close) is a millis()?", but the context makes it obvious. Strangest abbreviation for getting time in milliseconds I have come across. But then many would not like my getTimeMs() either, or the homely sisters, getTimeUs(), and getTimeTicks().
My function and variable names are on the long side so I can figure them out later on, in concert with comments of course. I also try to make variables start with an object or noun and my functions start with a verb (or noun used as a verb). /End of unwanted, pedantic, and perceivably smarmy, post./