Running two things


hi,
i trying run project need temperature sensor recording day no stop. need servo motor run, has long delay (12 hours). wondering if possible run on same arduino. have both of codes below.

servo motor code
code: [select]


#include <servo.h>

servo myservo;  // create servo object control servo
// twelve servo objects can created on boards

int pos = 0;    // variable store servo position

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

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


temperature sensor code
code: [select]
//dht humidity/temperature sensors
#include "dht.h"
#include <servo.h>
#define dhtpin 7     // 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 left) of sensor +5v
// connect pin 2 of sensor whatever dhtpin is
// connect pin 4 (on right) of sensor ground
// connect 10k resistor pin 2 (data) pin 1 (power) of 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 250 milliseconds!
  // sensor readings may 2 seconds 'old' (its 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 blocking nature of delay(), luckily there way manage timing without delay.

start looking @ blink without delay example in ide @ file > examples > digital or here.

you need make note of time using millis(), function returning time since startup, , every time through loop() compare new millis() (which little bit bigger since time ahs flown) noted millis() see if interval has elapsed. if hasn't, nothing , again next time round. if has, servo stuff, , note new start time. rinse , repeat.



Arduino Forum > Using Arduino > Programming Questions > Running two things


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor