-1

I have a functional sketch that I run on an Arduino Uno, the output controlling a fan module; I need another timer to run a 2nd fan and I would like to use another output on the Uno to do that rather than using another unit.

What my sketch does is as follows:

  • counts 15 minutes (interval_1)
  • starts the fan for 1 minute (interval_2)
  • starts another cycle.

What I need to do is to add another function that will start fan#2 for 15 sec (interval_3) when the cycle starts, using another output (relayPin_1), then stop.

So the flow will be:

  • counts 15 minutes (interval_1)
  • starts fan#1 for 60 sec (interval_2)
  • starts fan#2 for 15 sec (interval_3)
  • resets and starts another cycle.

Unfortunately, can't figure out how to insert the 2nd timer.

Sketch is below, if you have any ideas that would be much appreciated.

Thanks

const byte relayPin = 4;                           // fan#1 control on pin 4, 
const byte relayPin_1 = 7;                         // humidifier fan#2 control on pin 7,
const unsigned long interval_1 = 1000UL * 60,      // RUN timer, msec
                    interval_2 = 1000UL * 60 * 15, // OFF timer, msec
                    interval_3 = 1000UL * 15;      // Humidifier timer
unsigned long currentMillis, previousMillis = 0;

void setup() {
    // put your setup code here, to run once:
    pinMode(relayPin, OUTPUT);   // Output#1
    pinMode(relayPin_1, OUTPUT); //Output#2
    // relayPin_1 controls the humidifier's fan#2
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    // at startup, fan is ON for interval_1 duration
    currentMillis = millis() - previousMillis;
    if (currentMillis <= interval_1) {
        digitalWrite(relayPin, HIGH);    // fan ON
        digitalWrite(LED_BUILTIN, HIGH); // LED ON (blinking)
        delay(333 / 2);
        digitalWrite(LED_BUILTIN, LOW);   // LED Off
        delay(999);
    }

    // when interval_1 is reached, turn fan OFF for the duration of interval_2
    else if (currentMillis <= (interval_1 + interval_2)) {
        digitalWrite(relayPin, LOW);    // fan OFF
        digitalWrite(LED_BUILTIN, LOW); // led OFF
    }
    else {
        // reset the timer
        previousMillis = millis();
    }

}

Okay... I had time to play with it today; what it does: it starts both fan#1 and fan#2, then stops fan#2 at (interval_1 + interval_2), but fan#1 continues running until the cycle ends. Therefore: fan#1 runs for 35 sec, fan#2 runs for 15 sec, as it should; and the cycle is stopped for 30 sec as it should. However, I can't make fan#1 to run for only 20 sec. as it is defined.


const byte relayPin = 4;   // recyrculation fan#1 control on pin 4, 
const byte relayPin_1 = 7; // humidifier fan#2 control on pin 7,
const unsigned long interval_1 = 1000UL * 20,    // RUN timer fan#1, msec
                    interval_2 = 1000UL * 15,    // RUN timer fan#2, msec
                    interval_3 = 1000UL * 30;    // OFF timer
unsigned long currentMillis, previousMillis = 0;

void setup() {
   // put your setup code here, to run once:
   pinMode(relayPin, OUTPUT); // Output#1
    pinMode(relayPin_1, OUTPUT); //Output#2
     pinMode(LED_BUILTIN, OUTPUT); }

void loop() {
// at startup, fan#1 & fan#2 are ON 
 currentMillis = millis() - previousMillis;
  if (currentMillis < interval_1) {
    digitalWrite(relayPin, HIGH);   // fan#1 ON
    digitalWrite(relayPin_1, HIGH); // fan#2 ON
     digitalWrite(LED_BUILTIN, HIGH); // builtin LED ON

 } else if (currentMillis < (interval_1 + interval_2)) {
    digitalWrite(relayPin_1, LOW); // fan#2 OFF

   // when (interval_1 + interval_2) is reached, turn fans OFF for the duration of interval_3
 } else if (currentMillis < (interval_1 + interval_2 + interval_3)) {
   digitalWrite(relayPin, LOW);      // fan#1 OFF
    digitalWrite(LED_BUILTIN, LOW); // led OFF

 } else {
   // reset the timer  
     previousMillis = millis(); }
 }
  • your description of the fan cycles is unclear .... do both fans start at the same time? – jsotola Nov 28 '19 at 2:20
  • draw a timing chart on paper ... it nay help you to visualize the required code – jsotola Nov 28 '19 at 2:27
  • Your code doesn't match your description. In your description you want the fan off for 15 minutes, then on for 1 minute. You have your fan on for the entire 15 minute interval_1, then off for 1 minute. – Duncan C Nov 28 '19 at 3:05
  • The code comments on your intervals suggest that interval_1 is your run (fan 1 on) time and interval_2 is your fan off time. That is what your code does, but not what you say in your description. Which is correct? – Duncan C Nov 28 '19 at 3:15
  • 1
    I'm not clear on what you want it to do. Please add a clear statement of your goal. For the first interval (20 seconds?) which fans should be on? Then for the 2nd, 15 second interval which fans should be on? And I gather that you want both fans off for interval 3 (30 seconds)? – Duncan C Dec 2 '19 at 0:49
1

Assuming you get your logic straightened out, you should be able to add as many cases to your if statement as you want:

  if (currentMillis <= interval_1) {
      // Before end of interval 1, fan off.
  } else if (currentMillis <= (interval_1 + interval_2)) {
      // In interval 2, fan on
  } else if (currentMillis <= (interval_1 + interval_2 + interval_3 )) {
      // In interval 3, fan2 on
  } else {
      // Turn everything off
      // reset the timer  
  }
| improve this answer | |
  • In any case, I showed in my answer how to adjust your code to have 3 different intervals, then turn everything off after the end of the last interval. You can extend that pattern to as many intervals as you want, – Duncan C Nov 28 '19 at 16:47
  • If you can't get it to work edit your question and put the new code at the end, along with what it is doing wrong. – Duncan C Nov 28 '19 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.