# define BUZY A0

void myDelay(unsigned long tt)
{
//  delay(tt / 10);  // life too short, run at 10X
  delay(tt);  // normal time
}

# define delay myDelay


// Define button pins
const
int button1Pin =
    9;
		// 5;
const
int button2Pin =
    8;
		// 6:
const
int button3Pin =
    7;
const
int button4Pin =
    8;
const
int button5Pin =
    9;

// Define relay pins
const
int relay1Pin =
    2;
const
int relay2Pin =
    3;
const
int relay3Pin =
    4;

// Define pump run times (in milliseconds)
const
int pump1Time =
    1000;
// 1 second
const
int pump2Time =
    2000;
		// 9000;
// 9 seconds
const
int pump3Time =
    3000;
		// 1500;
// 1.5 seconds
const
int pump4Time =
    4000;
		// 8000;
// 8 seconds
const
int pump5Time =
    5000;
// 3 seconds
const
int pump6Time =
    6000;
		// 18000;
// 18 seconds

// Delay after the second pump finishes (in milliseconds)
const
int delayAfterSecondPump =
    1000;
// 3000;
// 3 seconds

void
setup()
{
// Initialize buttons and relays

	pinMode(button1Pin,
	        INPUT_PULLUP);

	pinMode(button2Pin,
	        INPUT_PULLUP);

	pinMode(button3Pin,
	        INPUT_PULLUP);

	pinMode(button4Pin,
	        INPUT_PULLUP);

	pinMode(button5Pin,
	        INPUT_PULLUP);

	pinMode(relay1Pin,
	        OUTPUT);

	pinMode(relay2Pin,
	        OUTPUT);

	pinMode(relay3Pin,
	        OUTPUT);

// Initially, turn off all relays

	digitalWrite(relay1Pin,
	             LOW);

	digitalWrite(relay2Pin,
	             LOW);

	digitalWrite(relay3Pin,
	             LOW);

  pinMode(BUZY, OUTPUT);
}

void
loop()
{

// beating heart shows buttons are being watched

digitalWrite(BUZY,(millis() >> 8) & 0x1);

// Check if buttons are pressed

	if
	(digitalRead(button1Pin)
	        == LOW)
	{

		runDispenser(1);

	}

	if
	(digitalRead(button2Pin)
	        == LOW)
	{

		runDispenser(2);

	}

	if
	(digitalRead(button3Pin)
	        == LOW)
	{

		runDispenser(3);

	}

	if
	(digitalRead(button4Pin)
	        == LOW)
	{

		runDispenser(4);

	}

	if
	(digitalRead(button5Pin)
	        == LOW)
	{

		runDispenser(5);

	}
}

void
runDispenser(int
             dispenserNumber)
{

	switch
	(dispenserNumber)
	{

	case
			1:

		activateRelay(relay1Pin,
		              pump1Time);

		activateRelay(relay2Pin,
		              pump2Time);

		break;

	case
			2:

		activateRelay(relay3Pin,
		              pump3Time);

		activateRelay(relay2Pin,
		              pump4Time);

		delay(delayAfterSecondPump);
// Wait for a delay after the second pump finishes

		break;

	case
			3:

		activateRelay(relay1Pin,
		              pump3Time);

		activateRelay(relay2Pin,
		              pump4Time);

		break;

	case
			4:

		activateRelay(relay3Pin,
		              pump3Time);

		activateRelay(relay2Pin,
		              pump4Time);

		delay(delayAfterSecondPump);
// Wait for a delay after the second pump finishes

		break;

	case
			5:

		activateRelay(relay1Pin,
		              pump5Time);

		activateRelay(relay2Pin,
		              pump6Time);

		break;

	}
}

void
activateRelay(int
              relayPin,
              int
              runTime)
{

	digitalWrite(relayPin,
	             HIGH);
// Turn on the relay

	delay(runTime);
// Run the pump for the specified time

	digitalWrite(relayPin,
	             LOW);
// Turn off the relay
}