// Deven Rich 12-5-2013
// This project was built on the Arduino Uno - ATmega328P
// I would also like to give credit to Maurice Ribble for providing chunks of the RTC code
// This code sets up the DS1307 Real Time clock on the Arduino board to controll 3 dosing pumps
// The RTC keeps track of time, the code checks it and turns on the pumps at a specified time
// to dose your aquarium
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.writeWire.writeWire.write(0x80);
Wire.endTransmission();
}*/
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
//define pins
int motorPin1 = 9;
int motorPin2 = 10;
int motorPin3 = 11;
void setup() // run once, when the sketch starts
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
Wire.begin();
Serial.begin(9600);
// Change these values to what you want to set your clock to.
// You only need to run this the first time you setup your RTC.
// Set the correct value below and un comment it to run it.
/*
second = 45;
minute = 55;
hour = 9;
dayOfWeek = 2;
dayOfMonth = 30;
month = 4;
year = 13;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
*/
}
void loop() // run over and over again
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// this prints the output to the serial window (tools > serial monitor in arduino) and is great for testing
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
// Set the time you want the motors to kick in
if((hour == 21)&&(minute == 23)&&(second==10)){
Serial.print(" TRUE");
Serial.println(" ");
Serial.println(" MP1");
analogWrite(motorPin1, 255);
delay(8500); // set how long you want the motor to run... 1000 = aprox 1ml
analogWrite(motorPin1, 0);
Serial.println(" MP2");
analogWrite(motorPin2, 255);
delay(9500); // set how long you want the motor to run... 1000 = aprox 1ml
analogWrite(motorPin2, 0);
Serial.println(" MP3");
analogWrite(motorPin3, 255);
delay(5500); // set how long you want the motor to run... 1000 = aprox 1ml
analogWrite(motorPin3, 0);
}
// we dont really need this since we set the pin to low above but just incase <img src="http://fishtankprojects.com/wp-includes/images/smilies/simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;">
else{Serial.println(" false");
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 0);
analogWrite(motorPin3, 0);
}
delay(1000);
}
No comments:
Post a Comment