Suddenly my millis() quit working. Can I not have a if followed by ar for loop?
if (currentMillis - previousReadFan >= readFanInterval) {
for (int i = 0; i < 2; i++) {
const int hallSensorPins[] = {2, 4}; // An array of pin numbers to which fans are attached
const int pinCount = 2; // The number of pins (i.e. the length of the array)
//const int pwmPin = 3;
const int ledPin = 13; // Pin 13 is used to test alarm
const int readFanInterval = 2000; // number of millisecs between reading the fan
const int alarmInterval = 1000; // number of millisecs for the alarm to go
const int fanMaxRev[2] = {1550, 1400}; // Array for storing the fans max RPM
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousReadFan = 0; // will store last time the LED was updated
unsigned long previousAlarm = 0; // will store last time the LED was updated
unsigned long previousPulseTime0; // Variables for storing the duration of the previous pulse.
unsigned long previousPulseTime1;
unsigned long highPulseTime0; // Variables for storing the duration of the high pulse
unsigned long highPulseTime1;
unsigned long lowPulseTime0; // Variables for storing the duration of the low pulse
unsigned long lowPulseTime1;
unsigned long newPulseTime0; // Variables for storing the duration of the new pulse
unsigned long newPulseTime1;
// Array for storing the duration of the previous pulse times.
unsigned long previousPulseTimes[2] = {previousPulseTime0, previousPulseTime1};
// Array for storing the duration of the high pulse times.
unsigned long highPulseTimes[2] = {highPulseTime0, highPulseTime1};
// Array for storing the duration of the low pulse times.
unsigned long lowPulseTimes[2] = {lowPulseTime0, lowPulseTime1};
// Array for storing the duration of the new pulse times.
unsigned long newPulseTimes[2] = {newPulseTime0, newPulseTime1};
int revsPerMinute0; // Variable for storing RPM value
int revsPerMinute1; // Variable for storing RPM value
// Array for storing the duration of the RPMs.
int revsPerMinute[2] = {revsPerMinute0, revsPerMinute1};
int smoothedRevsPerMinute0; // Variable for storing the smoothed RPM value
int smoothedRevsPerMinute1; // Variable for storing the smoothed RPM value
// Array for storing the duration of the smoothed RPMs.
int smoothedRevsPerMinute[2] = {smoothedRevsPerMinute0, smoothedRevsPerMinute1};
int percentage0; // Variable for storing percentage value
int percentage1; // Variable for storing percentage value
// Array for storing the percentages.
int percentages[2] = {percentage0, percentage1};
const int samples = 3; // Amount of samples that will be stored and used
// for smoothing
void setup() {
Serial.begin(9600); // We start a serial for debugging readouts
pinMode(ledPin, OUTPUT); // Set LedPin to output
/*
pinMode(PWMPin, OUTPUT); // generate 25kHz PWM pulse rate on Pin 3
TCCR2A = 0x23; // Set up Fast PWM on Pin 3 COM2B1, WGM21, WGM20
TCCR2B = 0x0A; // Set prescaler WGM21, Prescaler = /8
OCR2A = 79; // Set PWM frecuency
OCR2B = 40; // duty cycle for Pin 3 (0-79) generates 1 500nS pulse even when 0 :(
*/
for (int i = 0; i < pinCount; i++) {
pinMode(hallSensorPins[i], INPUT_PULLUP);
}
for (int i = 0; i < 2; i++) {
highPulseTimes[i] = pulseIn(hallSensorPins[i], HIGH);
lowPulseTimes[i] = pulseIn(hallSensorPins[i], LOW);
newPulseTimes[i] = highPulseTimes[i] + lowPulseTimes[i];
previousPulseTimes[i] = newPulseTimes[i];
}
}
void loop() {
currentMillis = millis(); // capture the latest value of millis()
readFans();
}
void readFans() {
if (currentMillis - previousReadFan >= readFanInterval) {
for (int i = 0; i < 2; i++) {
highPulseTimes[i] = pulseIn(hallSensorPins[i], HIGH);
lowPulseTimes[i] = pulseIn(hallSensorPins[i], LOW);
newPulseTimes[i] = highPulseTimes[i] + lowPulseTimes[i];
if (lowPulseTimes[i] == 0 && highPulseTimes[i] == 0) { // If both high and low pulsetime
fan_alarm(); // returns zero call function alarm
digitalWrite(13, HIGH); // Activates the LedPin
}
else {
digitalWrite(13, LOW); // Deactivates the LedPin
}
if (newPulseTimes[i] < (previousPulseTimes[i] * 0.9)) { //Kind of a high-pass filter
(newPulseTimes[i] = (previousPulseTimes[i] * 0.9)); //Takes outlier values down
}
if (newPulseTimes[i] > (previousPulseTimes[i] * 1.1)) { //Kind of a high-pass filter
(newPulseTimes[i] = (previousPulseTimes[i] * 1.1)); //Takes outlier values down
}
revsPerMinute[i] = (1000000 * 60)/(newPulseTimes[i] *2); // Convert pulse time to RPM
// Smoothing out the value by adding earlyer values and divide to an average
smoothedRevsPerMinute[i] = smoothedRevsPerMinute[i] + ((revsPerMinute[i] - smoothedRevsPerMinute[i])/samples);
// Calculate the fans spinning in percentage of its max
percentages[i] = (smoothedRevsPerMinute[i] / (fanMaxRev[i]/100)); //Current RPMs divided by Max RPMs
percentages[i] = constrain(percentages[i], 0, 100); //Constrains percentage scale
// Do some serial printing for debugging
Serial.print("High Pulsetime");
Serial.print("\t");
Serial.print("\t");
Serial.println(highPulseTimes[i], DEC);
Serial.print("Low Pulsetime");
Serial.print("\t");
Serial.print("\t");
Serial.println(lowPulseTimes[i], DEC);
Serial.print("Halfrev Microseconds");
Serial.print("\t");
Serial.println(newPulseTimes[i], DEC);
Serial.print("Unsmoothed per minute");
Serial.print("\t");
Serial.println(revsPerMinute[i], DEC);
Serial.print("Revolutions per minute");
Serial.print("\t");
Serial.println(smoothedRevsPerMinute[i], DEC);
Serial.print("Percentage of max revs");
Serial.print("\t");
Serial.println(percentages[i], DEC);
previousPulseTimes[i] = newPulseTimes[i];
previousReadFan += readFanInterval;
delay(1000);
}
}
}