The program counts them in the background and displays their sum after 10 seconds, which is 16 pulses. After another 10 seconds it displays the sum of the pulses from the previous 10 seconds and the first ones (it adds the pulses from the time 0:00 - 0:09 and 0:10 - 0:19) and displays, and so on.
I'm confused by that explanation. If it always receives pulses in that pattern (1, 1, 4, 1 etc) for a total of 16 every 10 seconds, what's the point of either counting them or displaying the sum. You know it's always 16.
Use the technique shown in the state change detect example in the ide to detect a pulse and increment a counter. Use the technique shown in the blink without delay ide example to let 10 seconds elapse delay()-lessly; then display the counter and reset it to zero.
There is an automatic function for doing this in the Arduino-IDE
just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
paste clipboard into write-window of a posting
Maybe I'm underestimating your capabilities by far.
in the other two threads after posting three or four lines you were able to solve the problems yourself. So I guess in this case it will be the same.
After subtle changes (indicating what is to be displayed and changing variables to make the code more understandable overall), I got a result that is satisfactory. I use the millis() function in the program, so I understand the operation of the above code.
you forgot to generate the code tags (and to indent the code, and will fail if you have more than 32767 line on an 8 bit arduino and it should start with a small 'p' if you want to respect camelCase).
don't look at the blink without delay-example!
This example does not explain how it really works.
I post the explanation as a code-section to keep the visible height of the posting smaller
experienced users skip the posting
it is just StefanL38's non-blocking-timing explanation:
as an everyday example with easy to follow numbers
delay() is blocking. As long as the delay is "delaying" nothing else of the code can be executed.
Now there is a technique of non-blocking timing.
The basic principle of non-blocking timing is **fundamental different** from using delay()
**You have to understand the difference first** and then look into the code.
otherwise you might try to "see" a "delay-analog-thing" in the millis()-code **which it really isn't**
Trying to see a "delay-analog-thing" in millis() makes it hard to understand millis()
Having understood the basic principle of non-blocking timing based on millis() makes it easy to understand.
imagine baking a frosted pizza
the cover says for preparation heat up oven to 200°C
then put pizza in.
Baking time 10 minutes
You are estimating heating up needs 3 minutes
You take a look onto your watch it is 13:02 (snapshot of time)
You start reading the newspaper and from time to time looking onto your watch
watch shows 13:02. 13:02 - 13:02 = 0 minutes passed by not yet time
watch shows 13:03. 13:03 - 13:02 = 1 minute passed by not yet time
watch shows 13:04. 13:04 - 13:02 = 2 minutes passed by not yet time
watch shows 13:05 when did I start 13:02? OK 13:05 - 13:02 = 3 minutes time to put pizza into the oven
New basetime 13:05 (the snapshot of time)
watch 13:06 not yet time
watch 13:07 not yet time
watch 13:08 not yet time (13:08 - 13:05 = 3 minutes is less than 10 minutes
watch 13:09 not yet time
watch 13:10 not yet time
watch 13:11 not yet time
watch 13:12 not yet time
watch 13:13 not yet time
watch 13:14 not yet time (13:14 - 13:05 = 9 minutes is less than 10 minutes
watch 13:15 when did I start 13:05 OK 13:15 - 13:05 = 10 minutes time to eat pizza (yum yum)
You did a repeated comparing how much time has passed by
This is what non-blocking timing does
In the code looking at "How much time has passed by" is done
currentTime - startTime >= bakingTime
bakingTime is 10 minutes
13:06 - 13:05 = 1 minute >= bakingTime is false
13:07 - 13:05 = 2 minutes >= bakingTime is false
...
13:14 - 13:05 = 9 minutes >= bakingTime is false
13:15 - 13:05 = 10 minutes >= bakingTime is TRUE time for timed action!!
So your loop() is doing
void loop()
// doing all kinds of stuff like reading the newspaper
if (currentTime - previousTime >= period) {
previousTime = currentTime; // first thing to do is updating the snapshot of time
// time for timed action
}
it has to be coded exactly this way because in this way it manages the rollover from Max back to zero
of the function millis() automatically
baldengineer.com has a very good tutorial about timing with function millis() too .
There is one paragraph that nails down the difference between function delay() and millis() down to the point:
The millis() function is one of the most powerful functions of the Arduino library. This function returns the number of milliseconds the current sketch has been running since the last reset. At first, you might be thinking, well that’s not every useful! But consider how you tell time during the day. Effectively, you look at how many minutes have elapsed since midnight. That’s the idea behind millis()!
Instead of “waiting a certain amount of time” like you do with delay(), you can use millis() to ask “how much time has passed”?
https://www.baldengineer.com/millis-tutorial.html
A compound statement or block groups a sequence of statements into a single statement by surrounding the sequence of statements with {}
so when you want to assign a value to a variable, you have a simple expression statement and thus you do int x = 3 and you end with the semi colon to denote the end of that statement. so int x = 3;
if x was an array and you want to provide an Aggregate initialization ( a form of list-initialization or direct initialization) then the value is within brackets and the terminating semi colon is there to mark the end of the expression statement.
now you have statements all over the place, for example an if() is a Selection statements and it's syntax is if ( condition ) statement âžś so what you execute when the if is true is a statement. if it's a simple expression statement like above, you don't even need brackets if (condition) x = 3; âžś the statement is x=3;
now you can decide to put a compound statement there and so use the brackets if (condition) {...} âžś no need for the semicolon since the compound statement definition says you don't need it.