Hi,
I've been working on trying to find this glitch in my code, but after days of reviewing my code, I am hoping another set of eyes will catch what I am missing.
I am trying to use my UNO to operate two relays in different ways based on the position of a potentiometer.
The problem is that when I turn the potentiometer, the relays flicker on and then off, which is not good for the motors that I'll eventually be switching with these relays. I expect that in the future, this will be running more than the two relays I am testing with.
Attached is a very basic diagram of what I've put together. And now for the code:
void loop() {
// Make sure LEDs are off
WriteLED(10); // Clear LED
// Initialize Relays after unknown start condition
digitalWrite(Relay1, RelayOff); // set the relay 1 off
digitalWrite(Relay2, RelayOff); // set the relay 2 off
// */
Mode = ReadMode(); // Check operating mode
switch (Mode){
case 1:
WriteLED(1); // Normal
delay (sec);
ControlRelay(Relay1,OnTimeRelay1,RelayOn);
ControlRelay(Relay2,OnTimeRelay2,RelayOn);
ControlRelay(Relay2,OffTimeRelay2,RelayOff);
ControlRelay(Relay1,OffTimeRelay1,RelayOff);
break;
case 2:
WriteLED(2); // Relay 1 only
delay (sec);
ControlRelay(Relay1,OnTimeRelay1,RelayOn);
ControlRelay(Relay1,OffTimeRelay1,RelayOff);
break;
case 3:
WriteLED(3); // Relay 2 only
delay (sec);
ControlRelay(Relay2,OnTimeRelay2,RelayOn);
ControlRelay(Relay2,OffTimeRelay2,RelayOff);
break;
default:
WriteLED(0); // OFF
} // end switch
// */
} // End Loop
ReadMode() takes a reading of the potentiometer and then based on that reading, writes to the global variable "Mode"
WriteLED() lights up a series of LEDs to indicate either the mode it is in or the time that the relay is operating until
ControlRelay() takes which relay I want to turn on or off and the time to run for either.
void ControlRelay(int Relay, int RunTime, int ControlState) {
int countTime;
switch (ControlState) {
case LOW:
digitalWrite(Relay, RelayOn); // set the relay to on
break;
case HIGH:
digitalWrite(Relay, RelayOff); // set the relay to off
break;
default: // Turn relay Off in any unknown conditions
digitalWrite(Relay, RelayOff); // set the relay to off
} // end switch
for (countTime = 0; countTime < RunTime; countTime++){
WriteLED(countTime);
CountDelay();
} // end for
} // end ControlRelay
CountDelay() is where I suspect my glitch is. All it does is count off the time, but I have it check to see if the input mode has changed. If it has, to break out of the for loop.
void CountDelay(){ //
int minutes, seconds, chkMode, chkTstate;
for (minutes = 0; minutes < DelayTime; minutes ++) {
for (seconds = 0; seconds < 60; seconds++){
chkMode = ReadMode(); // Check if Mode has changed
if (chkMode != Mode) { // Immediately stop if Mode has changed
break; // Break will have a slight delay as minutes count off
} // end if
delay(sec);
OnIndicator(); // flashes LED to indicate seconds
} // end for seconds
} // end for minutes
} // end CountDelay()
int ReadMode() { // Reads ADC input value 0-1023
int newMode=0;
int PotValue = analogRead(analogPin);
if (PotValue < 31) { newMode = 0; }
else if (PotValue < 325) { newMode = 1; }
else if (PotValue < 720) { newMode = 2; }
else if (PotValue < 980) { newMode = 3; }
else { newMode = 4; } // Not implemented, default to OFF
return newMode;
}
TIA!