Pages: 1 [2]   Go Down
Author Topic: Aquarium Auto Top Off Fail safe  (Read 3493 times)
0 Members and 1 Guest are viewing this topic.
SoCal
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int ledPin = 2;
const int floatSwitch = 4;
int floatStatecounter = 0;
int buttonState = 0;
long lastButtonState = 0;
long failSafe = 5000;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(floatSwitch, INPUT);
  Serial.begin(9600);
}
void loop() {
  //get switch state
  buttonState = digitalRead(floatSwitch);
  if (floatSwitch == HIGH){
    floatStatecounter = millis();
  }
if (millis() >= 5000){
  digitalWrite(ledPin, HIGH);
}
Serial.println(buttonState);
delay(10);

}

Ok, Im getting the LED to light right when the switch falls to HIGH.

I think it has to do with this right here
Code:
if (floatSwitch == HIGH){
    floatStatecounter = millis();
  }
if (millis() >= 5000){

Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (millis() >= 5000){

Rarely does this code actually do what the programmer intended. millis() returns the number of milliseconds since the Arduino was last reset or powered on, so that if statement is checking to see if it's been 5 seconds since power on/reset. Another thing to think about is that you are setting floatStatecounter, but never using it again. These two issues are related.
Logged

SoCal
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I will work on this tomorrow and see what I can do with it. I  feel like I almost have it or else I am completely lost...

I don't want to give in and just ask for the code just yet so bear with me! 

Thanks for your help!
Logged

SoCal
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so I am like 95% done now and the code is partially working right.  The problem I am having is resetting the counter when the switch is dropped back to the LOW state.  As of now the code simply just stops counting.  I though that if I reset the floatStatecounter = 0 it would reset on the way back around through the loop.

Any help?

Quote
const int ledPin = 2;
const int floatSwitch = 4;
int floatStatecounter = 0;
int buttonState = 0;
long lastButtonState = 0;
long failSafe = 5000;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(floatSwitch, INPUT);
  Serial.begin(9600);
}
void loop() {
  //Set Float State Counter = 0
  floatStatecounter == 0;
  //get switch state
  buttonState = digitalRead(floatSwitch);
  if (buttonState == HIGH){
    floatStatecounter++;
  }
if (floatStatecounter >= 5){
  digitalWrite(ledPin, HIGH);
}
Serial.println("buttonState:");
Serial.println(buttonState);
Serial.println("floatStatecounter");
Serial.println(floatStatecounter);
delay(1000);
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

typography or ...??  floatStatecounter == 0; it should be  floatStatecounter = 0;

also you can try remove this line and do it like this

Code:
if (buttonState == HIGH) { floatStatecounter++; } else { floatStatecounter = 0; }
« Last Edit: October 18, 2012, 07:27:17 pm by invader7 » Logged

SoCal
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, thanks, I fixed it and added a second output to control a pump relay. 

Code:
const int ledPin = 2;
const int floatSwitch = 4;
const int pump = 5;
int floatStatecounter = 0;
int buttonState = 0;
long lastButtonState = 0;
long failSafe = 5000;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(floatSwitch, INPUT);
  pinMode(pump, OUTPUT);
  Serial.begin(9600);
}
void loop() {

  //get switch state
  buttonState = digitalRead(floatSwitch);
  if (buttonState == HIGH){
    floatStatecounter++;
    digitalWrite(pump, HIGH);
  }
  else{
      //Set Float State Counter = 0
  floatStatecounter = 0;
  digitalWrite(pump,LOW);
  }
 
if (floatStatecounter >= 5){
  digitalWrite(ledPin, HIGH);
  digitalWrite(pump,LOW);
}
else{
  digitalWrite(ledPin, LOW);
}

Serial.println("buttonState:");
Serial.println(buttonState);
Serial.println("floatStatecounter");
Serial.println(floatStatecounter);
Serial.println("pump State");
Serial.println(pump);
delay(1000);
}

I used delay() at the end of the code to slow it down, What if I wanted to add, say, Temperature monitoring later on to this code so I can control fans?  Would this delay(1000) effect that?  Basically what I am asking is will using this delay(1000) negatively effect future sampling code?
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It may. If you want to do any time critical actions, then you'll run into issues down the road.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Basically what I am asking is will using this delay(1000) negatively effect future sampling code?

The answer is in Blink without delay example.
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not just incorporate some mechanical redundancy into your design.....i.e. a 2nd float switch mounted at the very top of the tank that will trigger the arduino to shut off the pump if it switches states?  That is what I did with mine.  Actually mine has 3 float switches.  One mounted at the top as an emergency cutoff, one mounted at the high end of my desired water level and one mounted at the low end of my desired water level.  I call them:

Code:
High_Float Switch
Low_Float_Switch
Emergency_Cutoff_Switch

Pseudo-code is:

If High_Float_Switch is off && Low_Float_Switch is off && the pump is off  //water level is too low
{
turn on the pump
}

If High_Float_Switch is on && Low_Float_Switch is on && the pump is on//water level is correct
{
turn off the pump
}

If Emergency_Cutoff_Switch is on && the pump is on //water level is too high
{
turn off the pump
}

The reason for having a high and low float switch is to avoid the rapid cycling of the pump that can occur as the float switch bobbles up and down on the surface of the water.  If you don't choose to do it that way, I would suggest putting in a time delay that will cause the pump to run a minimum amount of time after it receives a triggering pulse from the float switch.  Just make sure that you don't use the delay() function for this because delay() will cause it to miss a triggering pulse from the Emergency_Cutoff_Switch unless you put that routine into an interrupt which probably wouldn't be the best solution.
« Last Edit: October 19, 2012, 08:13:36 am by jerseyguy1996 » Logged

Arduino Uno;
Mega328

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another precaution I've seen suggested in acquarium threads is to restrict how often the system can respond to the low water level indicator. You might for example, only allow the top-up pump to run once a day, thus resticting the possibility of a serious flood if you have hardware issues.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd make the emergency float switch physically interrupt the pump power, so the pump will be shut off even if the Arduino or the sketch is non-functioning.
Having an emergency switch pass through software is scary ;-)
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd make the emergency float switch physically interrupt the pump power, so the pump will be shut off even if the Arduino or the sketch is non-functioning.
Having an emergency switch pass through software is scary ;-)

Really good point and something that I should probably implement with my setup.  I was thinking more about the possibility of a malfuntioning float switch (i.e. a snail deciding to crawl on it and holding it in the down position) without even considering a software problem.  Just make sure that you don't run the pump current through the float switch.  Pick up a 5v solenoid and use the float switch to trigger the solenoid on and off.  Run the pump current through the solenoid.
Logged

Arduino Uno;
Mega328

SoCal
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, thanks for all the advise.  I also want to thank everyone for their help!  I am appreciative that you guys didn't just code this for me.  It felt so good figuring it out on my own and now I have a much better understanding of how the code syntax needs to be written.

Thanks again, and im almost wishing I had bought the mega board instead of the uno...after I add the LCD display I won't have many pins left...
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
after I add the LCD display I won't have many pins left...

You can always use a serial LCD display. They've got a PIC or AVR uC on the back that takes care of driving the LCD pins. You just send chars to it via a serial interface and they're displayed.

I've bought three or four different types. They just need one pin (plust vcc and gnd, of course), because they're receive-only.
Logged

Pages: 1 [2]   Go Up
Jump to: