Not looping -servo control

hello all, could someone help me please, its driving me crazy.
Im trying to build a egg incubator, the code seems to work fine apart from the egg turning servo,
it moves in one direction, then goes back to start after a set time . Then thats it. it wont move no more. I have tried to put in a loop function but i either am not putting it in the write place or im doing something wrong.

#include <LiquidCrystal.h>
#include <Servo.h>
#include <dht.h>
#define DHT22 A0
const int ok = A1;
const int UP = A2;
const int DOWN = A3;
const int bulb = A4;
const int vap = A5;
const int rs = 12;
const int en = 11;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
int ack = 0;
int pos = 0;
int sec = 0;
int Min = 0;
int hrs = 0;
// ************ set default temps and humidty **********
int T_threshold = 37;
int H_threshold = 60;
int SET = 0;
int Direction = 0;
boolean T_condition = true;
boolean H_condition = true;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo motor;
dht DHT;
void setup()
{
pinMode(ok, INPUT);
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(bulb, OUTPUT);
pinMode(vap, OUTPUT);
digitalWrite(bulb, LOW);
digitalWrite(vap, LOW);
digitalWrite(ok, HIGH);
digitalWrite(UP, HIGH);
digitalWrite(DOWN, HIGH);
motor.attach(7);
motor.write(pos);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.setCursor(5, 0);
lcd.print("Digital");
lcd.setCursor(4, 1);
lcd.print("Incubator");
delay(1500);
}
void loop()
{
if (SET == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Temperature:");
lcd.setCursor(0, 1);
lcd.print(T_threshold);
lcd.print(" *C");
while (T_condition)
{
if (digitalRead(UP) == LOW)
{
T_threshold = T_threshold + 1;
lcd.setCursor(0, 1);
lcd.print(T_threshold);
lcd.print(" *C");
delay(200);
}
if (digitalRead(DOWN) == LOW)
{
T_threshold = T_threshold - 1;
lcd.setCursor(0, 1);
lcd.print(T_threshold);
lcd.print(" *C");
delay(200);
}
if (digitalRead(ok) == LOW)
{
delay(200);
T_condition = false;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Humidity:");
lcd.setCursor(0, 1);
lcd.print(H_threshold);
lcd.print("%");
delay(100);
while (H_condition)
{
if (digitalRead(UP) == LOW)
{
H_threshold = H_threshold + 1;
lcd.setCursor(0, 1);
lcd.print(H_threshold);
lcd.print("%");
delay(100);
}
if (digitalRead(DOWN) == LOW)
{
H_threshold = H_threshold - 1;
lcd.setCursor(0, 1);
lcd.print(H_threshold);
lcd.print("%");
delay(200);
}
if (digitalRead(ok) == LOW)
{
delay(100);
H_condition = false;
}
}
SET = 1;
}
ack = 0;
int chk = DHT.read22(DHT22);
switch (chk)
{
case DHTLIB_ERROR_CONNECT:
ack = 1;
break;
}
if (ack == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(DHT.temperature);
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.print(DHT.humidity);
if (DHT.temperature >= T_threshold)
{
delay(3000);
if (DHT.temperature >= T_threshold)
{
digitalWrite(bulb, LOW);
}
}
if (DHT.humidity >= H_threshold)
{
delay(3000);
if (DHT.humidity >= H_threshold)
{
digitalWrite(vap, LOW);
}
}
if (DHT.temperature < T_threshold)
{
delay(3000);
if (DHT.temperature < T_threshold)
{
digitalWrite(bulb, HIGH);
}
}
if (DHT.humidity < H_threshold)
{
delay(3000);
if (DHT.humidity < H_threshold)
{
digitalWrite(vap, HIGH);
}
}
sec = sec + 1;
if (sec == 60)
{
sec = 0;
Min = Min + 1;
}
if (Min == 60)
{
Min = 0;
hrs = hrs + 1;
}
if (hrs == 8 && Min == 0 && sec == 0)
{
for (pos = 0; pos <= 180; pos += 1)
{
motor.write(pos);
delay(25);
}
}
if (hrs == 16 && Min == 0 && sec == 0)
{
hrs = 0;
for (pos = 180; pos >= 0; pos -= 1)
{
motor.write(pos);
delay(25);
}
}
}
if (ack == 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Sensor data.");
lcd.setCursor(0, 1);
lcd.print("System Halted.");
digitalWrite(bulb, LOW);
digitalWrite(vap, LOW);
}
delay(1000);
}

please make sure you indent code in the IDE before copying, that’s done by pressing ctrlt on a PC or cmdt on a Mac)

this is just not readable on my small screen (I'm reading from my phone)

is this any better, i turned it all blue in,ide2 then did ctrl t , then copied it and paste here , not sure if it did anything differant or not??

#include <LiquidCrystal.h>
#include <Servo.h>
#include <dht.h>
#define DHT22 A0
const int ok = A1;
const int UP = A2;
const int DOWN = A3;
const int bulb = A4;
const int vap = A5;
const int rs = 12;
const int en = 11;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
int ack = 0;
int pos = 0;
int sec = 0;
int Min = 0;
int hrs = 0;
// ************ set default temps and humidty **********
int T_threshold = 37;
int H_threshold = 60;
int SET = 0;
int Direction = 0;
boolean T_condition = true;
boolean H_condition = true;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo motor;
dht DHT;
void setup() {
  pinMode(ok, INPUT);
  pinMode(UP, INPUT);
  pinMode(DOWN, INPUT);
  pinMode(bulb, OUTPUT);
  pinMode(vap, OUTPUT);
  digitalWrite(bulb, LOW);
  digitalWrite(vap, LOW);
  digitalWrite(ok, HIGH);
  digitalWrite(UP, HIGH);
  digitalWrite(DOWN, HIGH);
  motor.attach(7);
  motor.write(pos);
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.setCursor(5, 0);
  lcd.print("Digital");
  lcd.setCursor(4, 1);
  lcd.print("Incubator");
  delay(1500);
}
void loop() {
  if (SET == 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Temperature:");
    lcd.setCursor(0, 1);
    lcd.print(T_threshold);
    lcd.print(" *C");
    while (T_condition) {
      if (digitalRead(UP) == LOW) {
        T_threshold = T_threshold + 1;
        lcd.setCursor(0, 1);
        lcd.print(T_threshold);
        lcd.print(" *C");
        delay(200);
      }
      if (digitalRead(DOWN) == LOW) {
        T_threshold = T_threshold - 1;
        lcd.setCursor(0, 1);
        lcd.print(T_threshold);
        lcd.print(" *C");
        delay(200);
      }
      if (digitalRead(ok) == LOW) {
        delay(200);
        T_condition = false;
      }
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Humidity:");
    lcd.setCursor(0, 1);
    lcd.print(H_threshold);
    lcd.print("%");
    delay(100);
    while (H_condition) {
      if (digitalRead(UP) == LOW) {
        H_threshold = H_threshold + 1;
        lcd.setCursor(0, 1);
        lcd.print(H_threshold);
        lcd.print("%");
        delay(100);
      }
      if (digitalRead(DOWN) == LOW) {
        H_threshold = H_threshold - 1;
        lcd.setCursor(0, 1);
        lcd.print(H_threshold);
        lcd.print("%");
        delay(200);
      }
      if (digitalRead(ok) == LOW) {
        delay(100);
        H_condition = false;
      }
    }
    SET = 1;
  }
  ack = 0;
  int chk = DHT.read22(DHT22);
  switch (chk) {
    case DHTLIB_ERROR_CONNECT:
      ack = 1;
      break;
  }
  if (ack == 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temp:");
    lcd.print(DHT.temperature);
    lcd.setCursor(0, 1);
    lcd.print("Humidity:");
    lcd.print(DHT.humidity);
    if (DHT.temperature >= T_threshold) {
      delay(3000);
      if (DHT.temperature >= T_threshold) {
        digitalWrite(bulb, LOW);
      }
    }
    if (DHT.humidity >= H_threshold) {
      delay(3000);
      if (DHT.humidity >= H_threshold) {
        digitalWrite(vap, LOW);
      }
    }
    if (DHT.temperature < T_threshold) {
      delay(3000);
      if (DHT.temperature < T_threshold) {
        digitalWrite(bulb, HIGH);
      }
    }
    if (DHT.humidity < H_threshold) {
      delay(3000);
      if (DHT.humidity < H_threshold) {
        digitalWrite(vap, HIGH);
      }
    }
    sec = sec + 1;
    if (sec == 60) {
      sec = 0;
      Min = Min + 1;
    }
    if (Min == 60) {
      Min = 0;
      hrs = hrs + 1;
    }
    if (hrs == 8 && Min == 0 && sec == 0) {
      for (pos = 0; pos <= 180; pos += 1) {
        motor.write(pos);
        delay(25);
      }
    }
    if (hrs == 16 && Min == 0 && sec == 0) {
      hrs = 0;
      for (pos = 180; pos >= 0; pos -= 1) {
        motor.write(pos);
        delay(25);
      }
    }
  }
  if (ack == 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("No Sensor data.");
    lcd.setCursor(0, 1);
    lcd.print("System Halted.");
    digitalWrite(bulb, LOW);
    digitalWrite(vap, LOW);
  }
  delay(1000);
}

does the code seem to still be working? you should add Serial debug statements, like printing the value of ack and the hrs, min and sec

yes the code works fine, relays go on and off according to the dht22 ,servo moves ok, but only moves one way then back , then stops, when the servo should be doing its cycle continuously, ie turning the eggs every 4 hrs for at least 18 days or until i unplug the servo.

i did try and put a loop in for the servo but just wouldnt work or keep going

i just had a thought, are my colons the write way round for it too loop ?

    if (hrs == 8 && Min == 0 && sec == 0) {
      for (pos = 0; pos <= 180; pos += 1) {
        motor.write(pos);
        delay(25);
      }
    }
    if (hrs == 16 && Min == 0 && sec == 0) {
      hrs = 0;
      for (pos = 180; pos >= 0; pos -= 1) {
        motor.write(pos);
        delay(25);
      }

given that your servo goes at least once from 0 to 180 and back from 180 to 0 that code is fine.

if you don't see that happening it's probably that either the hrs, min, sec no longer do what you expect or that you don't enter the if (ack == 0) { part of the code anymore (or that the arduino crashed

if you want to be sure your for loops work, try this code

#include <Servo.h>
Servo motor;

void setup() {
  motor.attach(7);
  motor.write(0);
}

void loop() {
  for (int pos = 0; pos <= 180; pos += 1) {
    motor.write(pos);
    delay(25);
  }

  for (int pos = 180; pos >= 0; pos -= 1) {
    motor.write(pos);
    delay(25);
  }
}

thank you, will give that a try when i get home from work.

i tried incorporating it into the code but get this error ?
Compilation error: a function-definition is not allowed here before '{' token

void setup() {

don't incorporate, run it as a full code - nothing else

you should see your egg incubator flipping over and over

ahh i have managed to do that ok with another sample program and it worked ok, just need to incorporate it somehow or tweek the code i have to endless loop the servo that can be timed in between movements from one side to the other (wait times for it moves back again).

and thanks for answering so quick

if that works then your issue is elsewhere in the code. add printing as suggested

yeah i assumed something was wrong in the code, thats why i was attempting to put my own loop in, but could not suss out where or how

there are if () that could prevent the motor to move. if it does not move it and the hardware is proven to be working with a simple code (showing power and wires are probably right) it means the condition in the if does not let you go through. ➜ that's why I'm saying you should print that out to the serial monitor and see what's going on in your code

(and BTW instead of maintaining hours, minutes and seconds with a delay(1000) you should use millis())

thank you once again, will play tonight with the code and servo in front of me

At the risk of damaging your productivity at work, insofar as measured by your employer, I point out that you can play with servos and code wherever you have a decent computer and an internet connection.

See

what I mean.

a7

i i did this to both the IF in the code
//if (hrs == 00 && Min == 0 && sec == 10)

now its going back and forwards constantantly with a small delay at the start of the 1st move.

sorry to be a pain, how or where abouts in the code can i put a delay for 4 hrs between each move.

thank you for your assistance, i took your advice and lost all the (IF's) and changed it too this below and all works as it should and within the time i set.

for (pos = 0; pos <= 150; pos += 1) {
          motor.write(pos);
          delay(25);
        }
        delay(600000);  //60000 = 1 minute * 3600000 = 1hr * 14400000 = 4hrs
      }

      for (pos = 150; pos >= 0; pos -= 1) {
        motor.write(pos);
        delay(25);
      }
      delay(600000);  //60000 = 1 minute * 3600000 = 1hr * 14400000 = 4hrs
    }

You have too many closing curly brackets "}"

lol your probably right, but i will not see the code once ive finished with, as so far is working good.

so as they say "dont fix whats not broken"