Temperature dependent time control

Dear all,

I want to program my Arduino to start a fan for a defined period of time if the enviroment Temperature is higher than a defined Tempature. So far so easy. But I would not like to use a delay, as I also want the Arduino to update other incoming data and the LCD in the same time the fan is running. So I have to work with millis() (and yes, I watched blinkwithoutdelay 1000 times :slight_smile: ) but Im still lost. Problem is, that the if funtion (if (temp1>Tsoll) kills all following attempts of time controlling. The fan should stay on even if temp1 falls below the defined value again. Need help.
Best regards and Thanks.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

Is this simulator code?

Please describe the problem better then you just did.

Info about multi things

Move the timing out of the 'if'. Before you check the temperature, check if the fan is on and, if it is, check if it has been on for the required amount of time.

1 Like

consider

const byte PinInp = A0;
const byte PinLed = LED_BUILTIN;

const int  Thresh = 500;

enum { Off = HIGH, On = LOW };

unsigned long msecPeriod = 2000;
unsigned long msecLst;
unsigned long msec;

byte pinState;
bool run;

// -----------------------------------------------------------------------------
void
loop (void)
{
    msec = millis ();

    if (run && (msec - msecLst) >= msecPeriod)  {
        run = false;
        digitalWrite (PinLed, Off);
    }

    int val = analogRead (PinInp);
    if (Thresh <= val)  {
        run = true;
        msecLst = msec;
        digitalWrite (PinLed, On);
    }

    Serial.println (val);
}

void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinInp, INPUT_PULLUP);
    pinState = analogRead (PinInp);

    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);
}
1 Like

Thank you so much. that´s it! directly proofed it with lowering Thresh to 200 to test it with a simple poti. Nice!

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