Hey guys, attached you should find some code I have attempted to compose in hopes of remedying a problem I have with some telecom rack mounted fans. Where my hangup is at is with the “Mode Selection Button”. I am using “switch” to call a particular “case” each time it is pressed, but when it either starts off at the default mode (i.e., #1) or when you rotate back around to it, it runs the code once and stops. I know this to be true because this mode is the mode that detects the temp and speeds the fans up accordingly. SO…I have been told by many that I need to make Mode one a function and call the function so that it continues to loop and not actually have the above mentioned code in the actual “case/break” area. I have tried this every which way I can think of but obviously am not structuring it correctly as it will not compile and if it does, it will not work as intended. Any assistance with this would be greatly appreciated…
#include<LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //The following is the pin association from Digital Pin of Arduino to LCD Pin on the display: 8 to RS, 9 to E, 4 to D4, 5 to D5, 6 to D6, 7 to D7
#define pwm 11 //Digital Pin 11 is the PWM signal out to the Mosfet driving the fans.
byte degree[8] =
{
0b00011,
0b00011,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
float tempC;
int Tach1 = 2; //Fan 1 Tach signal connected to A2
int Tach2 = 3; //Fan 2 Tach signal connected to A3
int tempPin = 0; //Temo Sensor signal connected to A0
int Fan1OnlineLed = 3; //Fan 1 "On Line" LED connected to Digital Pin 3
int Fan2OnlineLed = 12; //Fan 2 "On Line" LED connected to Digital Pin 12
int HiTempLed = A1; //"Hi Temp" LED connected to Digital Pin 1
int Fan1FaultLed = 2; //Fan 1 "Fault" LED connected to Digital Pin 2
int Fan2FaultLed = 10; //Fan 2 "Fault" LED connected to Digital Pin 10
const int modePin = 13; // Active HIGH, held low by 4.7K Resistor
int mode = 0; // Selector State (Initial state = ALL OFF)
int val = 0; // Pin 13 HIGH/LOW Status
int butState = 0; // Last Button State
int modeState = 0; // Last Mode State
boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
void setup()
{
pinMode(Tach1, INPUT);
pinMode(Tach2, INPUT);
pinMode(Fan1OnlineLed, OUTPUT);
pinMode(Fan2OnlineLed, OUTPUT);
pinMode(HiTempLed, OUTPUT);
pinMode(Fan1FaultLed, OUTPUT);
pinMode(Fan2FaultLed, OUTPUT);
pinMode(tempPin, INPUT);
pinMode(modePin, INPUT);
if (debug){
Serial.begin(9600);
Serial.print("Initial Mode: ");
Serial.println(mode);
Serial.print("Setup Complete\n");
}
lcd.begin(16, 2);
lcd.createChar(1, degree);
lcd.clear();
lcd.print(" 4G Fan ");
lcd.setCursor(0,1);
lcd.print(" Controller ");
delay(2000);
lcd.clear();
lcd.print("Communications");
lcd.setCursor(0,1);
lcd.print("Hardware, Inc.");
digitalWrite(Fan1OnlineLed, HIGH);
digitalWrite(Fan2OnlineLed, HIGH);
delay(1000);
digitalWrite(Fan1OnlineLed, LOW);
digitalWrite(Fan2OnlineLed, LOW);
delay(1000);
digitalWrite(Fan1FaultLed, HIGH);
digitalWrite(Fan2FaultLed, HIGH);
delay(1000);
digitalWrite(Fan1FaultLed, LOW);
digitalWrite(Fan2FaultLed, LOW);
delay(1000);
digitalWrite(HiTempLed, HIGH);
delay(1000);
digitalWrite(HiTempLed, LOW);
delay(1000);
lcd.clear();
delay(1000);
lcd.print(" Make Mode ");
lcd.setCursor(0,1);
lcd.print(" Selection ");
delay(2000);
}
void loop() {
val = digitalRead(modePin);
if (val != butState && val == HIGH) //Increment mode value if change in button state occurs
{
mode++;
}
butState = val; // Keep track of most recent button state
if (modeState != mode){
switch ( mode ) {
//case 1 is actually handled below as default
case 2: //Place both fans in the "Hi/Max Speed" state and display "Hi Speed Mode" on LCD
analogWrite(pwm, 255);
lcd.clear();
lcd.print(" Hi Speed Mode ");
digitalWrite(Fan1OnlineLed, HIGH);
digitalWrite(Fan2OnlineLed, HIGH);
digitalWrite(HiTempLed, LOW);
digitalWrite(Fan1FaultLed, LOW);
digitalWrite(Fan2FaultLed, LOW);
delay(100);
break;
case 3: //Place both fans in the "OFF" state and display "Standby Mode" on LCD
analogWrite(pwm, 0);
lcd.clear();
lcd.print(" Standby Mode ");
digitalWrite(HiTempLed, LOW);
digitalWrite(Fan1OnlineLed, LOW);
digitalWrite(Fan2OnlineLed, LOW);
digitalWrite(Fan1FaultLed, LOW);
digitalWrite(Fan2FaultLed, LOW);
delay(100);
break;
default:
mode = 1;
{lcd.clear();
int tempPin = 0;
int temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(((temp)*18+5)/10+32); // Printing temperature on LCD
lcd.print(" F ");
lcd.setCursor(0,1);
if(temp<=20)
{
lcd.clear();
int tempPin = 0;
int temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(((temp)*18+5)/10+32); // Printing temperature on LCD
lcd.print(" F ");
lcd.setCursor(0,1);
analogWrite(pwm, 0);
lcd.print("Fan Speed: OFF ");
digitalWrite(Fan1OnlineLed, LOW);
digitalWrite(Fan2OnlineLed, LOW);
delay(100);
}
else if(temp<=30)
{
lcd.clear();
int tempPin = 0;
int temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(((temp)*18+5)/10+32); // Printing temperature on LCD
lcd.print(" F ");
lcd.setCursor(0,1);
analogWrite(pwm, 204);
lcd.print("Fan Speed: 80% ");
digitalWrite(Fan1OnlineLed, HIGH);
digitalWrite(Fan2OnlineLed, HIGH);
delay(100);
}
else if(temp<=35)
{
lcd.clear();
int tempPin = 0;
int temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(((temp)*18+5)/10+32); // Printing temperature on LCD
lcd.print(" F ");
lcd.setCursor(0,1);
analogWrite(pwm, 230);
lcd.print("Fan Speed: 90% ");
digitalWrite(Fan1OnlineLed, HIGH);
digitalWrite(Fan2OnlineLed, HIGH);
delay(100);
}
else if(temp<=40)
{
lcd.clear();
int tempPin = 0;
int temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(((temp)*18+5)/10+32); // Printing temperature on LCD
lcd.print(" F ");
lcd.setCursor(0,1);
analogWrite(pwm, 255);
lcd.print("Fan Speed: 100% ");
digitalWrite(Fan1OnlineLed, HIGH);
digitalWrite(Fan2OnlineLed, HIGH);
delay(100);
}
else if(temp>=50)
{
lcd.clear();
int tempPin = 0;
int temp = analogRead(tempPin);
temp = (5.0 * temp * 100.0)/1024.0;
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(((temp)*18+5)/10+32); // Printing temperature on LCD
lcd.print(" F ");
lcd.setCursor(0,1);
analogWrite(pwm, 255);
lcd.print("Hi Temp Warning!");
digitalWrite(HiTempLed, HIGH);
digitalWrite(Fan1OnlineLed, HIGH);
digitalWrite(Fan2OnlineLed, HIGH);
delay(100);
}
break;
} // end switch
// } // end of "if mode = 0" check
} // end of ModeState check
modeState = mode; // Keep track of mode recent mode value
delay(10); // slow the loop just a bit for debounce
}}