0

I want to count milliseconds on my Pico to make nonblocking code in C++. Arduino has the millis() function, but after looking though all the example programs and scouring the internet I couldn't find anything like that for the Pico using C++.

This is the BlinkWithoutDelay sketch from the Arduino examples that shows what I want to do:

const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
Taylor
  • 35
  • 6
  • Does your platform have the `gettimeofday()` function? – Ben Voigt Dec 09 '22 at 21:06
  • [pico time - timestamp](https://raspberrypi.github.io/pico-sdk-doxygen/group__timestamp.html) In fact, just bookmark [Pico API Documentation](https://raspberrypi.github.io/pico-sdk-doxygen/modules.html) and then there are a few extensions in the `pico-extras/src/rp2_common` directory of the `pico-extras` package that provide additional functionality not yet formally documented (sleep, etc..) – David C. Rankin Dec 09 '22 at 21:12
  • 1
    For what you show, you want `static absolute_time_t get_absolute_time (void)` but see the convenience function `static absolute_time_t make_timeout_time_ms (uint32_t ms)` It would also be better to simply set a repeating timer, and change the blink-state each time the timer interrupt fires. No looping involved. Much easier on the processor. – David C. Rankin Dec 09 '22 at 21:19
  • 1
    See [pico-examples/timer/periodic_sampler/](https://github.com/raspberrypi/pico-examples/tree/master/timer/periodic_sampler) for a repeating timer example. – David C. Rankin Dec 09 '22 at 21:33

1 Answers1

0

I decided to go with a repeating timer as suggested by some comments and the examples that used it really difficult to understand for me. So I'll try to explain it the best I can.

*Note that I don't really understand some of the values being passed into functions but I know enough to make this work. Please feel free to correct me

#include "pico/stdlib.h"

bool led_state;

//Function Called by the timer
bool timer_callback( repeating_timer_t *rt )
{   
    //Blinks the LED
    led_state = !led_state;
    gpio_put(25, led_state);

    return (true);
}

int main(){

    //From what I can tell this creates an identifier for the timer
    static repeating_timer_t timer;

    stdio_init_all(); 
    //GPIO setting (GPIO_25 is the LED pin on the pico)
    gpio_init(25);
    gpio_set_dir(25, GPIO_OUT);

    //Sets a repeating timer to call a function every 1000 milliseconds
    //add_repeating_timer_ms( int interval_in_milliseconds, ?&function to call?, ???, ?timer identifier? );
    add_repeating_timer_ms( 1000, &timer_callback, NULL, &timer );

    while(true){
        //Blinks LED briefly at a different interval than the repeating timer
        //This is to show that my code isn't blocked
        gpio_put(25, !led_state);
        sleep_ms(50);
        gpio_put(25, led_state);
        sleep_ms(450);
    }

    return 0;
}

Taylor
  • 35
  • 6