Help with a Arduino Project Please!!!!!!

Hi guys. I need your help with something. I’m a complete Newbie so please bear with me!

This is the scenario:

Amongst other non related hardware I have an 8ch relay module that I’ll be connecting to my Mega2560

To this is will connect 4 water pumps.

WP1

WP2

WP3

WP4

I want to run them in the following combinations, but it must select the flowDirection randomly and run it for 20mins

WP1 & WP4 = (flowDirection 1)

WP2 & WP3 = (flowDirection 2)

WP1 & WP2 = (flowDirection 3)

WP3 & WP4 = (flowDirection 4)

WP1 & WP3 = (flowDirection 5)

WP1 & WP3 = (flowDirection 6)

WP2 & WP4 = (flowDirection 7)

Then all the pumps must be off for 2 mins for the water settle/rest.

This will not be the only function I need the mega2560 to do so I don’t want the code to stop for 20mins every time this function starts!

Is this possible.

This is the code that I’ll also be running to run 4 channels LED that uses PWM for dimming.

// include the libraries:

#include <LiquidCrystal.h>

#include <Wire.h>

#include <Button.h>

#include <EEPROM.h>

#include <EEPROMVar.h>

/**** Define Variables & Constants ****/

/**************************************/

// set the RTC’s I2C address

#define DS1307_I2C_ADDRESS 0x68

// create the LCD

LiquidCrystal lcd(8, 7, 5, 4, 16, 2);

// set up backlight

int bkl = 6; // backlight pin

byte bklIdle = 10; // PWM value for backlight at idle

byte bklOn = 70; // PWM value for backlight when on

int bklDelay = 10000; // ms for the backlight to idle before turning off

unsigned long bklTime = 0; // counter since backlight turned on

// create the menu counter

int menuCount = 1;

int menuSelect = 0;

//create manual override variables

boolean override = false;

byte overmenu = 0;

int overpercent = 0;

// create the buttons

Button menu = Button(12,PULLDOWN);

Button select = Button(13,PULLDOWN);

Button plus = Button(14,PULLDOWN);

Button minus = Button(15,PULLDOWN);

// LED variables. These control the behavior of lighting. Change these to customize behavoir

int minCounter = 0; // counter that resets at midnight.

int oldMinCounter = 0; // counter that resets at midnight.

int oneLed = 9; // pin for channel 1

int twoLed = 10; // pin for channel 2

int threeLed = 11; // pin for channel 3

int fourLed = 3; // pin for channel 4

int oneVal = 0; // current value for channel 1

int twoVal = 0; // current value for channel 2

int threeVal = 0; // current value for channel 3

int fourVal = 0; // current value for channel 4

// Variables making use of EEPROM memory:

EEPROMVar oneStartMins = 750; // minute to start this channel.

EEPROMVar onePhotoPeriod = 720; // photoperiod in minutes for this channel.

EEPROMVar oneMax = 100; // max intensity for this channel, as a percentage

EEPROMVar oneFadeDuration = 60; // duration of the fade on and off for sunrise and sunset for

// this channel.

EEPROMVar twoStartMins = 810;

EEPROMVar twoPhotoPeriod = 600;

EEPROMVar twoMax = 100;

EEPROMVar twoFadeDuration = 60;

EEPROMVar threeStartMins = 810;

EEPROMVar threePhotoPeriod = 600;

EEPROMVar threeMax = 100;

EEPROMVar threeFadeDuration = 60;

EEPROMVar fourStartMins = 480;

EEPROMVar fourPhotoPeriod = 510;

EEPROMVar fourMax = 100;

EEPROMVar fourFadeDuration = 60;

/*

int oneStartMins = 1380; // minute to start this channel.

int onePhotoPeriod = 120; // photoperiod in minutes for this channel.

int oneMax = 100; // max intensity for this channel, as a percentage

int oneFadeDuration = 60; // duration of the fade on and off for sunrise and sunset for

// this channel.

int twoStartMins = 800;

int twoPhotoPeriod = 60;

int twoMax = 100;

int twoFadeDuration = 15;

int threeStartMins = 800;

int threePhotoPeriod = 60;

int threeMax = 100;

int threeFadeDuration = 30;

int fourStartMins = 800;

int fourPhotoPeriod = 120;

int fourMax = 100;

int fourFadeDuration = 60;

*/

/****** RTC Functions ******/

/***************************/

// Convert decimal numbers to binary coded decimal

byte decToBcd(byte val)

{

return ( (val/10*16) + (val%10) );

}

// Convert binary coded decimal to decimal numbers

byte bcdToDec(byte val)

{

return ( (val/16*10) + (val%16) );

}

// Sets date and time, starts the clock

void setDate(byte second, // 0-59

byte minute, // 0-59

byte hour, // 1-23

byte dayOfWeek, // 1-7

byte dayOfMonth, // 1-31

byte month, // 1-12

byte year) // 0-99

{

Wire.beginTransmission(DS1307_I2C_ADDRESS);

Wire.send(0);

Wire.send(decToBcd(second));

Wire.send(decToBcd(minute));

Wire.send(decToBcd(hour));

Wire.send(decToBcd(dayOfWeek));

Wire.send(decToBcd(dayOfMonth));

Wire.send(decToBcd(month));

Wire.send(decToBcd(year));

Wire.endTransmission();

}

// Gets the date and time

void getDate(byte *second,

byte *minute,

byte *hour,

byte *dayOfWeek,

byte *dayOfMonth,

byte *month,

byte *year)

{

Wire.beginTransmission(DS1307_I2C_ADDRESS);

Wire.send(0);

Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

*second = bcdToDec(Wire.receive() & 0x7f);

*minute = bcdToDec(Wire.receive());

*hour = bcdToDec(Wire.receive() & 0x3f);

*dayOfWeek = bcdToDec(Wire.receive());

*dayOfMonth = bcdToDec(Wire.receive());

*month = bcdToDec(Wire.receive());

*year = bcdToDec(Wire.receive());

}

/****** LED Functions ******/

/***************************/

//function to set LED brightness according to time of day

//function has three equal phases - ramp up, hold, and ramp down

int setLed(int mins, // current time in minutes

int ledPin, // pin for this channel of LEDs

int start, // start time for this channel of LEDs

int period, // photoperiod for this channel of LEDs

int fade, // fade duration for this channel of LEDs

int ledMax // max value for this channel

) {

int val = 0;

//fade up

if (mins > start || mins <= start + fade) {

val = map(mins - start, 0, fade, 0, ledMax);

}

//fade down

if (mins > start + period - fade && mins <= start + period) {

val = map(mins - (start + period - fade), 0, fade, ledMax, 0);

}

//off or post-midnight run.

if (mins <= start || mins > start + period) {

if((start+period)%1440 < start && (start + period)%1440 > mins )

{

val=map((start+period-mins)%1440,0,fade,0,ledMax);

}

else

val = 0;

}

if (val > ledMax) {val = ledMax;}

if (val < 0) {val = 0; }

analogWrite(ledPin, map(val, 0, 100, 0, 255));

if(override){val=overpercent;}

return val;

}

/**** Display Functions ****/

/***************************/

// format a number of minutes into a readable time (24 hr format)

void printMins(int mins, //time in minutes to print

boolean ampm //print am/pm?

) {

int hr = (mins%1440)/60;

int mn = mins%60;

if(hr<10){

lcd.print(" ");

}

lcd.print(hr);

lcd.print(“:”);

if(mn<10){

lcd.print(“0”);

}

lcd.print(mn);

}

// format hours, mins, secs into a readable time (24 hr format)

void printHMS (byte hr,

byte mn,

byte sec //time to print

)

{

if(hr<10){

lcd.print(" ");

}

lcd.print(hr, DEC);

lcd.print(“:”);

if(mn<10){

lcd.print(“0”);

}

lcd.print(mn, DEC);

lcd.print(“:”);

if(sec<10){

lcd.print(“0”);

}

lcd.print(sec, DEC);

}

void ovrSetAll(int pct){

analogWrite(oneLed,map(pct,0,100,0,255));

analogWrite(twoLed,map(pct,0,100,0,255));

analogWrite(threeLed,map(pct,0,100,0,255));

analogWrite(fourLed,map(pct,0,100,0,255));

}

/**** Setup ****/

/***************/

void setup() {

Wire.begin();

pinMode(bkl, OUTPUT);

lcd.begin(16, 2);

digitalWrite(bkl, HIGH);

lcd.print(“Typhon-Reef”);

lcd.setCursor(0,1);

lcd.print(“”);

delay(5000);

lcd.clear();

analogWrite(bkl,bklIdle);

}

/***** Loop *****/

/****************/

void loop() {

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

getDate(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

oldMinCounter = minCounter;

minCounter = hour * 60 + minute;

//check & set fade durations

if(oneFadeDuration > onePhotoPeriod/2 && onePhotoPeriod >0){oneFadeDuration = onePhotoPeriod/2;}

if(oneFadeDuration<1){oneFadeDuration=1;}

if(twoFadeDuration > twoPhotoPeriod/2 && twoPhotoPeriod >0){twoFadeDuration = twoPhotoPeriod/2;}

if(twoFadeDuration<1){twoFadeDuration=1;}

if(threeFadeDuration > threePhotoPeriod/2 && threePhotoPeriod >0){threeFadeDuration = threePhotoPeriod/2;}

if(threeFadeDuration<1){threeFadeDuration=1;}

if(fourFadeDuration > fourPhotoPeriod/2 && fourPhotoPeriod > 0){fourFadeDuration = fourPhotoPeriod/2;}

if(fourFadeDuration<1){fourFadeDuration=1;}

//check & set any time functions

//set outputs

if(!override){

oneVal = setLed(minCounter, oneLed, oneStartMins, onePhotoPeriod, oneFadeDuration, oneMax);

twoVal = setLed(minCounter, twoLed, twoStartMins, twoPhotoPeriod, twoFadeDuration, twoMax);

threeVal = setLed(minCounter, threeLed, threeStartMins, threePhotoPeriod, threeFadeDuration, threeMax);

fourVal = setLed(minCounter, fourLed, fourStartMins, fourPhotoPeriod, fourFadeDuration, fourMax);

}

else{

ovrSetAll(overpercent);

}

First, using the Code tags will help others read your code.

Second, does the timing have some desired resolution, 1 sec or 0.1 sec ?

Third, why not run your loop() at the resolution and timestamp the start time of the pump. Then ask something like;

loop(){
  get_time();
  if(some_condition && !inhibit_pump_change){
    random_pump();
    turn_pump_on();
    starTime = currenTime;
    inhibit_pump_change = true;
  }
  if(currenTime - starTime >= runTime){
    turn_pump_off();
    if(currenTime - starTime >= runTime + settleTime){
      inhibit_pump_change = false;
    }
  }
  //more code here

http://arduino.cc/en/Reference/Random

(click on to open)

Hi,

Thanks for the quick response to my question. Ill try this as soon as I get a free moment from work. I’ll keep you guys informed!