Hi,
I'm trying to send a series of timing pulses with variable rate.
I'm using a millisDelay to set a loop and another one to read the value of a pot which sets the rate.
This seems to kind of work, but whenever I turn the pot I have to stop and restart the timer to set the new rate. This stops the pulses while the pot is being turned and I'd like to keep them going even while adjusting their rate.
What is a better way to do this?
Here's the code:
#include <millisDelay.h>
const unsigned int pinSeq = 2;
const unsigned int hysterisis = 2;
const unsigned int minClock = 20;
const unsigned int maxClock = 1000;
int clockPot;
millisDelay internal_clock;
millisDelay trig_length;
millisDelay control_timer;
void setClock(int rate) {
internal_clock.stop();
internal_clock.start(rate);
}
void checkClock() {
// check if delay has timed out
if (internal_clock.justFinished()) {
internal_clock.repeat();
// start delay again without drift
// do stuff
Serial.write("X");
}
}
void checkControls() {
// check if delay has timed out
if (control_timer.justFinished()) {
float newClock = analogRead(A0);
if (clockPot + hysterisis < newClock || clockPot - hysterisis > newClock) {
clockPot = newClock;
newClock = ((newClock / 1023) * (maxClock - minClock)) + minClock;
setClock((int)newClock);
}
control_timer.repeat();
}
}
void setup() {
Serial.begin(9600);
clockPot = analogRead(A0);
internal_clock.start(100);
control_timer.start(100);
pinMode(pinSeq, OUTPUT);
}
void loop() {
checkControls();
checkClock();
}