how to update periodic timer?

natee.th
Posts: 26
Joined: Fri Feb 10, 2023 5:25 pm

how to update periodic timer?

Postby natee.th » Tue Aug 13, 2024 3:55 pm

Hi everyone, I'm trying to figure out how to make a rhythm machine from esp32, using the esp_timer_start_periodic function, but I don't know how to adjust the time with any command or method, can you give me some advice?

Code: Select all

    const esp_timer_create_args_t periodic_timer_args_bpm = {
        .callback = &periodic_timer_callback_bpm,
        /* name is optional, but may help identify the timer when debugging */
        .name = "periodic"};

    ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args_bpm, &periodic_timer_bpm));
    /* The timer has been created but is not running yet */

    ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer_bpm, 2500));

Code: Select all

static void periodic_timer_callback_bpm(void *arg)
{
    bpm_rise_signal = !bpm_rise_signal;
    gpio_set_level(SYNC_OUT_PIN, bpm_rise_signal);
}

aliarifat794
Posts: 200
Joined: Sun Jun 23, 2024 6:18 pm

Re: how to update periodic timer?

Postby aliarifat794 » Tue Aug 13, 2024 4:16 pm

You can modify your code to adjust the timing based on BPM (beats per minute). You can then use this interval when calling adjust_timer_interval.

Code: Select all

#include "esp_timer.h"

esp_timer_handle_t periodic_timer_bpm;
bool bpm_rise_signal = false;
#define SYNC_OUT_PIN 25 // Example GPIO pin

static void periodic_timer_callback_bpm(void *arg)
{
    bpm_rise_signal = !bpm_rise_signal;
    gpio_set_level(SYNC_OUT_PIN, bpm_rise_signal);
}

void setup_timer(int interval_us)
{
    const esp_timer_create_args_t periodic_timer_args_bpm = {
        .callback = &periodic_timer_callback_bpm,
        .name = "periodic"
    };

    // Create the timer
    ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args_bpm, &periodic_timer_bpm));
    
    // Start the timer with the given interval
    ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer_bpm, interval_us));
}

void adjust_timer_interval(int new_interval_us)
{
    // Stop the current timer
    ESP_ERROR_CHECK(esp_timer_stop(periodic_timer_bpm));
    
    // Start the timer again with the new interval
    ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer_bpm, new_interval_us));
}

void app_main(void)
{
    // Example: Set initial BPM interval
    int initial_interval_us = 2500;
    setup_timer(initial_interval_us);

    // Adjust timer to a new interval after some condition
    int new_interval_us = 5000; // Example: New interval (e.g., based on BPM)
    adjust_timer_interval(new_interval_us);
}







MicroController
Posts: 1750
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: how to update periodic timer?

Postby MicroController » Wed Aug 14, 2024 9:22 am


Who is online

Users browsing this forum: No registered users and 35 guests