May 7, 2011
admin

Light+Time Final: PhysComp Room Countdown Clock


















Code:

/*****************************************
* Liza Stark
* Physical Computing Final Project: Light+Time
* May 3, 2011
*
* This sketch uses the Macetech Chronodot and I2C protocol
* to program a cleanup countdown clock for the p comp room.
*
* Code for the I2C communication with the Chronodot was
* adapted from code written by Maurice Ribble. Documentation
* of it can be found here: http://www.glacialwanderer.com/hobbyrobotics/?p=12
*
*
*
*
*
*****************************************/

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

int cleanUpLED = 8; //Set LED action for first alarm
int lastCallLED = 10; //Set LED action for second alarm
int getOutLED = 11; //Set LED action for third alarm

// 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) );
}

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.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(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.send(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f);
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}

void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);

// Change values to set clock
second = 30;
minute = 15;
hour = 2;
dayOfWeek = 1;
dayOfMonth = 25;
month = 4;
year = 11;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

pinMode (cleanUpLED,OUTPUT);
pinMode (lastCallLED,OUTPUT);
pinMode (getOutLED,OUTPUT);

}

void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day_of_week:");
Serial.println(dayOfWeek, DEC);

delay(1000);

//Set tests for each day of the week

//////////////*********************************Initial Test***********************************//////////////////////
// Starts at 2:15:30 on Day 1

//2:15:40 am = Clean Up!
if((dayOfWeek == 4 ||dayOfWeek == 3 ||dayOfWeek == 2 ||dayOfWeek == 1) && hour == 2 && minute == 15 && second == 40)
{
Serial.println("Clean up!");
digitalWrite(cleanUpLED, HIGH);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(cleanUpLED, LOW);
}

//2:15:50 am = Last Call_This is not a pub
if((dayOfWeek == 4 ||dayOfWeek == 3 ||dayOfWeek == 2 ||dayOfWeek == 1) && hour == 2 && minute == 15 && second == 50)
{
Serial.println("Last Call - this is not a pub");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, HIGH);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(lastCallLED, LOW);
}

//2:16:00 am = Get Out
if((dayOfWeek == 4 ||dayOfWeek == 3 ||dayOfWeek == 2 ||dayOfWeek == 1) && hour == 2 && minute == 16 && second == 00)
{
Serial.println("Get out.");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, HIGH);

delay(4000);

digitalWrite(getOutLED, LOW);
}

//////////////*********************************Test Monday - Thursday***********************************//////////////////////

//12:50 am = Clean Up!
if((dayOfWeek == 4 ||dayOfWeek == 3 ||dayOfWeek == 2 ||dayOfWeek == 1) && hour == 0 && minute == 50 && second == 0)
{
Serial.println("Clean up!");
digitalWrite(cleanUpLED, HIGH);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(cleanUpLED, LOW);
}

//1:15 am = Last Call_This is not a pub
if((dayOfWeek == 4 ||dayOfWeek == 3 ||dayOfWeek == 2 ||dayOfWeek == 1) && hour == 1 && minute == 15 && second == 0)
{
Serial.println("Last Call - this is not a pub");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, HIGH);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(lastCallLED, LOW);
}

//1:30 am = Get Out
if((dayOfWeek == 4 ||dayOfWeek == 3 ||dayOfWeek == 2 ||dayOfWeek == 1) && hour == 1 && minute == 30 && second == 0)
{
Serial.println("Get out.");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, HIGH);

delay(4000);

digitalWrite(getOutLED, LOW);

}

//////////////*********************************Test Friday and Sunday***********************************//////////////////////

//10:50 pm = Clean Up!
if((dayOfWeek == 5 || dayOfWeek == 7) && hour == 23 && minute == 50 && second == 0)
{
Serial.println("Clean up!");
digitalWrite(cleanUpLED, HIGH);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(cleanUpLED, LOW);
}

//11:15 pm = Last Call_This is not a pub
if((dayOfWeek == 5 || dayOfWeek == 7) && hour == 24 && minute == 15 && second == 0)
{
Serial.println("Last Call - this is not a pub");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, HIGH);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(lastCallLED, LOW);
}

//11:30 pm = Get Out
if((dayOfWeek == 5 || dayOfWeek == 7) && hour == 24 && minute == 30 && second == 0)
{
Serial.println("Get out.");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, HIGH);

delay(4000);

digitalWrite(getOutLED, LOW);
}

//////////////*********************************Test Saturday***********************************//////////////////////

//6:50 pm = Clean Up!
if(dayOfWeek == 6 && hour == 18 && minute == 50 && second == 0)
{
Serial.println("Clean up!");
digitalWrite(cleanUpLED, HIGH);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(cleanUpLED, LOW);
}

//7:15 pm = Last Call_This is not a pub
if(dayOfWeek == 6 && hour == 19 && minute == 15 && second == 0)
{
Serial.println("Last Call - this is not a pub");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, HIGH);
digitalWrite(getOutLED, LOW);

delay(4000);

digitalWrite(lastCallLED, LOW);
}

//7:30 pm = Get Out
if(dayOfWeek == 6 && hour == 19 && minute == 30 && second == 0)
{
Serial.println("Get out.");
digitalWrite(cleanUpLED, LOW);
digitalWrite(lastCallLED, LOW);
digitalWrite(getOutLED, HIGH);

delay(4000);

digitalWrite(getOutLED, LOW);
}

}

Leave a comment