Hi there! So I have a timer running (using the timer.h library) and I've been trying to, instead of having it run at a set speed, run at a variable speed controlled by another variable.
Is it possible to use a variable to control the speed of a timer, and if so, how would I go about doing it?
I'd prefer to stick to the timer.h library if possible, but if there is a solution with another library, I'm down to switch
What do you mean by "instead of having it run at a set speed, run at a variable speed controlled by another variable"? Are you using t.pulse()? t.oscillate()? t.every()? t.after? Perhaps if you showed some example code...
I'm using t.every() to determine the duration. I'd like to have a variable (that I can adjust using an analog in) inside t.every() in place of long period.
void setup()
{t.every(freqpotval, lfoevent);} //freqpotval is the variable I'd like to use for long duration
void loop()
{t.update();
freqpotval = map(analogRead(20),0,1023,10,30000);} //allows me to adjust freqpotval with a pot
void lfoevent()
{}//the event triggered by the timer
/////////////////////////////////////////////////////////////////////////////////////////
My ultimate goal would be to turn the pot, and have it adjust the duration of Timer t
I don't think there is a way to change the timer duration in the middle of a cycle but you can probably use .after() instead of .every() and start a new timer with the new value when the old value expires.
Most people would just use a millis() timer:
void setup() {
}
Â
void loop() {
 unsigned long freqpotval;
 static unsigned long timerStart = 0;
 freqpotval = map(analogRead(20),0,1023,10UL,30000UL);
 if (millis() - timerStart > freqpotval) {
  timerStart = millis();
  lfoevent();
 }
}
// The function called each time the timer expires
void lfoevent() {
}
thank you very much! millis() and some noodling worked perfectly!
if anyone is curious as to the final code, here ya go (it was written in processing as a test, just change void draw() to void loop() and remove the unnecessary code and it should work swell!):
int clock = 0;
int lfoclock = 0;
int lfospeed = 500; //these two variables are lfo rate. adjust these using analogRead
int prevmillis = 499; //these two variables are lfo rate. adjust these using analogRead
void setup()
{size(25,25); background(0);}//unnecessary for the arduino code
void draw()//loop() function
{background(0); stroke(210); fill(210,90); //unnecessary for the arduino code
clockfunc();} //calls the lfo function to run
void clockfunc() //this is the timer
{if (millis() - prevmillis > lfospeed)
{prevmillis = millis(); println("tick");}}//this is the output blip of the clock
void keyPressed()//keys w and s will be switched out for analogRead
{switch(key) {case 'w': lfospeed += 5; prevmillis += 5;break; } //slows down
switch(key) {case's': lfospeed -= 5; prevmillis -= 5; break;}} //speeds up
I was incorrect. Upon adapting the processing code into arduino, it still does not allow me to adjust the duration of the timer (this time the timer being a custom one built with millis(). No matter what number I change the prevmillis and lfospeed variables to, it still runs at the same speed.
Does the arduino millis() function and processing millis() function work in exactly the same way? Could the arduino be running at a more irregular speed compared to my computer?