Decrease the frequency of the loop() loop

Hi everybody! Tell me, how can I control the frequency of the loop () loop? I surfed through articles and forums. Found the following command: #define F_CPU 16000000UL. I tried to set a lower frequency with it, up to a value of 1, but I did not see a noticeable result.

Why do you want to do this ?

What problem are you trying to solve ?

Just because loop() runs at a certain frequency does not mean that the same code within it needs to execute every time that loop() repeats

Of course, you could just put a delay() in loop() if you really wanted to

Why not use millis() for timing?

unsgined long delayOneK=1000;
unsigned long timePast = millis();

voided setup()
{
if ( (millis() - timePast) >= delayOneK )
{
this will execute one a second
timePast=millis();
}

}

Look at File|Examples|02.Digital|BlinkWithoutDelay to use millis() for timing instead of delay().

I want to measure the frequency of a sinusoid (how many ones and zeros) that comes to my digital input. I can't use millis(), because I need to read the network frequency, and my frequency is high. How to be? I don't know.

The macro F_CPU 16000000UL is just the info for you and for your program, so you know what freq the controller is running at. From the fact that you change this number, the frequency of the chip will not change.

I know. I was hoping that I could kind of slow down the execution of my commands.

Why? I dont't understand the connection.

and so you need to DECREASE the speed of mcu?
Don't you need to run the loop as fast as possible to measure the high frequency signal?

I need to examine a signal with a frequency of 50 Hz. Can't figure out how to do it?

The frequency of loop() depends on the number and duration of commands in it. The more commands in the loop, the lower its frequency. Accordingly, if you want to slow down the loop - just add some long commands to it, the easiest way is delays

Read sequentially the values of the signal at the input for several periods, save to an array and analyze it the way you want. For example, to find the frequency of a signal, you can calculate the average time between its maximums.

Okay, how do I know at what frequency my cycle will run? Through calls to millis() and micros() ?

I do. I enter the received data at first in an array.

indeed

What is your program experience? It seems to me that you need to start from some simple project. Perhaps to blink the led with predefined frequency using the millis() would be a good start.

I have the following code:

#define F_CPU 16000000UL
uint32_t tmr1;         // timer variable 1
uint32_t tmr2;         // timer variable 2
#define MY_PERIOD 20  // period of 20 ms
int values[500] = {};
int t;

void setup() {

  PORTC &= ~0b00100000;   // Setting to zero (INPUT) pin digital 19 (A5)
  Serial.begin(9600);     // setting the port speed (bps, baud)
  
}

void loop() {

 byte y = bitRead(PINC, 5);


  if (millis() - tmr1 >= MY_PERIOD) {  
    tmr1 = millis();                   // reset the timer
    for (t = 0; t < 500; t++) {
      values[t] = bitRead(PINC, 5);
    }
  }
  if (millis() - tmr2 >= (MY_PERIOD+1000)) {  
    tmr2 = millis();                  // reset the timer
  
  if (t = 499) {
    for (int w = 0; w < 500; w++) {
      Serial.println (values[w]);
    }
    Serial.println ("Array successfully retrieved");
  }
}

I would use micros() timing to count pin state transitions per second and divide by 2.
The faster my loop runs, the more often I can read the pin and the closer to 1 second I can do reads.

I can't use millis(), because I need to read the network frequency, and my frequency is high. How to be? I don't know.

When accuracy counts, use micros. Dealing with a slower network should not be a problem.

Ok, can you provide an example in C code?

In this code you read all 500 values in a turm instead of read it sequentially with 20ms interval.

I think you want to swap the for and millis loop:

 t =0;
 while(t < 500) {
   if (millis() - tmr1 >= MY_PERIOD) {  
      tmr1 = millis();                   // reset the timer
      values[t] = bitRead(PINC, 5);
      t++;
    }
  }

but the code above will be blocking, it is just an example

In order to analyze a signal with frequency X, you must sample at least X*2 (Nyquist's theorem).
To improve accuracy, it is better to take readings more often, so I recommend taking an interval of not 20ms, but for example 1ms or even shorter

I succeeded using your hint. Here's what I got:

#define F_CPU 16000000UL
uint32_t tmr1;         // timer variable 1
//uint32_t tmr2;         // timer variable 2
#define MY_PERIOD 1  // period of 1 ms
int values[500] = {};
int t;

void setup() {

  PORTC &= ~0b00100000;   // Setting to zero (INPUT) pin digital 19 (A5)
  Serial.begin(9600);     // setting the port speed (bps, baud)
  
}

void loop() {

 byte y = bitRead(PINC, 5);


 t = 0;
  while (t < 500) {
    if (millis() - tmr1 >= MY_PERIOD) {
      tmr1 = millis();                   // reset the timer
      values[t] = bitRead(PINC, 5);
      t++;
    }
  }
 // if (millis() - tmr2 >= (MY_PERIOD+1000)) {  
   // tmr2 = millis();                  // reset the timer
  
  if (t = 499) {
    for (int w = 0; w < 500; w++) {
      Serial.println (values[w]);
    }
    Serial.println ("Array successfully retrieved");
  }
//}

A manner to make the loop run at certain frequency is to turn the solutions offered inside out.

I use 100 Hz, giving me 10 ms to do anything else.

void loop()
{
  static unsigned long loopTime;

  unsigned long now = millis();
  if (now - loopTime < 10)
    return;         // not time to run the loop

  loopTime = now;

// everything below here runs 100 Hz

}

In the rest of the loop, use now instead of calling millis().

And make sure you do not use more than 10 milliseconds getting the work done.

That's one call to nillis(), and no calls to delay(), and the only code involved with timing.

Adjust constants to taste.

If you add things that need the higher speed of loop, put them above the code that throttles the rest of the loop.

HTH

a7