I have a float switch on pin 5... Which is closed when the water level is correct.
But somehow the only way I can get the arduino to trigger is by disconnecting pin 5, if I manually jiggle the float switch holding it down for a few seconds the arduino doesn't sense the connection breaking when I manually diddle the float bob. I tested the switch with multumeter as far as I can tell the switch is working okay. Disconnecting the switch to ground pin doesn't seem to change much only disconnecting pin 5. I had it on pin 2 before and had a bit of water spilled on the arduino and there was a little bit of 'fizzing' action happening not on the 328p but on the little chip nearby the tx/rx leds but I wiped it off... so in all honesty maybe there is something wrong with the board? But for all other intents and purposes it seems to be working fine.. perhaps there is something I am not aware of or am missing in the code. Any guidance is appreciated, thanks.
/*ardumidifier 1.01
* maciej lach
* debounce code by limor fried
* basic on/off + pump activation via float switch
* debounce float switch to avoid intermittent pump bursts and only when float consistently low
*
*/
const int float_monitor_pin = 5;
const int fan_control_pin = 3;
const int pump_control_pin = 8;
const int ssr_control_pin = 7;
const int ledPin = 13;
bool pwrState = HIGH;
const long interval = 6390; // interval at which to fan cycle (milliseconds) - disabled
const int fan1 = 180;
const int fan2 = 200;
const int runFan = 255;
bool ssrState = HIGH;
const long OnTime = (4.5 * 1000); // milliseconds of on-time
const long OffTime = (60000 - OnTime); // millis off, one minute total operation cycle
bool floatState;
bool lastFloatState = HIGH;
unsigned long previousMillisFan = 0; // will store last time LED was updated
unsigned long previousMillisSsr = 0;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 1500; // the debounce time; increase if the output flickers
void setup() {
pinMode(fan_control_pin, OUTPUT);
pinMode(pump_control_pin, OUTPUT);
pinMode(ssr_control_pin, OUTPUT);
pinMode(float_monitor_pin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(fan_control_pin, runFan);
digitalWrite(pump_control_pin, 0);
digitalWrite(ssr_control_pin, ssrState);
//digitalWrite(ledPin, ssrState);
/*
int current = millis();
while (current <= 10000)
{
digitalWrite(pump_control_pin, 1);
current = millis();
}
digitalWrite(pump_control_pin, 0);
*/
Serial.begin(9600);
}
void loop() {
//code for running fan speed
unsigned long currentMillisFan = millis(); //get current time
//cycle fan max poewer if ssr on, otherwise bistable vibrator between fan1 and fan2 via pwm
if (ssrState == HIGH)
{
analogWrite(fan_control_pin, runFan);
//Serial.println("fan 0");
}
else if (currentMillisFan - previousMillisFan >= interval) {
// Interval exceeded, store new time for next round
previousMillisFan = currentMillisFan;
// if the LED is off turn it on and vice-versa:
if (pwrState == LOW) {
pwrState = HIGH;
analogWrite(fan_control_pin, fan1);
//Serial.println("fan 0");
} else {
pwrState = LOW;
analogWrite(fan_control_pin, fan2);
//Serial.println("fan 80");
}
}
//check water float
int floatVal = digitalRead(float_monitor_pin);
if (floatVal != lastFloatState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (floatVal != floatState) {
floatState = floatVal;
// only toggle the LED if the new button state is HIGH
if (floatState == HIGH) {
digitalWrite(pump_control_pin, HIGH); //pump on
digitalWrite(ledPin, HIGH);
} else {
//digitalWrite(13, HIGH);
digitalWrite(pump_control_pin, LOW); //pump off
digitalWrite(ledPin, LOW);
}
}
}
// save the reading. Next time through the loop, it'll be the lastFloatState:
lastFloatState = floatVal;
unsigned long currentMillisSsr = millis();
if ((ssrState == HIGH) && (currentMillisSsr - previousMillisSsr >= OnTime))
{
//Serial.println("OFF");
ssrState = LOW; // Turn it off
previousMillisSsr = currentMillisSsr; // Remember the time
//digitalWrite(ledPin, ssrState); // Update the actual LED
digitalWrite(ssr_control_pin, ssrState);
}
else if ((ssrState == LOW) && (currentMillisSsr - previousMillisSsr >= OffTime))
{
//Serial.println("ON");
ssrState = HIGH; // turn it on
previousMillisSsr = currentMillisSsr; // Remember the time
//digitalWrite(ledPin, ssrState); // Update the actual LED
digitalWrite(ssr_control_pin, ssrState);
}
}
I put some print in your code to see if the button is operational
Run it and see if it prints the status of the button.
In my test with your code it printed.
/*ardumidifier 1.01
maciej lach
debounce code by limor fried
basic on/off + pump activation via float switch
debounce float switch to avoid intermittent pump bursts and only when float consistently low
*/
const int float_monitor_pin = 5;
const int fan_control_pin = 3;
const int pump_control_pin = 8;
const int ssr_control_pin = 7;
const int ledPin = 13;
bool pwrState = HIGH;
const long interval = 6390; // interval at which to fan cycle (milliseconds) - disabled
const int fan1 = 180;
const int fan2 = 200;
const int runFan = 255;
bool ssrState = HIGH;
const long OnTime = (4.5 * 1000); // milliseconds of on-time
const long OffTime = (60000 - OnTime); // millis off, one minute total operation cycle
bool floatState;
bool lastFloatState = HIGH;
unsigned long previousMillisFan = 0; // will store last time LED was updated
unsigned long previousMillisSsr = 0;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 1500; // the debounce time; increase if the output flickers
void setup() {
pinMode(fan_control_pin, OUTPUT);
pinMode(pump_control_pin, OUTPUT);
pinMode(ssr_control_pin, OUTPUT);
pinMode(float_monitor_pin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(fan_control_pin, runFan);
digitalWrite(pump_control_pin, 0);
digitalWrite(ssr_control_pin, ssrState);
//digitalWrite(ledPin, ssrState);
/*
int current = millis();
while (current <= 10000)
{
digitalWrite(pump_control_pin, 1);
current = millis();
}
digitalWrite(pump_control_pin, 0);
*/
Serial.begin(9600);
}
void loop() {
//code for running fan speed
unsigned long currentMillisFan = millis(); //get current time
//cycle fan max poewer if ssr on, otherwise bistable vibrator between fan1 and fan2 via pwm
if (ssrState == HIGH)
{
analogWrite(fan_control_pin, runFan);
//Serial.println("fan 0");
}
else if (currentMillisFan - previousMillisFan >= interval) {
// Interval exceeded, store new time for next round
previousMillisFan = currentMillisFan;
// if the LED is off turn it on and vice-versa:
if (pwrState == LOW) {
pwrState = HIGH;
analogWrite(fan_control_pin, fan1);
//Serial.println("fan 0");
} else {
pwrState = LOW;
analogWrite(fan_control_pin, fan2);
//Serial.println("fan 80");
}
}
//check water float
int floatVal = digitalRead(float_monitor_pin);
if (floatVal != lastFloatState) {
// reset the debouncing timer
Serial.println(floatVal);
Serial.println(lastFloatState);
lastDebounceTime = millis();
Serial.println(lastDebounceTime);
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (floatVal != floatState) {
floatState = floatVal;
// only toggle the LED if the new button state is HIGH
if (floatState == HIGH) {
digitalWrite(pump_control_pin, HIGH); //pump on
digitalWrite(ledPin, HIGH);
} else {
//digitalWrite(13, HIGH);
digitalWrite(pump_control_pin, LOW); //pump off
digitalWrite(ledPin, LOW);
}
}
}
// save the reading. Next time through the loop, it'll be the lastFloatState:
lastFloatState = floatVal;
unsigned long currentMillisSsr = millis();
if ((ssrState == HIGH) && (currentMillisSsr - previousMillisSsr >= OnTime))
{
//Serial.println("OFF");
ssrState = LOW; // Turn it off
previousMillisSsr = currentMillisSsr; // Remember the time
//digitalWrite(ledPin, ssrState); // Update the actual LED
digitalWrite(ssr_control_pin, ssrState);
}
else if ((ssrState == LOW) && (currentMillisSsr - previousMillisSsr >= OffTime))
{
//Serial.println("ON");
ssrState = HIGH; // turn it on
previousMillisSsr = currentMillisSsr; // Remember the time
//digitalWrite(ledPin, ssrState); // Update the actual LED
digitalWrite(ssr_control_pin, ssrState);
}
}
Well, it seems to be able to see the switch... I can't seem to be able to copy and paste more than 1 line of the serial monitor but it does switch from 0/1....
The switch is going straight from pin 5 to the ground pin... i am assuming that something is messing with the internal pullup resistor or something because sometimes it seems to work and then next minute it seems to completely stop responding to the switch. But then if I disconnect pin 5 it will 'react' to that. Maybe I will try and swap out the arduino since like I mentioned before this one had a bit of contact with water, but also it's frustrating because cannot easily pinpoint whats happening here, bit of a noviceif that wasn't apparent...
edit 5 min later:
Definitely seems like some sort of an interference issue, now it's working fine, but I swear as soon as I unplug the computer or something it will start acting up again. I will try with another dev board and give an update later.
I would try a 1K external pull up resistor, that is a resistor from the Arduino input to the 5V (or 3V3 depending on the type of Arduino). The internal pull up resistors are very weak.
It also helps with some switches that have a minimum switching current requirement before they will work correctly.