I am trying to write a code that will control a light and a pump and have a start and stop button for the pump. I want the light to come on when the Arduino is powered up. but shut off after 15 min unless the pump is turned on or off then I want the timer reset. I also want the pump to shut off if it is on for 15 minutes.
//CONSTANTS
const int PUMP_START = 3;
const int PUMP_STOP = 4;
const int LIGHT = 13;
const int PUMP = 12;
//VARIABLES
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
bool relayOn = false;
bool LightOn = false;
//MILLIS
unsigned long previousMillis = 0;
const unsigned long interval = 1000;
const unsigned long PumpInterval = 2000;
const unsigned long LightInterval = 2000;
void setup()
{
pinMode(PUMP_START, INPUT_PULLUP);
pinMode(PUMP_STOP, INPUT_PULLUP);
digitalWrite(LIGHT, HIGH);
Serial.begin(9600);
pinMode(LIGHT, OUTPUT);
pinMode(PUMP, OUTPUT);
}
void loop() {
buttonState = digitalRead(PUMP_START);
unsigned long currentMillis = millis();
if ( buttonState == LOW )
{
previousMillis = currentMillis;
digitalWrite(PUMP, HIGH);
relayOn = true;
}
// if relay is currently on...
if ( relayOn )
{
// turn pump on, if close to turning off the relay
if (currentMillis - previousMillis >= interval - PumpInterval )
digitalWrite(PUMP, (millis() / 300) % 2);
// if enough time has elapsed, turn off the relay
if (currentMillis - previousMillis >= interval)
{
// .. turn off relay
digitalWrite(PUMP, LOW);
relayOn = false;
}
buttonState = digitalRead(PUMP_STOP);
if (buttonState == LOW)
digitalWrite (PUMP, LOW);
{
previousMillis = currentMillis;
digitalWrite(LIGHT, HIGH);
LightOn = true;
}
// if enough time has elapsed, turn off the Light
if (currentMillis - previousMillis >= interval)
{
// .. turn off light
digitalWrite(LIGHT, LOW);
relayOn = false;
}
}
}
I can turn the pump on and off with the switches. The light comes on when powered up. They just don’t time out. They should go off after 2 seconds in that program
Andrew530:
They should go off after 2 seconds in that program
I believe you have a misplaced curly brace in the section which controls the light. Pretend you're the processor and step through the code from [color=blue]buttonState = digitalRead(PUMP_STOP);[/color] down.
I now have this the problem is it turns them one independently but turns them off at the same time
<const int buttonPin = 2; // the number of the pushbutton pin
const int StartPin = 3;
const int StopPin = 4;
const int LightPin = 12; // the number of the LED pin and SSR
const int PumpPin = 11;
int buttonState = 0;
int StartState = 0;
unsigned long time = 0;
void setup() {
pinMode(LightPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP); //n.o. button from pin 2 to ground
digitalWrite(LightPin, HIGH); //connect SSR dc input to pin 13 output and ground
pinMode(PumpPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(StartPin, INPUT_PULLUP); //n.o. button from pin 2 to ground
digitalWrite(PumpPin, LOW); //connect SSR dc input to pin 13 output and ground
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(LightPin, HIGH);
time = millis();
}
else {
if ((millis() - time) > 3600) { // 3600000 milliseconds is 1 hour
digitalWrite(LightPin, LOW);
StartState = digitalRead(StartPin);
if (StartState == LOW) {
digitalWrite(PumpPin, HIGH);
time = millis();
}
else {
if ((millis() - time) > 3600) { // 3600000 milliseconds is 1 hour
digitalWrite(PumpPin, LOW);
}
}
}
}
}>