Turning on a fan using millis() function only

Can someone help me up with a code; I am trying to run a fan for 10 seconds after every 10 minutes without using delay function as it put the whole code on hold and have failed to come up with a logic using only millis() function

Hello osamaaftab2

Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.

What have you tried so far?

Have you studied this famous example?
https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay

For further study:

https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html

This works on Nano, maybe too simple?

unsigned long timer,
              cycleTime = 600000; // ten minutes
const int onTime = 10000; // ten seconds
const byte fanPin = 2;

void setup()
{
  //Serial.begin(9600);
  pinMode(fanPin,OUTPUT);
}

void loop()
{
  if(millis() - timer >= cycleTime)
  {
    timer += cycleTime; // add cycleTime to timer for next event
  }
  digitalWrite(fanPin,millis() - timer < onTime); // ON for first 10 seconds of cycle
}

NOTE You cannot run a fan directly from an Arduino output pin, you need a relay or transistor to handle the motor current.

Try the following sketch -- tested using L (On for 1-sec and OFF for 2-sec) using millis() function.

unsigned long prMillis = 0;
byte ledState = HIGH;
bool flag = false;


void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, ledState);   //LED is ON
  flag = true;
  prMillis = millis();
}

void loop()
{
  if (flag == true)
  {
    if (millis() - prMillis >= 1000)//1-sec is over
    {
      ledState = !ledState;
      digitalWrite(13, ledState); //LED is OFF
      flag = false;
      prMillis = millis();
    }
  }
  else
  {
    if (millis() - prMillis >= 2000)//2-sec is over
    {
      ledState = !ledState;
      digitalWrite(13, ledState); //LED is OFF
      flag = true;
      prMillis = millis();
    }
  }
}

Hello osamaaftab2

Try this modified BLINKWITHOUT example from IDE.

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
// constants won't change:
const unsigned long interval[] {600000,10000};  // 10min/10sec
void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}
void loop() {
  // here is where you'd put code that needs to be running all the time.
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval[ledState]) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.