int Gled = 9;
int Rled = 10;
int Bled = 11;
int color = 4;
int bright = 7;
int mode =8;
volatile float time = 0;
volatile float time_last = 0;
volatile int rpm_array[5] = {0,0,0,0,0};
void setup()
{
pinMode(Rled, OUTPUT);
pinMode(Gled, OUTPUT);
pinMode(Bled, OUTPUT);
pinMode(color, INPUT);
pinMode(bright, INPUT);
pinMode(mode, INPUT);
attachInterrupt(0, rpm_interrupt, FALLING);
}
void loop()
{
int rpm = 0;
while(1){
analogWrite(Bled, rpm );
analogWrite(Rled, rpm + 128);
analogWrite(Gled, 255 - rpm);
delay(1000);
if(time > 0)
{
//5 Sample Moving Average To Smooth Out The Data
rpm_array[0] = rpm_array[1];
rpm_array[1] = rpm_array[2];
rpm_array[2] = rpm_array[3];
rpm_array[3] = rpm_array[4];
rpm_array[4] = 300*(100000/(time*7));
//Last 5 Average RPM Counts Eqauls....
rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
}
}
//Capture rpm
void rpm_interrupt()
{
time = (micros() - time_last);
time_last = micros();
}