How to display data every 10 seconds?

Problem: Despite trying, I cannot write a program that receives data from an encoder every second but displays it every 10 seconds.

Example:

The sensor transmits the following pulses:

1s - 1 incoming pulse
2s - 1 incoming pulse
3s - 4 incoming pulses
4s - 1 incoming pulse
5s - 3 incoming pulse
6s - 1 incoming pulse
7s - 1 incoming pulse
8s - 1 incoming pulse
9s - 2 incoming pulse
10s - 1 incoming pulse

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.

Post your best effort

How are you doing the timing ?

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.

Maybe I missed something?

Actually, I misstated the subject. My mistake.

Every second the number of pulses will be different - depending on the speed of the encoder.

general purpose function to solve most coding-problems faster

float solvingTimeDays(int noOfSentencesDescribingTheDetails) {
  
  return 100 / noOfSentencesDescribingTheDetails;
}

best regards Stefan

1 Like

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.

universal function for receiving detailed support:

void PostingYourCode() {
  for (int LineOfCode = VeryFirstLine;  LineOfCode <= VeryLastLine; LineOfCode++) {
  Serial.println(myCode[LineOfCode] );
  }
}

maybe you prefer this method

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. 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.

best regards Stefan

consider

int count = 0;
unsigned long msecLst;

#define TWO_SEC     2000

void setup() {
    Serial.begin (9600);
}

void loop() {
    unsigned long msec = millis();

    if ( (msec - msecLst) > TWO_SEC)  {
        msecLst = msec;
        Serial.println (count);
        count = 0;
    }
};

Looking for a solution, I found an example of a program that displays text using a defined interval:

#define INTERVAL_MESSAGE1 10000

unsigned long time_1 = 0;

{
  if (millis() >= time_1 + INTERVAL_MESSAGE1) {
    time_1 += INTERVAL_MESSAGE1;
    print_time(time_1);
    Serial.print(flowMilliLitres);
    Serial.print(",");
    Serial.println(totalMilliLitres);
  }

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.

Is this a good solution?

Hello
Do you are looking for a flow meter sketch?

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).

:wink:

if this is a good solution could only be said of you would post your

own code

as a code-section

But it is the same as in the other two threads. You seem to need to have posted a question as a pre-requisite for finding the solution on your own.

No, there will be issues in 50 days or so if you do this
if (millis() >= time_1 + INTERVAL_MESSAGE1) {

There are plenty of good tutorials on using millis(), look at blink without delay to get started.

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

Bank this stray but harmless semicolon, you may need it elsewhere. :expressionless:

It always bugged me that you need one after variable initial lists, but not after function bodies.

a7

ken thompson (unix) and rob pike helped develop the "Go" programming language. it doesn't use semicolons. each expression is on a separate line

It is likely to fail when millis() rolls over to zero at about 49 days, but that may not be a problem in your project

Just in your next project. :slight_smile:

1 Like

it does have some explanations, have a look there:

everything in C++ is about statements (fragments of the C++ program that are executed in sequence). if you read about statements, it becomes clear:

  • An expression statement is an expression followed by a semicolon
  • 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.