Hi everyone! I am a British Colombia Institute of Technology Student and is currently studying Mechanical Engineering. I am currently doing a project for my Engineering Project course. My project is a Automated Kitchen Garden controlled by Arduino and Rasp Pi that uses a growing method called hydroponics. I hope to seek help from your expertise on programming the Arduino and automation.
I have started with the main frame of the program and have ran into problems with time alarms. I'm basically trying to mix the PC time sync code with the Time alarm code from the Arduino Cookbook by Michael Margolis. The timers trigger just fine, but the alarms won't trigger. I'm using a Arduino Mega. Below is my code.
#include <TimeAlarms.h>
#include<Time.h>
#define TIME_MSG_LEN 11 // time sync consists of a HEADER followed by ten
// ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
//inputs (excluding pH and EC)
const int temp1=A0;
const int temp2=A1;
const int temp3=A2;
const int temp4=A3;
const int temp5=A4;
const int temp6=A5;
const int level1=2;
const int level2=3;
const int level3=4;
const int level4=5;
//outputs
//Pumps:
const int pHup=22;
const int pHdown=24;
const int nutVeg=26;
const int nutFlw=28;
const int mixVeg=30;
const int mixFlw=31;
const int feedVeg=32;
const int feedFlw=33;
//Drivers:
const int led1=34;
const int led2=35;
const int led3=36;
const int led4=37;
//Fans:
const int ledFan1=38;
const int ledFan2=39;
const int ledFan3=40;
const int cabFan=41;
const int boxFan=42;
const int coolFan1=43;
const int coolFan2=44;
//Others:
const int coolPad1=45;
const int coolPad2=46;
const int alarm=47;
//Variables:
unsigned long vegStr;
unsigned long flwStr;
void setup(){
//Serial devices baud rate set
Serial.begin(38400);
Serial1.begin(38400);
Serial2.begin(38400);
Serial3.begin(38400);
//Define pin mode
//Inputs
pinMode(temp1,INPUT);
pinMode(temp2,INPUT);
pinMode(temp3,INPUT);
pinMode(temp4,INPUT);
pinMode(temp5,INPUT);
pinMode(temp6,INPUT);
pinMode(level1,INPUT);
pinMode(level2,INPUT);
pinMode(level3,INPUT);
pinMode(level4,INPUT);
//Outputs
pinMode(pHup,OUTPUT);
pinMode(pHdown,OUTPUT);
pinMode(nutVeg,OUTPUT);
pinMode(nutFlw,OUTPUT);
pinMode(mixVeg,OUTPUT);
pinMode(mixFlw,OUTPUT);
pinMode(feedVeg,OUTPUT);
pinMode(feedFlw,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(ledFan1,OUTPUT);
pinMode(ledFan2,OUTPUT);
pinMode(ledFan3,OUTPUT);
pinMode(cabFan,OUTPUT);
pinMode(boxFan,OUTPUT);
pinMode(coolFan1,OUTPUT);
pinMode(coolFan2,OUTPUT);
pinMode(coolPad1,OUTPUT);
pinMode(coolPad2,OUTPUT);
pinMode(alarm,OUTPUT);
//Turn on Dry Box Fan
digitalWrite(boxFan,HIGH);
//Time Request
Serial.println("waiting for sync message");
//Alarms and Timers
Alarm.alarmRepeat(22,15,0,dayTime);
Alarm.alarmRepeat(22,16,0,nightTime);
Alarm.timerRepeat(120,wtrVeg);
Alarm.timerRepeat(120,wtrFlw);
}
void loop(){
Alarm.delay(0);
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus()!= timeNotSet)
{
// here if the time has been set
digitalClockDisplay();
}
if (millis()-vegStr>=15000){
digitalWrite(feedVeg,LOW);
Serial.println("Veg Pumpp Off");
}
if (millis()-flwStr>=15000){
digitalWrite(feedFlw,LOW);
Serial.println("Flw Pumpp Off");
}
}
void dayTime(){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
Serial.println("Lights On");
}
void nightTime(){
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
Serial.println("Lights Off");
}
void wtrVeg(){
digitalWrite(feedVeg,HIGH);
vegStr=millis();
Serial.println("Veg Pump On");
}
void wtrFlw(){
digitalWrite(feedFlw,HIGH);
flwStr=millis();
Serial.println("Flw Pump On");
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
Alarm.delay(1000);
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon
// and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
// time message consists of a header and ten ascii digits
while(Serial.available() >= TIME_MSG_LEN ){
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( isDigit(c)) {
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync clock to the time received on serial port
}
}
}
Please ignore the long list of pin designations as they are used for future coding. As you can see, the time library realated codes are the same as the ones found in the Cookbook. I'm using the Alarms to turn on and off some relays which are controlling LED panels for the plants. The timers trigger feeding pumps for feeding nutrient infused solution to the plant roots.