Hi all,
I'm trying to write a program that will flash LED's attached to a wheel so that they are on for half a revolution and off for half a revolution. Revolutions are detected by a interrupt attached to a hall effect sensor and this works. In other programs I have current MPH displayed on the LED's (i.e. 1 led = 1mph etc), all driven from the interrupt.
The idea for my code is that timing of the LED ON/OFF is governed by variables that are updated every 5 seconds or so as the speed slowly changes the timing changes to maintain the half on, half off behavior.
I set the initial vales to flash every 128 milliseconds and when I start the code and spin the wheel they flash correctly but aren't changing speed of flashing as time elapses. I've tested that the code is firing every 5 seconds by increasing the pixeldelay value by one each time and this behaved as expected and the flashing slowed down every 5 seconds.
I really don't understand why the delay variables aren't updating.
Any help would greatly appreciated and you have all been of such great assistance so far in the project.
Below is the pertinent code:
volatile int revcount;
int duration;
float onerevduration;
float pixeldelay;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(2, INPUT); // Make digital 2 an input
digitalWrite(2, HIGH); // Enable pull up resistor
attachInterrupt(0, RPMfun, FALLING);
revcount = 0;
duration = 0;
onerevduration = 256.0;
pixeldelay = 1.0;
}
void loop() {
if (duration > 5000) {
onerevduration = duration / revcount;
pixeldelay = onerevduration / 256;
duration =0;
revcount = 0;
}
else {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
digitalWrite(latchPin, 1);
delay((int)(pixeldelay * 128));
duration = (int)(duration + (pixeldelay * 128));
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay((int)(pixeldelay * 128));
duration = (int)(duration + (pixeldelay * 128));
}
}
void RPMfun()
{
revcount++;
}