Here is the code. I'm sure there are many better ways of coding this, but I am a total newbie to this and my only previous programming experience had been Basic and LOGO in late 1980's
unsigned long echo = 0;
int echoPin = 7; // ultrasound pin listen
int trigPin = 8; // ultrasound pin ping
int pumpPin = 13; //pump pin
unsigned long ultrasoundValue = 0;
void setup()
{
pinMode (pumpPin,OUTPUT);
digitalWrite (pumpPin,LOW);
pinMode (trigPin,OUTPUT);
pinMode (echoPin, INPUT);
}
unsigned long currentMillis = millis ();
long previousMillis = 0; // sets previousMillis to 0
long interval = 1800000; // sets interval between water level checks at 30 minutes (1.800.000 miliseconds)
long duration = 0;
long distance = 0;
void loop ()
{
pingcheck:
digitalWrite (trigPin, LOW); // clears the pingpin for signal
delayMicroseconds(2); // for 2 miliseconds
digitalWrite (trigPin, HIGH);
delayMicroseconds(5); // signal on for 5 miliseconds
digitalWrite (trigPin, LOW); // signal off
duration = pulseIn (echoPin, HIGH);
distance = duration/58.2;
if (distance > 23) // if the distance to the water is more than 23cm
{
digitalWrite (pumpPin, HIGH); // turns the pump ON
delay(500); // runs the pump for 0.5 second
goto pingcheck; // rechecks the water level
}
else
{digitalWrite(pumpPin, LOW); // if the distance to the water is less than 23cm, go to timeout
goto timeout;}
timeout:
if (millis() - previousMillis > interval) // if more time than (interval) has passed from the last check, check water level again
{previousMillis = millis (); // record time of the current water level check
goto pingcheck;}
else
// continuously running code (temp probe)
goto timeout;
}
My next step is to add a 20x4 display and connect some temperature probes so I can use the Arduino as a thermometer, too. I'm also working on a function which will shut off the return pump, skimmer, and power heads for either 10 or 30 minutes depending on how long you hold a push button down (momentarily or 1 second) so I can feed my fish or corals without having to remember to turn everything on.
Here is the link for the 4-channel relay. It will handle 10 amp of 250V AC or 10 amp of 30V DC and you can wire them as either normally closed or normally open. I believe the guy also sells single and dual channel boards. If you don't like the clicking of mechanical relays, solid state relays are the way to go. They are a little bit more expensive, but totally silent.
Edited by Carlsson - October 20 2013 at 10:39am