I'm trying to turn on a relay and an led at the same time in my aquarium controller code and can't seem to get it to work. I'm using the arduino MEGA R3 running arduino 1.0. The LED is on pin 36 and the relay is on pin 52. When I test all the relays using the blink function, everything works fine, but when I combine it with my code it works intermittently. Once I get it working, I add whatever changes later in the program to another led/relay pair, then it stops working. So it's not a connection problem because I didn't touch anything. It shouldn't be in the code because the changes were farther down in the loop. I though it was a current draw problem, so I removed the led part of the code and the same thing happend (worked next compile, added the changes to later part of code, then failed next recompile).
The only thing I haven't tried is a temperature problem so I have it powered off right now.I also have an ethernet shield and RTC module hooked up if that matters. Using pins 20-21 for serial comm
Here's the code:
//** AUTO-TOP-OFF *************************** Water is added from Kalk reactor **************************
int ato_level = rangefinderDist(SRF04_US);
int kalk_level = rangefinderDist(Parallax);
Serial.print(ato_level);
Serial.print(" ");
Serial.println(kalk_level);
delay(100);
//Serial.print(hour); Serial.println(ato_hour);
if ((ato_level > ato_max && hour >= ato_hour)||(ato_level > ato_max && ato_day!=day && hour>=24-ato_hour)) {
ato_day = day;
//while (abs(ato_prev-ato_level) <= 3) { // look for change in water level
// add counter in case of fault or for loop x numebr of times
// add part that only adds h2o if reading is different that 1 from previous reading
if (hour < (24-ato_freq)){ // limiting the freqency of ato fill ups
ato_hour = hour+ato_freq;
//Serial.println(ato_hour);
}
else {
ato_hour = 24-hour+ato_freq;
}
Serial.print("Next ato hour - ");
Serial.print(ato_hour);
Serial.print(" Day - ");
Serial.println(day);
//digitalWrite(ato, HIGH); // TURN ON ATO
digitalWrite(ato, LOW);
delay(100);
digitalWrite(ato, HIGH);
// delay(100);
// digitalWrite(ato_light, HIGH);
delay(ato_time);
Serial.print("Water level - ");
Serial.println(ato_level);
//Serial.println("add water");
Serial.print(hour);
Serial.print(':');
Serial.print(minute);
Serial.print(':');
Serial.println(second);
//}
ato_prev = ato_level;
}
//digitalWrite(ato, LOW); // TURN OFF ATO
digitalWrite(ato,LOW);
digitalWrite(ato_light, LOW);
//** Refill Kalk resevouir *************************************************************************
if ((kalk_level > kalk_max && hour >= kalk_hour)||(kalk_level > kalk_max && kalk_day!=day && hour>=24-kalk_hour)) {
kalk_day = day;
//while (abs(kalk_prev-kalk_level) <= 3) { // look for change in water level
// add counter in case of fault or for loop x numebr of times
// limiting the freqency of kalk fill ups
if (hour < (24-kalk_freq)){
kalk_hour = hour+kalk_freq;
//Serial.println(kalk_hour);
}
else {
kalk_hour = 24-hour+kalk_freq;
}
Serial.print("Next kalk fill up hour - ");
Serial.print(kalk_hour);
Serial.print(" Day - ");
Serial.println(day);
//digitalWrite(kalk, HIGH); // TURN ON kalk
digitalWrite(kalk, LOW);
delay(100);
digitalWrite(kalk, HIGH);
// delay(100);
// digitalWrite(kalk_light, HIGH);
delay(kalk_time);
Serial.print("Kalk Reactor level - ");
Serial.println(kalk_level);
//Serial.println("add water");
Serial.print(hour);
Serial.print(':');
Serial.print(minute);
Serial.print(':');
Serial.println(second);
//}
kalk_prev = kalk_level;
}
//digitalWrite(kalk, LOW); // TURN OFF ATO
digitalWrite(kalk,LOW);
digitalWrite(kalk_light, LOW);
Test code, works every time:
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
for(int pin = 48; pin<53; pin++){
pinMode(pin, OUTPUT);
}
pinMode(13, OUTPUT);
}
void loop() {
for(int pin = 48; pin<53; pin++){
digitalWrite(pin, HIGH);
}
digitalWrite(13, HIGH);// set the LED on
delay(1000); // wait for a second
for(int pin = 48; pin<53; pin++){
digitalWrite(pin, LOW); // set the LED off
}
digitalWrite(13, LOW);
delay(1000); // wait for a second
}