Having problems in code

Hi there :D, im new on arduino and i did some projects by myself, but now im having some troubles on my new project.
Im trying to make a kennel with arduino, like im trying to make the handler job easier, if they press a push button they can open the food container and the food falls on to the dog bowl and if they push the other push button associated to the servo motor they close the container, and they can see the temperature and the humidity of the room on a 16x4 LCD.

Im using a arduino mega 2560, a DHT11 sensor, 2 LCD's 16x4, 4 push buttons and 2 servo motor HS-422.

I made the codes separated and then joined them, the codes were working fine alone, but then when i joined them the servo motors didnt work, i can ear the servo motor working, but if i push the button they dont move, i cant find the problem i searched the internet i cant find nothing.

My code:

#include <stdio.h>
#include <dht.h>
#include <LiquidCrystal.h>
#include<Servo.h>

#define DHTPIN A0
#define DHTPIN1 A1

int pos = 0;

Servo servo;
Servo servo2;




LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd2(12, 10, 5, 4, 3, 2);



dht DHT;

void setup(){
 
 lcd.begin(20, 4);
 lcd2.begin(16, 2);
 
pinMode(52, INPUT);
pinMode(50, INPUT);
pinMode(53, INPUT);
pinMode(51, INPUT);

 servo.attach(22);
 servo2.attach(24);
}

void loop()
{
 DHT.read11(DHTPIN);
 lcd.setCursor(0,0); 
 lcd.print("Temp= ");
 lcd.print(DHT.temperature);
 lcd.print("C");
 lcd.setCursor(0,1);
 lcd.print("Hum= ");
 lcd.print(DHT.humidity);
 lcd.print("% ");
 delay(2000);
 DHT.read11(DHTPIN1);
 lcd2.setCursor(0,0); 
 lcd2.print("Temp= ");
 lcd2.print(DHT.temperature);
 lcd2.print("C");
 lcd2.setCursor(0,1);
 lcd2.print("Hum= ");
 lcd2.print(DHT.humidity);
 lcd2.print("% ");
 delay(2000);

 if (digitalRead(52) == HIGH && pos < 180) {
   pos++;
   servo.write(pos);
   delay(15);
 }
 if (digitalRead(50) == HIGH && pos > 0) {
   pos--;
   servo.write(pos);
   delay(15);
 }
 if (digitalRead(53) == HIGH && pos < 180) {
   pos++;
   servo2.write(pos);
   delay(15);
 }
 if (digitalRead(51) == HIGH && pos > 0) {
   pos--;
   servo2.write(pos);
   delay(15);
 }
}

I did the images on circuits.io and doesnt exist arduino mega so i have 1 image for the 2 LCD's and sensor and the other for the push buttons, on the second image the white lines represent the wires that will conect on the arduino ( first wire on the 52 pin, second wire on the 50 pin, third wire on the 53 pin and fourth wire on the 51 pin), two of the servo motors pins will conect on the GND and 5V, and the other will conect on the 22 pin, the other servo motor pin will not conect on 22 pin but on the 24 pin.

If someone could help me i would be very happy.

1.PNG

Please add code tags around your code. Todo so, edit your post and

type [code] before your code and
type[/code] after your code.

Also, please provide a wiring diagram how everything is connected. A photo / scan of a handdrawn one is fine.

How are you powering the servos?

You have two significant delays in the lcd section which are blocking the button reading. It looks like you will move 1 degree every 4 seconds. Without the display code you were moving 1 degree every 15 ms.

Take a look at the "blink without delay" example in the ide,(File>Examples>02Digital>BlinkWithoutDelay) and place the dht readings and their display on a millis() timer.

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

...R

Im feeding my servo with 5 V from the arduino.
I still dont get the delay part, could you tell me more specific whats the problem with the delay, please :confused: .

In a delay, the Arduino does NOTHING, absolute nothing. It just waits and waits. It does not read buttons, it updates nothing, it just sits there. Not very great if you want to detect a button press :wink:

trode:
Im feeding my servo with 5 V from the arduino.

The usual advice is NOT to do that as Servos can draw more power than the 5v pin can provide. That can damage the Arduino or at least cause it to behave erratically.

Give the servo a separate power supply and connect the servo GND to the Arduino GND.

...R

So i take out the delay from the servos?

So i take out the delay from the servos?

Start by taking the two delay(2000) calls out of the dht/lcd routines.

You may want to place that section of code on a "blink without delay" millis() timer.

I´ve been trying tounderstand the blink without dealy code but i cant understand how do i put it on my code.
Can someone help me pls?

I´ve been trying to understand the blink without delay code but i cant understand how do i put it on my code.

What have you tried? Give us your best effort to remove the two delay(2000) calls from temperature and humidity reading and display section.

You can place that whole block of code within a millis() timer.

if(millis() -lastTime >= someInterval)
{
lastTime = millis(); //alternatively you can use lastTime += someInterval
//read and display temperature and humidity
}