Trap software and programming

Software and programming

Since i have 2 purposes for this project which is

  • reading the temperature and the moisture in the LCD

  • Rotate the servo motor at a specific time to touch the camera so when i will be in the field i can recieve a photo from the inside of the trap

I wanted to try if it works so i first tried the motor with the real clock ds3231

But before that i had to set The RTC otherwise it will print wrong data (time and date)

  #include <Wire.h>
 #include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () 
{
  Serial.begin(9600);
      delay(3000); // wait for console opening

  if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
 }

 if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");

// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
 //rtc.adjust(DateTime(2021, 12, 30, 13, 9, 0));
   }
   }

 void loop () 
{
DateTime now = rtc.now();

Serial.println("Current Date & Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.println("Unix Time: ");
Serial.print("elapsed ");
Serial.print(now.unixtime());
Serial.print(" seconds/");
Serial.print(now.unixtime() / 86400L);
Serial.println(" days since 1/1/1970");

// calculate a date which is 7 days & 30 seconds into the future
DateTime future (now + TimeSpan(7,0,0,30));

Serial.println("Future Date & Time (Now + 7days & 30s): ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(1000);
 }

So to set the RTC I had First to upload the code showing above and not to frget to comment out the highlighted code lines

So in my case I set :

The Date :30/12/2021

The Hour :13.09.00

After uploading the code with the main modification I had tp download othertime and to comment the highlighted code lines again and yu will see that the RTC is printing the current dae and hour on the serial port

The I tested the dht 11 with he LCD , so to evaluate this step correctly , the LCD must display the moisture and the temperature values

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#include <dht.h>


#define dht_apin A0 // Analog Pin sensor is connected to

 dht DHT;

  void setup(){
 lcd.init();
 lcd.backlight();
Serial.begin(9600);
 delay(500);//Delay to let system boot


}//end "setup()"

void loop(){
//Start of Program

DHT.read11(dht_apin);
lcd.setCursor(0,0);
lcd.print(" Humidity =");
lcd.print(DHT.humidity);
 lcd.setCursor(0,12);
Serial.print(" humidity = ");
Serial.print(DHT.humidity);
Serial.print("%  ");
lcd.setCursor(0,1);
lcd.print("Temperature=");
lcd.print(DHT.temperature);
 lcd.setCursor(9,1);
Serial.print("Temperature= ");
Serial.print(DHT.temperature); 
Serial.println("C  ");

delay(5000);//Wait 5 seconds before accessing sensor again.



  }// end loop(

So Let’s say that at first look my board is well done but i need to test the whole components together i mean the DHT11, the servo motor , the DS3231 rtc clock , and the LCD

I uploaded my final code to the board using arduino as showing bellow and finally i see that every thing is going perfectly

Actually it is a nice progress, the board design and the programming part are done successfully

It just still to power my board , so as i mentioned i used a battery 12 v , and the reason that i used a battery that the system can be installed everywhere in the fields

All my components need a 5v , so i included a voltage regulator in my board to reduce the 12v to 5v , I was really afraid that something went bad while designing or soldering the board so i can loose the microcontroller or one of the components due to a high voltage . But I think that I took my time and I checked every thing more then once so every thing will be ok , I hope !!!!!!

And yes thanks god my project board is well done , the components recieve the 5v and i could read the humidity and the temperature i also see the servo rotating at the time i had picked in the code sended by the RTC DS3231

Check the Trap final code