servo zaman yardım

öncelikle mrb oğlumla bir kuluçka makinası yaptık tek takıldıgımız yer servo motorun zaman konusu delay kullandığımızda kodlar duruyo ve sıcaklı ve nem ölçümü alamıyoruz servoyu 2 saatte bir 45 derece çevirmek istiyoruz bu konuda yardımcı olursanız çok sevinirim

kod :
#include “Adafruit_LiquidCrystal.h”
#include <dht11.h> // dht11 kütüphanesini ekliyoruz.
#define DHT11PIN A4 // DHT11PIN olarak Dijital 2’yi belirliyoruz.
dht11 DHT11;
int rezistans = 8;
int nem = 9;
Adafruit_LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include<Servo.h>
Servo motor;
int aci;

//ARDUINO HOCAM TEMPERATURE CONTROLLED FAN

void setup () {
Serial.begin(9600);
pinMode ( rezistans , HIGH );
pinMode ( nem , HIGH );
lcd.begin(16, 2);
int millis();
motor.attach(6);
}

void loop() {
//sürekli döngü içinde çalışacak kodumuzu yazıyoruz
ac_kapa();//ac kapa fonksiyonunu çalıştırıyoruz fonksiyon loop içinde olduğu için
}

void ac_kapa() {
delay(1000); // bir saniye daha bekliyoruz
{
int chk = DHT11.read(DHT11PIN);
Serial.print("Nem (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Sicaklik): ");
Serial.println((float)DHT11.temperature, 2);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DERECE: ");
lcd.print((float)DHT11.temperature, 2);
lcd.setCursor(0, 1);
lcd.print("NEM: ");
lcd.print((float)DHT11.humidity);
Serial.begin(9600);
delay(1000);

for(aci=30;aci<=120;aci+=3){ // ACI DEGERLERİ
motor.write(aci);
delay(100);
}
delay(1000);//ÇEVİRME ZAMAN AYARI YAPILICAK

for(aci=120;aci>=30;aci-=3){
motor.write(aci);
delay(100);
}
delay(1000);//ÇEVİRME ZAMAN AYARI YAPILICAK
}

if (DHT11.temperature <= 37.00 )// 37 DERECENİN ALTIN DA ROLE AÇOLIR
{
digitalWrite (rezistans , HIGH);
}else
{
digitalWrite (rezistans , LOW );
}

if (DHT11.humidity <= 55.00 )// 55 nem ALTIN DA ROLE AÇOLIR
{
digitalWrite (nem , HIGH);
}else
{
digitalWrite (nem , LOW );
}

}

How good is your English? Google translate: İngilizcen ne kadar iyi?

Please use English or google translate. Google translate: Lütfen İngilizce veya google translate kullanın

google translate of opening post:

firstly we made an incubator with my mrb son, the only place we have installed is when we use the delay of servo motor, the codes stop and we can not get the temperature and humidity measurement, we want to turn the servo 45 degrees every 2 hours.

I'm not familiar with servos.

From your description, I suspect that you have two problems:
1)
Your display doe not update for roughly 20 seconds. If so, your code does 90 times 100 milliseconds delay equals 9 seconds in each for-loop; plus some additional delays (why?). During that time, you can't read the temperature sensor. ou will need to get rid of the for-loops.
2)
You do not know how to do something every 2 hours.

Is that all correct?

Something to read for now:

google translate

Servolara aşina değilim.

Açıklamanızdan, iki sorununuz olduğundan şüpheleniyorum:
1)
Ekranınız yaklaşık 20 saniye güncellenmiyor. Öyleyse, kodunuz her döngü için 90 mil 100 milisaniye gecikme 9 saniyeye eşittir; artı bazı ek gecikmeler (neden?). Bu süre zarfında sıcaklık sensörünü okuyamazsınız. Döngülerden kurtulmanız gerekecek.
2)
Her 2 saatte bir nasıl yapacağınızı bilmiyorsunuz.

Hepsi doğru mu?

Şimdilik okunacak bir şey:

How to use this forum - please read.

Using millis() for timing. A beginners guide

Demonstration code for several things at the same time

cevap için tesekkür ederim aralardaki gecikmeleri düzenleyebilirim evet haklısın ama servoyu her 2 saatte bir sağa 2 saat sonra sola çevirmem için bana yardımcı olurmusun inan kafam durdu ve fazlada bilgi sahibi diğilim sadece merak var bu kuluçkada oğlum için yapmam gerekli

for(aci=30;aci<=120;aci+=3){ // ACI DEGERLERİ
motor.write(aci);
delay(100);
}
delay(1000);//ÇEVİRME ZAMAN AYARI YAPILICAK

for(aci=120;aci>=30;aci-=3){
motor.write(aci);
delay(100);
}
delay(1000);//ÇEVİRME ZAMAN AYARI YAPILICAK
}
delay kullanmadan nasıl olacak bana yazarmısın

Lütfen İngilizce veya google translate kullanın

Why the heck Do you keep Posting on turkish? Seriously, sterretje his Motivation is one in a Million... I dont See the point why i should Do more work to help you then you to receive help.

Thank you for the answer, I can arrange the delays between you. Yes, you are right, but believe me, my head has stopped and my other knowledgeable extra is just curious.

for (pain = 30; pain <= 120; pain + = 3) {// ACI VALUES
motor.writ to (ACU);
delay (100);
}
delay (1000); // TURNING TIME TO BE SET

for (ac = 120; aci> = 30; opened from = 3) {
motor.writ to (ACU);
delay (100);
}
delay (1000); // TURNING TIME TO BE SET
}
How would you write to me without using delay?

sterretje:
Using millis() for timing. A beginners guide

Sterretje also answered this question. Just read that. It is very good explained. This will ultimately solve your problem as well.

Your head will even hurt more after this :wink:

Note that I don’t have experience with this and it’s solely based on your code.

First we take the the for-loop and place it in a function; I’ll demonstrate for the moving of the servo from 30…120

bool moveServoForward()
{
  for (aci = 30; aci <= 120; aci += 3) // ACI DEGERLERİ
  {
    motor.write(aci);
    delay(100);
  }
}

Next we remove the for-loop and create a local static variable aci; you can remove aci from the top of your code. Using local static variables allows you to keep the function completely self-contained.

bool moveServoForward()
{
  // aci; range from 30 .. 120
  static byte aci = 30;
  
  motor.write(aci);
  delay(100);
}

Next we get rid of delay; first add a static variable for the last time that you moved the servo a step.

bool moveServoForward()
{
  // aci; range from 30 .. 120
  static byte aci = 30;
  // last time that servo moved
  static unsigned long lastServoMoveTime;
  
  motor.write(aci);
  delay(100);
}

Next we add a test to check if 100 ms has lapsed and get rid of the delay.

bool moveServoForward()
{
  // aci; range from 30..120
  static byte aci = 30;
  // last time that servo moved
  static unsigned long lastServoMoveTime;
  
  // check if it's time for the next step
  if (millis() - lastServoMoveTime >= 100)
  {
    // move the servo
    motor.write(aci);
    // update the last time that the servo moved
    lastServoMoveTime = millis();
    // update aci for the next time that we call servoMoveForward
    aci+=3;
  }
}

Lastly you need to check if the complete move from 30…120 is done and inform the calling function

/*
  move servo forward from 30 .. 120
  Returns:
    true when move is completed, else false
*/
bool moveServoForward()
{
  // aci; range from 30 .. 120
  static byte aci = 30;
  // last time that servo moved
  static unsigned long lastServoMoveTime;
  
  // check if it's time for the next step
  if (millis() - lastServoMoveTime >= 100)
  {
    // debug
    Serial.print(millis());
    Serial.print("\tMoving servo to ");
    Serial.println(aci);
    // move the servo
    motor.write(aci);
    // update the last time that the servo moved
    lastServoMoveTime = millis();
    // update aci for the next time that we call servoMoveForward
    aci+=3;
  }
  
  // if we've done 120
  if (aci > 120)
  {
    // debug
    Serial.println("Moving complete");
    // reset aci
    aci = 30;
    // tell the aclling function that the movement is complete
    return true;
  }
  else
  {
    return false;
  }
}

The code for moving the servo the other way around is basically the same.

/*
  move servo backward from 120 .. 30
  Returns:
    true when move is completed, else false
*/
bool moveServoBackward()
{
  // aci; range from 120 .. 30
  static byte aci = 120;
  // last time that servo moved
  static unsigned long lastServoMoveTime;
  
  // check if it's time
  if (millis() - lastServoMoveTime >= 100)
  {
    // debug
    Serial.print(millis());
    Serial.print("\tMoving servo to ");
    Serial.println(aci);
    // move the servo
    motor.write(aci);
    // update the last time that the servo moved
    lastServoMoveTime = millis();
    // update aci for the next time that we call servoMoveForward
    aci-=3;
  }
  
  // if we have done 30
  if(aci < 30)
  {
    // debug
    Serial.println("Moving complete");
    // reset aci
    aci = 120;
    // tell the calling function that the movement is complete
    return true;
  }
  else
  {
    return false;
  }
}

You now need to implement a small finite state machine to switch between moving forward and moving backward and the delays in between.
Add the below near the top of your code; it defines the four states.

enum SERVOSTATES
{
  ST_SERVO_FORWARD,         // state where servo moves forward
  ST_SERVO_WAIT4BACKWARD,   // state where we wait before moving servo backward
  ST_SERVO_BACKWARD,        // state were servo moves backward
  ST_SERVO_WAIT4FORWARD,    // state where we wait before moving servo forward again
};

This goes in a new function.

/*
  move servo from 30..120 and back with 1 second delay in between
*/
void moveServo()
{
  // variabele for servo move state; initial value forward
  static SERVOSTATES currentState = ST_SERVO_FORWARD;
  // variabele for 'delay' between moves
  static unsigned long delayStartTime;
  
  switch(currentState)
  {
    case ST_SERVO_FORWARD:
      // move from 30 .. 120; when done
      if(moveServoForward() == true)
      {
        // set next state
        currentState = ST_SERVO_WAIT4BACKWARD;
        // set start time for 'delay'
        delayStartTime = millis();
        // debug
        Serial.print(millis());
        Serial.println("\tSwitching to delay");
      }
      break;
    case ST_SERVO_WAIT4BACKWARD:
      // if 'delay' finished
      if (millis() - delayStartTime >= 1000)
      {
        // set next state
        currentState = ST_SERVO_BACKWARD;
        // debug
        Serial.print(millis());
        Serial.println("\tDelay finished; switching to backward");
      }
      break;
    case ST_SERVO_BACKWARD:
      // move from 120 .. 30; when done
      if(moveServoBackward() == true)
      {
        // set next state
        currentState = ST_SERVO_WAIT4FORWARD;
        // set start time for 'delay'
        delayStartTime = millis();
        // debug
        Serial.print(millis());
        Serial.println("\tSwitching to delay");
      }
      break;
    case ST_SERVO_WAIT4FORWARD:
      if (millis() - delayStartTime >= 1000)
      {
        // set next state
        currentState = ST_SERVO_FORWARD;
        // debug
        Serial.print(millis());
        Serial.println("\tDelay finished; switching to forward");
      }
      break;
  }
}

moveServo() can be called from loop() or your own function.

Full demo code to move the servo forward and backward. Based on the serial output, this works.

enum SERVOSTATES
{
  ST_SERVO_FORWARD,
  ST_SERVO_WAIT4BACKWARD,
  ST_SERVO_BACKWARD,
  ST_SERVO_WAIT4FORWARD,
};

#include <Servo.h>
Servo motor;

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

  motor.attach(6);
}

void loop()
{
  moveServo();
}


/*
  move servo forward from 30 .. 120
  Returns:
    true when move is completed, else false
*/
bool moveServoForward()
{
  // aci; range from 30 .. 120
  static byte aci = 30;
  // last time that servo moved
  static unsigned long lastServoMoveTime;
  
  // check if it's time for the next step
  if (millis() - lastServoMoveTime >= 100)
  {
    // debug
    Serial.print(millis());
    Serial.print("\tMoving servo to ");
    Serial.println(aci);
    // move the servo
    motor.write(aci);
    // update the last time that the servo moved
    lastServoMoveTime = millis();
    // update aci for the next time that we call servoMoveForward
    aci+=3;
  }
  
  // if we've done 120
  if (aci > 120)
  {
    // debug
    Serial.println("Moving complete");
    // reset aci
    aci = 30;
    // tell the aclling function that the movement is complete
    return true;
  }
  else
  {
    return false;
  }
}

/*
  move servo backward from 120 .. 30
  Returns:
    true when move is completed, else false
*/
bool moveServoBackward()
{
  // aci; range from 120 .. 30
  static byte aci = 120;
  // last time that servo moved
  static unsigned long lastServoMoveTime;
  
  // check if it's time
  if (millis() - lastServoMoveTime >= 100)
  {
    // debug
    Serial.print(millis());
    Serial.print("\tMoving servo to ");
    Serial.println(aci);
    // move the servo
    motor.write(aci);
    // update the last time that the servo moved
    lastServoMoveTime = millis();
    // update aci for the next time that we call servoMoveForward
    aci-=3;
  }
  
  // if we have done 30
  if(aci < 30)
  {
    // debug
    Serial.println("Moving complete");
    // reset aci
    aci = 120;
    // tell the calling function that the movement is complete
    return true;
  }
  else
  {
    return false;
  }
}

/*
  move servo from 30..120 and back with 1 second delay in between
*/
void moveServo()
{
  // variabele for servo move state; initial value forward
  static SERVOSTATES currentState = ST_SERVO_FORWARD;
  // variabele for 'delay' between moves
  static unsigned long delayStartTime;
  
  switch(currentState)
  {
    case ST_SERVO_FORWARD:
      //
      if(moveServoForward() == true)
      {
        currentState = ST_SERVO_WAIT4BACKWARD;
        delayStartTime = millis();
        Serial.print(millis());
        Serial.println("\tSwitching to delay");
      }
      break;
    case ST_SERVO_WAIT4BACKWARD:
      if (millis() - delayStartTime >= 10000)
      {
        currentState = ST_SERVO_BACKWARD;
        Serial.print(millis());
        Serial.println("\tDelay finished; switching to backward");
      }
      break;
    case ST_SERVO_BACKWARD:
      if(moveServoBackward() == true)
      {
        currentState = ST_SERVO_WAIT4FORWARD;
        delayStartTime = millis();
        Serial.print(millis());
        Serial.println("\tSwitching to delay");
      }
      break;
    case ST_SERVO_WAIT4FORWARD:
      if (millis() - delayStartTime >= 10000)
      {
        currentState = ST_SERVO_FORWARD;
        Serial.print(millis());
        Serial.println("\tDelay finished; switching to forward");
      }
      break;
  }
}

Note that I modified the delays between the moves.

You should now have enough info to get rid of all delays in your code; whatever you do, don’t use delay() as it will break the basics.

sen çok iyi bir adamsın teşekkür ederim dostum :

Google translate

you are a very good man thank you my friend:

Pleasure.

Please use google translate in future.