sketch problem? NANO problem?

I’m trying to dose kalk every hour for a day, (24 times) then a single dose of mag, then start dosing kalk again. After a day or so, everything stops.

I wonder if someone could please look at the sketch to help figure out if the sketch is wrong or maybe the NANO is bad?

I’m using a 12V power supply for the NANO and optocouplers and another 12V power supply for the dosing pumps.

  unsigned long hoursInMs(int h)  
{  
  return h*3600000UL;         //values will be expressed in hours 
 
}

  long minutesInMs(int m){ 
  return m*60000L;           //values will be expressed in minutes 
 
} 

  long secondsInMs(int s){ 
  return s*1000L;            //values will be expressed in seconds
} 

 
 void setup() 
{   
  pinMode(3, OUTPUT);       // pin 3 controls mixing pump
  pinMode(4, OUTPUT);       // pin 4 controls kalk slurry
  pinMode(5, OUTPUT);       // pin 5 controls mag
   
}


  void loop() 
{
   
      
  

  for(int x = 0; x < 24; ++x)   //dose kalk slurry for 24 hours, then a single dose of mag

{
  digitalWrite(4, LOW);     // turn kalk slurry dosing pump off
  digitalWrite(5, LOW);     // turn mag dosing pump off
  digitalWrite(3, HIGH);    // turn mixing pump on using a NO relay
  delay(secondsInMs(7));   // let mixing pump run for 7 seconds, or long enough to create kalk slurry    
  digitalWrite(3, LOW);     // turn mixing pump off
  delay(minutesInMs(2));    // wait 2 minutes before dosing kalk slurry to allow solid chunks to fall out of solution
  
  digitalWrite(4, HIGH);    // turn kalk slurry dosing pump on using a second NO relay      
  delay(secondsInMs(12));    // let kalk slurry dosing pump run only briefly to prevent a precipitation event
  digitalWrite(4, LOW);     // turn kalk slurry dosing pump off 
  delay(secondsInMs(20));   // leave kalk slurry dosing pump off briefly to allow slurry to dissipate
  
  digitalWrite(4, HIGH);    // same as above, this just spreads out the kalk slurry.      
  delay(secondsInMs(12));
  digitalWrite(4, LOW);   
  delay(minutesInMs(60)); 


}
 
              
  digitalWrite(3, LOW);     // turn mixing pump off
  digitalWrite(4, LOW);     // turn kalk slurry dosing pump off   
  digitalWrite(5, LOW);     // turn mag dosing pump on 
  delay(secondsInMs(5));    // dose mag long enough to dose 13 ml  
  digitalWrite(5, LOW);     // turn mag dosing pump off 
  delay(5);                 //short delay then back to top of sketch

 
}

saltyjoe:   digitalWrite(5, LOW);    // turn mag dosing pump on   delay(secondsInMs(5));    // dose mag long enough to dose 13 ml    digitalWrite(5, LOW);    // turn mag dosing pump off

Did you mean to put a HIGH in the first digitalWrite()?

Yes, it's LOW for now until I get that pump hooked up. I have it, but wanted to fix the problem with the sketch not running continuously first.

This sketch seems ok. (I mean, not how I would have written it - but it should work).

You are powering the nano with 12V :O ? It's not going to enjoy that much! To confirm that this might be an issue, check if the voltage regulator on the board is getting hot.

To fix - organize a 5v power supply. If you are far from a power point and want it to run for a while, maybe a 6v lantern battery might be the go. Or even just 4 C cells.

Hi,

 void setup()
{ 
  pinMode(3, OUTPUT);       // pin 3 controls mixing pump
  pinMode(4, OUTPUT);       // pin 4 controls kalk slurry
  pinMode(5, OUTPUT);       // pin 5 controls mag
}

To make your code more readable and help with all the comments, can I suggest you use variable names for your pins?

int mixingPump = 3;
int kalkSlurry = 4;
int magControl = 5;

void setup()
{
  pinMode(mixingPump, OUTPUT);      
  pinMode(kalkSlurry, OUTPUT);      
  pinMode(magControl, OUTPUT);      

}

Then in the rest of your code;

digitalWrite(kalkSlurry, LOW);
digitalWrite(magControl, LOW);
digitalWrite(mixingPump, HIGH);

Tom... :)

Thanks PaulMurray, I'll feed the NANO fewer volts. I appreciate you taking the time to look at my sketch-I know I'm not much at coding and wasn't sure if that was the problem.

Thank you as well TomGeorge, I'll rename the pin numbers to what their job is.

Much appreciated guys!

saltyjoe:
Thank you as well TomGeorge, I’ll rename the pin numbers to what their job is.

Since we are taking suggestions -

IMO: variables holding pin numbers should always be named somethingSomethingSomethingPin. This clearly distinguishes pin numbers from values read from (or being writte to) a pin.

I also like doing pin numbers as enums. Personal preference.

Oh, out of curiosity - as the regulator getting hot? Heck - does it even have a voltage regulator? It must do! Or connecting 12v would have smoked it instantly! Oh, I see: it’s that lump on the underside with three pins and a big earth connection.

Anyway. Also of interest: http://forum.arduino.cc/index.php?topic=26954.0

I wonder if you guys might take another look at this. The optocoupler/relay seems to work the opposite way it's supposed to. 

I'm using a brand new nano and 5V, 4 channel opto/relay. There is a 5V, 550mA power supply. I connected VCC from the opto and VIN from the nano to +5V. The ground from the NANO and opto are connected to -5V. 



[code 




  

  unsigned long hoursInMs(int h)  
{  
  return h*3600000UL;         //values will be expressed in hours 
 
}

  long minutesInMs(int m){ 
  return m*60000L;           //values will be expressed in minutes 
 
} 

  long secondsInMs(int s){ 
  return s*1000L;            //values will be expressed in seconds
} 
 
int mixingPump = 2;
int kalkSlurry = 3;
int magControl = 4;
int futurePump = 5; 
 
 void setup() 
{   
   
  pinMode(mixingPump, OUTPUT);      
  pinMode(kalkSlurry, OUTPUT);      
  pinMode(magControl, OUTPUT);         
  pinMode(futurePump, OUTPUT);  
  
}
  void loop() 
{
   
      
  

  for(int x = 0; x < 24; ++x)   //dose kalk slurry for 24 hours, then a single dose of mag

{
  digitalWrite(kalkSlurry, LOW);     // turn kalk slurry dosing pump off
  digitalWrite(magControl, LOW);     // turn mag dosing pump off
  digitalWrite(futurePump, LOW);     // turn future pump off
  digitalWrite(mixingPump, HIGH);    // turn mixing pump on using a NO relay
  delay(secondsInMs(7));                // let mixing pump run for 7 seconds, or long enough to create kalk slurry    
  digitalWrite(mixingPump, LOW);     // turn mixing pump off
  delay(minutesInMs(1));             // wait 1 minute before dosing kalk slurry to allow solid chunks to fall out of solution
  
  digitalWrite(kalkSlurry, HIGH);    // turn kalk slurry dosing pump on using a second NO relay      
  delay(secondsInMs(12));            // let kalk slurry dosing pump run only briefly to prevent a precipitation event
  digitalWrite(kalkSlurry, LOW);     // turn kalk slurry dosing pump off 
  delay(secondsInMs(20));            // leave kalk slurry dosing pump off briefly to allow slurry to dissipate
  
  digitalWrite(kalkSlurry, HIGH);    // same as above, this just spreads out the kalk slurry.      
  delay(secondsInMs(12));
  digitalWrite(kalkSlurry, LOW);   
  delay(minutesInMs(60)); 


}
 
   
  digitalWrite(magControl, HIGH);     // turn mag dosing pump on 
  delay(secondsInMs(9));             // dose mag long enough to dose 13 ml  
  digitalWrite(magControl, LOW);     // turn mag dosing pump off 
  delay(5);                          //short delay then back to top of sketch

 
}
  


]

saltyjoe: The optocoupler/relay seems to work the opposite way it's supposed to.

It's as if the sketch is running in reverse. Any pin assigned LOW energizes the optocoupler and closes the NO relay. Any pin assigned HIGH does not energize the optocoupler.

Reversing pin assignments might make things work, but seems like a flaky workaround.