OK, I've tried to put this together several different ways and I just can't get it to act the way I think it should.
I'm just trying to turn on a relay. When it's on, blink an LED and after it's been on for a period of time, turn it off.
So I have to assume that I am thinking the the If statement or the way I am writing my statements is just plain wrong. Also, I think my code is junk, I feel like I'm going around the barn twice to get in the door. There has to be a shorter way to do it.
in a nutshell. wait a while, turn the pump on for a short time, blink while the pump is on, turn off the pump and don't blink. It should be so easy!!!! So, please, where is my error? It just sits there with 13 on, doing nothing. I can get it to turn on and off if I just use blink without delay, it turns off and on just fine.
/* Program to turn on misting pump for plant propogation.
Turn on the pump relay for a few seconds every 8 to 10 minutes.
*/
const int ledPin = 13; // the number of the LED pin
const int pumpRelay = 10; // Number of the PumpRun Pin// Variables will change:
int ledState = LOW; // ledState used to set the LED
int pumpState = LOW;unsigned long blinkMillis = 0; // last time we blinked
unsigned long blinkInterval = 500; //Interval between blinks
unsigned long previousBlinkMillis = 0;
unsigned long previousPumpMillis =0;
unsigned long previousMillis =0;
unsigned long currentMillis =0;
unsigned long pumpRunMillis =0;
unsigned long pumpRunTime = 100; // interval to run the pump (10 sec currently)
unsigned long betweenPumpRun = 6000; // interval between pump running (10 min)void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pumpRelay, OUTPUT);
digitalWrite(ledPin, HIGH);
digitalWrite(pumpRelay, LOW);
}void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > betweenPumpRun) { //turn the pump on if enough time has passed
previousMillis = currentMillis; // remember when we last ran the pump
digitalWrite (pumpRelay, LOW);
pumpState = LOW;
}pumpRunMillis = millis();
if (pumpState == LOW) {
if (pumpRunMillis - previousPumpMillis > pumpRunTime){
previousPumpMillis = pumpRunMillis ;
digitalWrite (pumpRelay, HIGH);
pumpState = HIGH;}}blinkMillis = millis();
if(previousBlinkMillis - blinkMillis > blinkInterval) {
previousBlinkMillis = blinkMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}