Motor works 1 minute every 2 hours..??

Hi.
I am new to arduino and not expert with electronics.
I want to change my incubator setup that i had till now and rebuild it with arduino commands and control.
With the thermometer and humidity i have done it.
The problem i face and can't find a solution after many hours of search is the following...
I want to trigger a motor with a relay (egg turning motor) for one minute every two or three hours.
Can someone helps what do i must search for so i find a solution?
Thanks alot for any help.

Pol

The kind of thinking embodied in Blink Without Delay (aka BWOD) is good for this stuff. Instead of an led you have your relay, presumably via a transisitor.

It so happens I have a library to do BWOD for on time and off time differing. By my calculations, in milliseconds you have an on time of 60000 in every 9000000 (taking 2.5 hours.)

Usage is as below and library is attached. Just have (presumably) one instance of BWOD in there- call it what you like, perhaps BWOD turnMotor;

/* Blink without Delay in a class in a library
*/

#include <BWOD.h>

BWOD myBWOD;
BWOD anotherBWOD;

void setup() {
  myBWOD.attach(8, 97, 110);      //pin, on interval, off interval
  anotherBWOD.attach(14, 1798, 1015);
}

void loop()
{
  myBWOD.manageBlink();
  anotherBWOD.manageBlink();
}

BWOD.zip (1.21 KB)

Thank you JimboZA for your time to give me an answer and help.I will try it and i will report if it works as i want.
Thank you again.

You should look at the BWOD example if you didn't already, to familiarise yourself with the approach. My library uses that thinking, but you ought to understand the idea.

You may also find some useful ideas in Several Things at a Time

...R

@JimboZA:

BWOD.zip

Neat, K++

@Jimbo

The BWOD it works exactly as it should be.Is what i was looking for.Thank you.
But the problem i have now is how do i compile the incubator sketch with the BWOD.

#include <dht22.h>

 
dht22 dht22;//change to dht22 DHT22; if using the dht22

//you will also need to change all instances of DHT22 to DHT22

 
byte umidifiyerrelaypin = 2;// pin 2 humidifier relay

byte heatrelaypin = 3;// pin for heater resistant relay
 
void setup()

{

  dht22.attach(4);// pin from dht22
  Serial.begin(9600);

  Serial.println("Virtuabotix DHT22 FAN & Heater CONTROL");

  pinMode(umidifiyerrelaypin, OUTPUT);

  digitalWrite(umidifiyerrelaypin, LOW);//turn off the relay

  pinMode(heatrelaypin, OUTPUT);

  digitalWrite(heatrelaypin, LOW);//turn off the relay

}

 
void loop()

{

  Serial.println("\n");

 
  int chk = dht22.read();

 
  Serial.print("Read sensor: ");

  switch (chk)

  {

    case 0: Serial.println("OK"); break;

    case -1: Serial.println("Checksum error"); break;

    case -2: Serial.println("Time out error"); break;

    default: Serial.println("Unknown error"); break;

  }

 
  Serial.print("Humidity (%): ");

  Serial.println((float)dht22.humidity, DEC);

 
  if(dht22.humidity > 57)//incubator's humidity

  {

    digitalWrite(umidifiyerrelaypin, LOW);//off humidifier 

  }

  else

  {

    digitalWrite(umidifiyerrelaypin, HIGH);

  }

 
  Serial.print("Temperature (C): ");

  Serial.println(dht22.temperature, DEC);

 
  if(dht22.temperature < 26)//incubator's temperature
  {

    digitalWrite(heatrelaypin, HIGH);//heater off

  }

  else

  {

    digitalWrite(heatrelaypin, LOW);

  }

 
  delay(2000);

 
}

I know i ask too much but i am newbie .
Thank you all for your time to answer and help me.
Pol

Just put these above setup with your existing code:

#include <BWOD.h>

BWOD myBWOD;  // change name to suit

This in setup with the existing stuff:

myBWOD.attach(8, 97, 110);      //pin, on interval, off interval
// chnge name and use your values

And this somewhere in loop:

myBWOD.manageBlink(); // with your name

Yeaaaa ...i did it.
Thanks alot Jimbo.
Now the last thing to do is to add the 16 x 2 lcd screen so i can control the data without use the pc and run the serial.
But i will try tomorrow for this.

And if all go ok then in the future i will try to add or touch screen or buttons to change and correct the data.But this is not for now.
I will report the results in the future about the chicks.

Thanks again for the help.

Pol

Glad it works... just a note on why it does work, in case you didn't pick that up:

Blink without Delay as an approach, doesn't stop other code from working while it's doing its thing. It uses millis(), the Arduino's own timer, to see what the time is and to see if it's time to do blink-related stuff.

So it basically turns the led on or off, and notes the time. The processor continues doing other work in loop(). Then when loop starts again, we check the time elapsed since we last did blink stuff, against the required interval. Has the interval elapsed? If no, do no blink stuff and carry on in loop. If yes, do blink stuff (ie toggle the led on or off depending on if it's off or on) and continue in loop.

Round to top of loop again, rinse and repeat.

All I did with my library was bundle all that thinking into a couple of functions, with a bit of extra stuff to allow you to set the pin number and have on and off times that are not the same.

So with the library, every time it gets to do bwod.manageBlink(), that's it looking to see if the time has elapsed and it acts accordingly.

The whole point of this, is that the blinking doesn't interfere with your other code. All I did was bundle existing Blink WithOut Delay thinking, into a a library to make it easy to use.

int ledPin;
int ledState; // ledState used to set the LED
long previousMillis; // will store last time LED was updated

@JimboZA, may want to update to: unsigned long previousMillis;
There maybe a few other variables too that should be unsigned long.
.

LarryD:
int ledPin;
int ledState; // ledState used to set the LED
long previousMillis; // will store last time LED was updated

@JimboZA, may want to update to: unsigned long previousMillis;
There maybe a few more too.
.

Funny you should day that, I was just wondering if I got those right.

unsigned long previousMillis; // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval; // interval at which to blink (milliseconds)
unsigned long onTime;
unsigned long offTime;
and
void BWOD::attach(int p,unsigned long on, unsigned long off)

FYI
BWD using structure example:

//Blink without Delay skeleton example using a structure.
//LarryD

//======================================================================
struct timer
{
  //lastMillis = the time this "timer" was (re)started
  //waitMillis = delay time (mS) we are looking for. You can use math 60*60*1000 for 1 hour
  //restart    = do we start "this timer" again and again  
  //enableFlag = is "this timer" enabled/allowed to be accessed
  //**********************
  //For each timer object you need: 
  //Example:
  //   timer myTimer = //give the timer a name "myTimer"
  //   {
  //     0, 200UL, true, true  //lastMillis, waitMillis, restart, enableFlag 
  //   };
  // You have access to: 
  // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.CheckTime() 
  //**********************

  unsigned long lastMillis; 
  unsigned long waitMillis; 
  bool          restart; 
  bool          enableFlag;
  bool CheckTime() //Delay time expired function "CheckTime()"
  {
    //is the time up for this task?
    if (enableFlag && millis() - lastMillis >= waitMillis) 
    //Note: if delays of < 2 millis are needed use micros() and adjust waitMillis as needed
    {
      //should this start again? 
      if(restart)
      {
        //get ready for the next iteration
        lastMillis += waitMillis;  
      }
      //time was reached
      return true;
    }
    //time was not reached
    return false;
  } //END of CheckTime()

}; //END of structure timer
//======================================================================


//**********************************************************************
//Let's create 6 timer objects and initialize them in this sketch
//**********************************************************************
timer pin13 = //create timer pin13
{
  0, 200UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin12 = //create timer pin12
{
  0, 3*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin11 = //create timer pin11
{
  0, 10*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin10 = //create timer pin10
{
  0, 6*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer Toggle10 = //create timer Toggle10
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer checkSwitches = //create timer checkSwitches
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************

byte lastMySwitchState = 1; //for mySwitch on Pin 2
byte counter           = 0; 

const byte Pin13 = 13;
const byte Pin12 = 12;
const byte Pin11 = 11;
const byte Pin10 = 10;
const byte Pin9  =  9;

const byte mySwitch = 2;

//**********************************************************************

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

  pinMode(Pin13,OUTPUT);
  pinMode(Pin12,OUTPUT);
  pinMode(Pin11,OUTPUT);
  pinMode(Pin10,OUTPUT);
  pinMode(Pin9, OUTPUT);

  digitalWrite(Pin13,LOW);
  digitalWrite(Pin12,LOW);
  digitalWrite(Pin11,LOW);
  digitalWrite(Pin10,LOW);
  digitalWrite(Pin9, LOW);

  pinMode(mySwitch,INPUT_PULLUP);


} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<


void loop()
{
  //Below are examples demonstrating different timing situations 

  //***************************
  //example 1    Toggle Pin13 every 200ms
  if (pin13.CheckTime())
  {
    //Toggle Pin13
    digitalWrite(Pin13,!digitalRead(Pin13));

    //if you only want this section of code to happen once
    //uncomment the next line 
    //pin13.enableFlag = false;
  }

  //***************************
  //example 2    After 3 seconds, Pin12 goes and stays HIGH 
  if (pin12.CheckTime())
  {
    //Make Pin12 HIGH now
    digitalWrite(Pin12,HIGH);
    //disable timing section of code 
    pin12.enableFlag = false;
  }

  //***************************
  //example 3    Pin11 is HIGH for 10 seconds, then goes and stays LOW
  if (pin11.enableFlag && !pin11.CheckTime())
  {
    digitalWrite(Pin11,HIGH);
  }
  //10 seconds is now up now, leave the Pin11 LOW
  else
  {
    digitalWrite(Pin11,LOW);
    //disable timing section of code 
    pin11.enableFlag = false;
  }

  //***************************
  //example 4    For 6 seconds, toggle Pin10
  if (pin10.enableFlag && !pin10.CheckTime())
  {
    //example 5  Toggling Pin10 every 50mS
    if(Toggle10.CheckTime())
    {  
      //toggle Pin10
      digitalWrite(Pin10,!digitalRead(Pin10));    
    }
  }
  //6 seconds is now up, toggling is stopped
  else
  {
    digitalWrite(Pin10,LOW);
    //disable timing section of code 
    pin10.enableFlag = false;
  }

  //***************************
  //example 6    Is it time to check the switches?
  if (checkSwitches.CheckTime())
  {
    //time to read the switches
    Switches();      
  } 

  //**********************************
  //Put other non-blocking stuff here
  //**********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//**********************************************************************
//switches are checked every checkSwitches.waitMillis milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void Switches()  
{
  boolean thisState; //re-usable for all the switches      

  //*************************** mySwitch Pin 2 code  
  //check if this switch has changed state
  thisState = digitalRead(mySwitch); 
  if (thisState != lastMySwitchState)
  {  
    //update the switch state
    lastMySwitchState = thisState;  

    //This switch position has changed, let's do some stuff

    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if(thisState == HIGH)        
    {
      //example: LED on Pin9 is Push ON, Push OFF
      digitalWrite(Pin9,!digitalRead(Pin9)); 
    }

    //"LOW condition code"
    //switch goes from HIGH to LOW
    else                          
    {
      //example: display the current switch push count 
      Serial.println(++counter);      
    }
  } //END of mySwitch Pin 2 code

  //******************************************  
  //similar code for other switches goes here 
  //******************************************  

} //END of Switches()


//======================================================================
//                        E N D  O F  C O D E
//======================================================================

I would like to report that the above sketch working like a charm, perfect.I have finished the incubator construction and setup everything.Put it to work and four days now is working very precise.
The arduino controls the heater resistant ,the turn egg motor,and the humidifier.
Thank you all for helping me.

I will also report the incubation results .