I need a little help trying to make a timer a little fuzzy. I want to use a fixed interval of timing like this:
____|_______|____|_____|_________|_____|___|_______|
where the pipe is the event occurrence and the underscore is the delay in an array like this:
int myIntervals = { 1000, 2000, 750, 850, 1200, 850, 500, 1000};
but the values are arbitrary.
I would like to create a slight randomness to the event, but not allow the randomness to affect the overall timing:
___*|*_____*|*__*|*___*|*_______*|*___*|*_*|*_____*|
where the randomness is described as the time contained by asterisks.
So the event always happens at the interval +- a random delay:
int fuzzyPeriod = random(-75, 75);
I've experimented around this but to no avail... I'm finding myself in a recursion when the fuzzy period is negative, or like this I get a millis() overflow problem, obviously.
int sequence[] = {1000, 750, 950, 1150, 1200, 1500, 1000, 1900, 2000};
unsigned int index;
unsigned long startMillis = 0;
unsigned int fuzzy = sequence[0];
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if (startMillis < millis()) // will have rollover issues <<<<<<<
{
if (millis() - startMillis >= fuzzy)
{
digitalWrite(13, !digitalRead(13));
startMillis += sequence[index]; // advance startMillis by the last interval used
index++;
if (index >= sizeof(sequence) / sizeof(sequence[0]))
{
index = 0;
Serial.println("Restarted Sequence");
}
fuzzy = sequence[index] + random(-75, 76); // arbitrary offset needs to be positive or negative
Serial.print("fuzzy = ");
Serial.println(fuzzy);
}
}
}
I hope I've done a good job explaining... I cannot for the life of me get this done and I know I'm to the point where I need a little help!
Aucun commentaire:
Enregistrer un commentaire