Naguissa / uTimerLib

Arduino tiny and cross-device compatible timer library
https://www.foroelectro.net/electronica-digital-microcontroladores-f8/utimerlib-libreria-arduino-para-eventos-temporizad-t191.html
GNU Lesser General Public License v3.0
20 stars 9 forks source link

change TimerLib.setInterval_us(timed_function, myVar); #10

Closed 2kohm closed 4 years ago

2kohm commented 4 years ago

Hello, is it possible to change TimerLib.setInterval_us(timed_function, myVar); outside void setup(); ?

i wan't to change the interval on the fly

Naguissa commented 4 years ago

Hello,

Yes it's no problem. Calling that function will automatically ignore previous timed function so you don't need to call clearInterval before if you don't want to.

2kohm commented 4 years ago

Thanks for you fast replay :D


#include "Arduino.h"
#include "uTimerLib.h"

volatile unsigned long int prevMillis = 0;
volatile unsigned long int actMilis = 0;
unsigned long ticker = 200000;
int ticks = 0;

    void
    timed_function()
{
    Serial.println(ticks);
}

void setup()
{
    Serial.begin(57600);
    //TimerLib.setTimeout_us(timed_function, ticker);
    prevMillis = millis();
}

void ticktimer(){
    TimerLib.setTimeout_us(timed_function, ticker);
    ticks++;

    if (ticks == 384){
        ticks = 0;
    }
}

void loop()
{
    ticktimer();
    actMilis = millis();
}

how can i count ticks in the timeframe defined in ticker ?

Naguissa commented 4 years ago

I don't fully understand what are you trying to do, but anyway I've made it run.

First, few items:

Revamped code looks like:

#include "Arduino.h"
#include "uTimerLib.h"

unsigned long ticker = 200000;

volatile int ticks = 0;
volatile bool inProgressFlag = false;

void timed_function() {
  Serial.println(ticks);
  inProgressFlag = false;
}

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

void ticktimer(){
  if (!inProgressFlag) {
    inProgressFlag = true;
    TimerLib.setTimeout_us(timed_function, ticker);
  }
  ticks++;
  if (ticks == 384){
    ticks = 0;
  }
}

void loop() {
  ticktimer();
}

The problem with previous code was that TimerLib.setTimeout_us(timed_function, ticker); was resetting the timer before timed_function was called.

2kohm commented 4 years ago

Thank you, i just want to make an counter that counts the ticker time interval

Naguissa commented 4 years ago

Then If i'm not wrong it should be this way:

#include "Arduino.h"
#include "uTimerLib.h"

unsigned long ticker = 200000;

volatile unsigned int ticks = 0;
volatile bool inProgressFlag = false;

void timed_function() {
  Serial.println(ticks);
  inProgressFlag = false;
}

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

void ticktimer(){
  if (inProgressFlag) {
    ticks++;
  } else {
    inProgressFlag = true;
    ticks = 0;
    TimerLib.setTimeout_us(timed_function, ticker);
  }
}

void loop() {
  ticktimer();
}

*Edit: Added "unsigned" because ticks was overflowing.

2kohm commented 4 years ago

thanks again for your fast replay. let me make things clearer. I want to create an simple BPM Clock but with this old style micros i never get precise values.

unsigned long stepticks = 60000000 / (120 * 96);

    unsigned long timenow = micros();
    if (timenow - oldtime >= stepticks)
    {
        oldtime = timenow;
        tick++;
}

so, i want to get the timer and count for each micro senconds tick +1

2kohm commented 4 years ago

ok, this works

volatile double cal1 = (120.00 / 60.0) * 96.0;
volatile double cal2 = 2000000.0 / cal1;

void timed_function()
{
    Serial.print("\r ticks  ");
    Serial.print(ticks);
    inProgressFlag = false;
}
void setup()
{
    Serial.begin(57600);
}
void ticktimer()
{
    if (!inProgressFlag)
    {
        inProgressFlag = true;
        TimerLib.setTimeout_us(timed_function, cal2);
        ticks++;
        if (ticks == 2)
        {
            midi.send(0xF8);
            midi.update();
            ticks = 0;
    }
    }
}

void loop()
{
    ticktimer();
}

this sends 119.52 BPM instead of 120.00

Naguissa commented 4 years ago

Why don't you use setInterval and calculate us corresponding to desired BPM?

#include "Arduino.h"
#include "uTimerLib.h"

unsigned long us_per_desired_bpm = 123456;

void timed_function() {
  midi.send(0xF8);
  midi.update();
}

void setup() {
  Serial.begin(57600);
  TimerLib.setInterval_us(timed_function, us_per_desired_bpm);
}

void loop() {
  if (false) { // Here will be the condition to change BPM; us_per_desired_bpm should correspond to new BPM
    TimerLib.setInterval_us(timed_function, us_per_desired_bpm);
  }

}

us_per_desired_bpm = 1000000 * 60 / DESIRED_BPM

2kohm commented 4 years ago

tanks again, this works great

volatile double cal1 = (121.00 / 60.0) * 24.0;
volatile double cal2 = 1000000.0 / cal1;
TimerLib.setInterval_us(timed_function, cal2);
Naguissa commented 4 years ago

Perfect!