I uploaded a pump controller code by Nick Gammon, but this doesn't work for me. I suspect I don't have the library as I have searched and looked up libraries/ and can only see Bounce in there.
Do I need to upload "avr/sleep.h" and put that in "Arduino/libraries?
Also I changed the last command (if that's what you call it) from "pumpTime *= 60000UL; // convert minutes to milliseconds" to "pumpTime *= 5000UL;" which I think is going to allow the timer to run for 5 seconds for testing.
Are pins 3 to 11 held HIGH or LOW or allowed to float, when not selected?
Any help greatly appreciated.
// Pump timer
// Author: Nick Gammon
// Date: 7th January 2012
// Released into the public domain.
#include <avr/sleep.h>
const int pumpPin = 13; // output pin for the pump and solenoid (goes to the relay)
const int buttonPin = 2; // input pin (for a pushbutton switch)
const int firstSwitch = 3; // first switch pin for time rotary button
const int lastSwitch = 11; // last switch pin
const int debounceTime = 20; // debounce in milliseconds
const unsigned long delayTime [] = { 2, 5, 10, 15, 20, 30, 45, 46, 120}; // Pump run times in minutes
unsigned long startPumpTime = 0; // when pump started
unsigned long pumpTime; // how long to run pump
volatile boolean buttonPressed; // set when button pressed
// interrupt service routine in sleep mode
void wake ()
{
sleep_disable (); // first thing after waking from sleep:
} // end of wake
// interrupt service routine when awake and button pressed
void buttonDown ()
{
buttonPressed = true;
} // end of buttonDown
void sleepNow ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable (); // enables the sleep bit in the mcucr register
attachInterrupt (0, wake, LOW);
sleep_mode (); // here the device is actually put to sleep!!
detachInterrupt (0); // stop LOW interrupt
} // end of sleepNow
void deBounce ()
{
unsigned long now = millis ();
do
{
// on bounce, reset time-out
if (digitalRead (buttonPin) == LOW)
now = millis ();
}
while (digitalRead (buttonPin) == LOW ||
(millis () - now) <= debounceTime);
} // end of deBounce
void setup()
{
pinMode(pumpPin, OUTPUT);
digitalWrite (buttonPin, HIGH); // pull-up on button
for (int i = firstSwitch; i <= lastSwitch; i++)
digitalWrite (i, HIGH); // pull-up on switch
} // end of setup
void loop ()
{
// if pump is running, see if time to turn it off
if (digitalRead (pumpPin) == HIGH)
{
if ((millis () - startPumpTime) >= pumpTime || buttonPressed)
{
digitalWrite (pumpPin, LOW);
deBounce ();
buttonPressed = false;
}
return; // not time to sleep yet
} // end of pump running
// ------ here if pump not running -----
// pump not running? sleep then
sleepNow ();
// pump is not running and we woke up, work out how long to run it
deBounce ();
pumpTime = 0;
EIFR = 1; // cancel any existing falling interrupt (interrupt 0)
attachInterrupt (0, buttonDown, FALLING); // ready for button press
for (int i = firstSwitch; i <= lastSwitch; i++)
if (digitalRead (i) == LOW)
pumpTime = delayTime [i - firstSwitch];
if (pumpTime == 0)
return; // no time selected
// start pump
startPumpTime = millis ();
digitalWrite (pumpPin, HIGH);
pumpTime *= 6000UL; // convert minutes to milliseconds
// pumpTime = 5000; // FOR TESTING - 5 seconds
} // end of loop