Go Down

Topic: Need help to clean up codes (Read 473 times) previous topic - next topic

bezz

I have no experience or programming knowledge and after researching for 2 weeks, I came up with the attached codes for switching 3 relays on/off at intervals. I'm not sure if the codes work properly as I can't monitor it for 1 full cycle, so I hope the experts here can critique the codes.

Thanks

Code: [Select]
#include <Time.h>
#include <TimeAlarms.h>
int Pin1 = 7;   
int Pin2 = 6; 
int Pin3 = 5;   


void setup()
{
  Serial.begin(9600);

  pinMode(Pin1, OUTPUT); 
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);

setTime(17,59,45,3,1,12);

  Alarm.alarmRepeat(18,00,00, Pump1); 
  Alarm.alarmRepeat(19,00,00, Pump2);
Alarm.alarmRepeat(20,00,00, Pump3);
  Alarm.alarmRepeat(21,00,00, Pump4);
Alarm.alarmRepeat(22,00,00, Pump5); 
Alarm.alarmRepeat(23,00,00, Pump6); 
  Alarm.alarmRepeat(0,00,00, Pump7);
  Alarm.alarmRepeat(1,00,00, Pump8); 
Alarm.alarmRepeat(2,00,00, Pump9); 
  Alarm.alarmRepeat(3,00,0, Pump10);
  Alarm.alarmRepeat(4,00,00, Pump11); 
Alarm.alarmRepeat(5,00,00, Pump12);
  Alarm.alarmRepeat(6,00,00, Pump13);
Alarm.alarmRepeat(7,00,00, Pump14); 
Alarm.alarmRepeat(8,00,00, Pump15);
  Alarm.alarmRepeat(9,00,00, Pump16);
  Alarm.alarmRepeat(10,00,00, Pump17);
Alarm.alarmRepeat(11,00,00, Pump18);
  Alarm.alarmRepeat(12,00,00, Pump19);
  Alarm.alarmRepeat(13,00,00, Pump20);
Alarm.alarmRepeat(14,00,00, Pump21);
  Alarm.alarmRepeat(15,00,00, Pump22);
  Alarm.alarmRepeat(16,00,00, Pump23);
Alarm.alarmRepeat(17,00,00, Pump24);


}

void  loop()

   Alarm.delay(1000);
}


void Pump1()
{
  digitalWrite(Pin1, HIGH);   
  delay(40000);                   
  digitalWrite(Pin1, LOW);     
  delay(50);                     
}

void Pump2()
{
  digitalWrite(Pin3, HIGH);   
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(50);                     
}

void Pump3()
{
  digitalWrite(Pin2, HIGH);   
  delay(28000);                   
  digitalWrite(Pin2, LOW);     
  delay(100);                     
}

void Pump4()
{
  digitalWrite(Pin1, HIGH);   
  delay(33000);                   
  digitalWrite(Pin1, LOW);     
  delay(100);                     
}

void Pump5()
{
  digitalWrite(Pin3, HIGH);   
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(100);                   
}

void Pump6()
{
  digitalWrite(Pin2, HIGH);   
  delay(25000);                   
  digitalWrite(Pin2, LOW);     
  delay(100);                     
}

void Pump7()
{
  digitalWrite(Pin1, HIGH);   
  delay(40000);                 
  digitalWrite(Pin1, LOW);     
  delay(100);                   
}

void Pump8()
{
  digitalWrite(Pin3, HIGH); 
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(100);                   
}

void Pump9()
{
  digitalWrite(Pin2, HIGH);   
  delay(28000);                   
  digitalWrite(Pin2, LOW);     
  delay(100);                   
}

void Pump10()
{
  digitalWrite(Pin1, HIGH);   
  delay(33000);                   
  digitalWrite(Pin1, LOW);     
  delay(100);                   
}

void Pump11()
{
  digitalWrite(Pin3, HIGH);   
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(100);                     
}

void Pump12()
{
  digitalWrite(Pin2, HIGH);   
  delay(28000);                   
  digitalWrite(Pin2, LOW);     
  delay(100);                     
}

void Pump13()
{
  digitalWrite(Pin1, HIGH);   
  delay(40000);                   
  digitalWrite(Pin1, LOW);     
  delay(100);                   
}

void Pump14()
{
  digitalWrite(Pin3, HIGH);   
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(100);                   
}

void Pump15()
{
  digitalWrite(Pin2, HIGH);   
  delay(25000);                 
  digitalWrite(Pin2, LOW);     
  delay(100);                   
}

void Pump16()
{
  digitalWrite(Pin1, HIGH); 
  delay(33000);                 
  digitalWrite(Pin1, LOW);     
  delay(100);                     
}

void Pump17()
{
  digitalWrite(Pin3, HIGH); 
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(100);                   
}

void Pump18()
{
  digitalWrite(Pin2, HIGH);   
  delay(28000);                   
  digitalWrite(Pin2, LOW);     
  delay(100);                     
}

void Pump19()
{
  digitalWrite(Pin1, HIGH);   
  delay(40000);                 
  digitalWrite(Pin1, LOW);     
  delay(100);                   
}

void Pump20()
{
  digitalWrite(Pin3, HIGH);   
  delay(15000);                   
  digitalWrite(Pin3, LOW);     
  delay(100);                     
}

void Pump21()
{
  digitalWrite(Pin2, HIGH);   
  delay(25000);                 
  digitalWrite(Pin2, LOW);     
  delay(100);                     
}

void Pump22()
{
  digitalWrite(Pin1, HIGH); 
  delay(33000);                 
  digitalWrite(Pin1, LOW);     
  delay(100);                     
}

void Pump23()
{
  digitalWrite(Pin3, HIGH);   
  delay(15000);                 
  digitalWrite(Pin3, LOW);   
  delay(100);                     
}

void Pump24()
{
  digitalWrite(Pin2, HIGH);   
  delay(28000);                   
  digitalWrite(Pin2, LOW);     
  delay(100);                   
}


pYro_65

These links may be helpful.
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/control/ -> Read iteration structures ( loops )
http://www.cplusplus.com/doc/tutorial/arrays/

You can make good use of arrays and loops, especially for the alarmRepeat function calls.



lloyddean

Confusing!

You have 3 relays connected to pins 5, 6 and 7 each connecting to a pump?

Why the subroutines with the confusing sequential names 'pump'[1-23].

bezz

#3
Jan 16, 2013, 04:44 am Last Edit: Jan 16, 2013, 04:53 am by bezz Reason: 1

Confusing!

You have 3 relays connected to pins 5, 6 and 7 each connecting to a pump?

Why the subroutines with the confusing sequential names 'pump'[1-23].



Sorry, I'm new to this..Yes, I have 3 pumps, connected to 3 relays with pin 5, 6 & 7 controlling each relay. I want to switch on the relays for a few seconds every couple hours or so.

After reading further, I came up with this:

Code: [Select]

#include "Timer.h"

Timer t;

int p1;
int p2;
int p3;

void setup()
{
 pinMode(7, OUTPUT);
 p1 = t.every(3600000, event1);

 pinMode(6, OUTPUT);
 p2 = t.every(3600000, event2);

 pinMode(5, OUTPUT);
 p3 = t.every(3600000, event3);

}

void loop()
{
 t.update();
}

void event1()
{
   t.oscillate(7, 10000, HIGH);
}

void event2()
{
   t.oscillate(6, 10000, HIGH);
}

void event3()
{
   t.oscillate(5, 10000, HIGH);
}


Thanks

lloyddean

Can you share "Timer.h" and any associated .cpp files

AMPS-N

1)Please let me know your pumps are rated @.
2) what is time interval you on turn on and off. Before you start program Start with truth table taking 3 inputs  with state  of pumps re fence for output.
3) please mention type of relay your using with rating. like SPDT/SPST/DPDT

SO above point can help us to guide
AMPS

michinyon

All those huge delays will cause the whole process to stop,  until each delay is finished.

what you need to do, in each of those pump routines,   is turn the pump on,  calculate when you want to turn it off again,
and then either set an interrupt timer to remind you when to turn it off,    or keep some sort of list of the turn-off times
which you check frequently to see if there are any pumps which need to be turned off.

bezz

#7
Jan 16, 2013, 09:25 am Last Edit: Jan 16, 2013, 09:28 am by bezz Reason: 1

Can you share "Timer.h" and any associated .cpp files


The files can be downloaded here:  https://sites.google.com/site/programmingarduinobook/downloads/Timer.zip

Quote from: AMPS-N

1)Please let me know your pumps are rated @.
2) what is time interval you on turn on and off. Before you start program Start with truth table taking 3 inputs  with state  of pumps re fence for output.
3) please mention type of relay your using with rating. like SPDT/SPST/DPDT


1) Pumps are 6V and run on a separate power supply controlled by a relay
2) I need to pumps to turn on and off at 1hr-3hr intervals individually/separately and stay on for 10 - 50 secs. After that, they will be off until the next cycle (1 - 3hrs later)
3) I am using this relay, it runs on a separate power supply, I just need the 5V signal from the pin to activate the relay:


Thanks for all the help guys. Sorry if my answers are incorrect as I may not fully understand your questions.

Thanks again for helping  :)

UKHeliBob

My advice would be to go back to using Timealarms.  Write a function to turn on each pump and a function to turn off each pump (6 functions).  Set alarms to call the functions when you want the pumps to turn on and off.  Do not use the delay() function anywhere in the program or it will stall waiting for the delay to complete, but you still need to use Alarm.delay in loop() to make the alarms work.  If you need to be checking other things such as button presses to manually control the pumps you can call Alarm.delay() very frequently.

NOTE - you could get away with a single function to turn a specific pump on or off but get the simple version working first.  Using Timealarms you can test the whole cycle by temporarily changing the alarm times.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up