Running two things

Hi,
I am trying to run a project where I need my temperature sensor recording all day with no stop. I also need a servo motor to run, but that has a very long delay (12 hours). I was wondering if its is possible to run it on the same arduino. I have both of the codes below.

Servo motor code

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
  delay(5000);                                            // waits 15ms for the servo to reach the position
  for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees        
    delay(15);// waits 15ms for the servo to reach the position
       delay(43200000);
  
}

Temperature sensor code

//DHT humidity/temperature sensors
#include "DHT.h"
#include <Servo.h>
#define DHTPIN 7     // what pin we're connected to
Servo myservo;
//whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);
int pos = 0;
void setup() {
  Serial.begin(9600); 
 myservo.attach(9);
  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float t = dht.readTemperature ();
  float h = dht.readHumidity ();
    Serial.print("Temperature: "); 
      Serial.println(t*9/5+32);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.println("%");
   delay(3000);
  }

You've encountered the blocking nature of delay(), but luckily there is a way to manage timing without delay.

Start by looking at the Blink Without Delay example in the IDE at File > Examples > Digital or here.

You just need to make a note of the time using millis(), which is a function returning the time since startup, and then every time through loop() compare the new millis() (which will be a little bit bigger since time ahs flown) with that noted millis() to see if the interval has elapsed. If it hasn't, do nothing and look again next time round. If it has, do the servo stuff, and note the new start time. Rinse and repeat.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

There should be no difficulty doing your two tasks on the same Arduino.

...R

so basically I use Millis() instead of delay() and put both code in loop?

Norgart200:
so basically I use Millis() instead of delay() and put both code in loop?

You don't just replace delay() with millis() but you do use millis() to determine whether the required period has passed by putting the check in loop() so that it is checked frequently. Other actions can be carried out inside loop() as long as they don't block its free operation.