Ok, after a little testing, it seems the original frequency can range from about 5Hz to as fast as about 300Hz. The idea is measure that frequency, adjust it a bit based on the setting of a trim pot, and create an adjusted frequency to output. With that as the goal, please comment on this as an approach:
//measures timing of square wave on pin 20 (interrupt 3)
//adjustes that speed based on setting of pot on pin A0
//creates a square wave with adjusted frequency on pin 21
volatile int adjustedFreq;
volatile int latestSpeed;
volatile int lastSpeed;
unsigned long startTime;
unsigned long now;
int speedoPin =21;
void setup() {
pinMode(speedoPin, OUTPUT);
attachInterrupt(3, iSr, CHANGE);}
void loop()
//creates square
{
startTime=micros(); now=startTime;
while (now-startTime<adjustedFreq) {speedoPin=HIGH; now=micros();}
startTime=micros(); now=startTime;
while (now-startTime<adjustedFreq) {speedoPin=LOW; now=micros();}
}
void iSr()
//calculates length of most recent pulse and adjusts it based on pot setting on A0
{
latestSpeed=micros();
adjustedFreq=(latestSpeed-lastSpeed)*(analogRead(A0)/512);
lastSpeed=latestSpeed;
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.