Variables not updating in a loop
hi all,
i'm trying write program flash led's attached wheel on half revolution , off half revolution. revolutions detected interrupt attached hall effect sensor , works. in other programs have current mph displayed on led's (i.e. 1 led = 1mph etc), driven interrupt.
the idea code timing of led on/off governed variables updated every 5 seconds or speed changes timing changes maintain half on, half off behavior.
i set initial vales flash every 128 milliseconds , when start code , spin wheel flash correctly aren't changing speed of flashing time elapses. i've tested code firing every 5 seconds increasing pixeldelay value 1 each time , behaved expected , flashing slowed down every 5 seconds.
i don't understand why delay variables aren't updating.
any appreciated , have been of such great assistance far in project.
below pertinent code:
[font=courier]volatile int revcount;
int duration;
float onerevduration;
float pixeldelay;
void setup() {
//set pins output because addressed in main loop
pinmode(latchpin, output);
pinmode(2, input); // make digital 2 input
digitalwrite(2, high); // enable pull 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++;
}
[/font]
i'm trying write program flash led's attached wheel on half revolution , off half revolution. revolutions detected interrupt attached hall effect sensor , works. in other programs have current mph displayed on led's (i.e. 1 led = 1mph etc), driven interrupt.
the idea code timing of led on/off governed variables updated every 5 seconds or speed changes timing changes maintain half on, half off behavior.
i set initial vales flash every 128 milliseconds , when start code , spin wheel flash correctly aren't changing speed of flashing time elapses. i've tested code firing every 5 seconds increasing pixeldelay value 1 each time , behaved expected , flashing slowed down every 5 seconds.
i don't understand why delay variables aren't updating.
any appreciated , have been of such great assistance far in project.
below pertinent code:
[font=courier]volatile int revcount;
int duration;
float onerevduration;
float pixeldelay;
void setup() {
//set pins output because addressed in main loop
pinmode(latchpin, output);
pinmode(2, input); // make digital 2 input
digitalwrite(2, high); // enable pull 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++;
}
[/font]
i've been playing around think line problem lies:
[font=courier]onerevduration = duration / revcount;[/font]
still no idea how fix it!
[font=courier]onerevduration = duration / revcount;[/font]
still no idea how fix it!
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Variables not updating in a loop
arduino
Comments
Post a Comment