Appreciate your help. How about like this:
const int trimPot = A0; //voltage divider to adjust speedometer output
const int speedPin = 21; //pin for new speed pulse train
int pulseState = LOW; // state used to set the pulse
volatile unsigned long currentMicros = 0; //to measure new pulse
volatile unsigned long previousMicros= 0; //to mark prior pulse
unsigned long currentSpeed = 0; //to measure incoming pulse
unsigned long previousSpeed = 0; //to mark prior pulse
unsigned long interval = 1; //original pulse length
float adjustPercent = 1; //adjustment to original pulse length
void setup()
{
pinMode(speedPin, OUTPUT);
pinMode(13, OUTPUT);
attachInterrupt (3, iSr, CHANGE);
}
void loop()
{
currentMicros = micros(); //get the current micros count
adjustPercent = (analogRead(trimPot)/512);
interval=interval*adjustPercent;
if(currentMicros - previousMicros > interval) //check to see if it's time to change
{
previousMicros = currentMicros; //keep track of the last time pulse changed
if (pulseState == LOW) pulseState = HIGH; else pulseState = LOW;
digitalWrite(13, pulseState); //show pulse train on built in LED
digitalWrite(speedPin, pulseState); //flip the state of the new sqaure wave
}
}
void iSr()
{
currentSpeed=micros();
interval=(currentSpeed-previousSpeed); //how long since last change
previousSpeed=currentSpeed;
}
interval needs to be volatile and decide what it's really for.
pulseState should be type byte.
adjustPercent = (analogRead(trimPot)/512);
interval=interval*adjustPercent;
The above code may run many times before interval is up, the dial does not set frequency and the ISR interval change is another mystery, also there's better ways than float.
Are you wanting to set the frequency with a dial? And the ISR to read the frequency? Or control frequency at least partly through the ISR?
Is that variable interval supposed to be changed by the ISR (triggered by writes in the code after interval is up) and then changed/initialized next time through loop()?
C code ^ is bitwise xor.
bool A xor bool B returns 1 if A and B are different, 0 if the same.
You could try
pulseState ^= 1; // flips pulseState between 0 and 1
or if that doesn't compile:
pulseState = pulseState ^ 1; // flips pulseState between 0 and 1