Skip to content

Instantly share code, notes, and snippets.

@2N2222A
Created December 30, 2012 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2N2222A/4416025 to your computer and use it in GitHub Desktop.
Save 2N2222A/4416025 to your computer and use it in GitHub Desktop.
Arduino Climate Monitor/Control System revision 1.
/*
Arduino Climate Monitor and Control System By JS106351
Remember! You have to upload the sketch twice, once to set the time, and the second time
to remove the setClock() function as to not accidentally reset the clock at power up.
This only needs to be done once to set the RTC's registers.
Compatible RTC's include the DS1307 and the DS3231 (from what I know)
The code is set to control the humidity level in the room, but can be changed to control temperature too.
*/
#include <Sensirion.h>
const uint8_t dataPin = 3;
const uint8_t clockPin = 4;
const int hourButtonPin = 7;
const int minButtonPin = 8;
int hourButtonState;
int minButtonState;
float temperature;
float humidity;
float dewpoint;
Sensirion tempSensor = Sensirion(dataPin, clockPin);
#define SETCLOCK //compile clock setup code
int n;
#include <Wire.h>
#include <SPI_VFD.h>
SPI_VFD vfd(10, 11, 12, VFD_BRIGHTNESS25);
uint8_t brightness = 0;
#define DS1307_I2C_ADDRESS 0x68 //seven bit address
#define AM 0
#define PM 1
#define Mode12 1
void setup(){
Wire.begin();
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
vfd.setBrightness(3);
pinMode(hourButtonPin,INPUT);
pinMode(minButtonPin,INPUT);
hourButtonState = 0;
minButtonState = 0;
//setClock(); // uncomment this to set the time, upload code,
//then recomment and upload the code again to avoid resetting time
}
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, ampm;
getClock(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year, &ampm);
int hours= hour;
int minutes=minute;
int seconds=second;
if (hours<10){
vfd.setCursor(9,1);
vfd.print("0");
vfd.setCursor(10,1);
vfd.print(hour, DEC);
}else{
vfd.setCursor(9,1);
vfd.print(hour, DEC);
}
vfd.setCursor(11,1);
vfd.print(":");
if (minutes<10){
vfd.setCursor(12,1);
vfd.print("0");
vfd.setCursor(13,1);
vfd.print(minute, DEC);
}else{
vfd.setCursor(12,1);
vfd.print(minute, DEC);
}
vfd.setCursor(14,1);
vfd.print(":");
if (seconds<10){
vfd.setCursor(15,1);
vfd.print("0");
vfd.setCursor(16,1);
vfd.print(second, DEC);
}else{
vfd.setCursor(15,1);
vfd.print(second, DEC);
}
if (ampm != ' ') {
vfd.setCursor(18,1); //not space
//vfd.print(" ");
vfd.print(ampm);
vfd.setCursor(19,1);
vfd.print("M");
}
n++;
int setpoint=map(analogRead(0), 0, 1023, 40, 60);
if (n==5){
tempSensor.measure(&temperature, &humidity, &dewpoint);
vfd.setCursor(0,0);
vfd.print("RH");
vfd.setCursor(3,0);
vfd.print(humidity,1);
vfd.setCursor(7,0);
vfd.print("%");
vfd.setCursor(0,1);
vfd.print("TH");
vfd.setCursor(3,1);
vfd.print(convertToFahrenheit(temperature),1);
vfd.setCursor(7,1);
vfd.print("F");
digitalWrite(5, HIGH);
n=0;
}else{
digitalWrite(5, LOW);
}
if (humidity>setpoint+3){
digitalWrite(6, LOW);
vfd.setCursor(17,0);
vfd.print("OFF");
}
else if (humidity<setpoint-3){
digitalWrite(6, HIGH);
vfd.setCursor(17,0);
vfd.print(" ON");
}
vfd.setCursor(9,0);
vfd.print("SP");
vfd.setCursor(12,0);
vfd.print(setpoint);
vfd.setCursor(14,0);
vfd.print("%");
check_buttons();
delay(1000);
}
float convertToFahrenheit(float c) {
return ((9.0 / 5.0) * c) + 32.0;
}
/*void leadzero(byte val) {
if (val < 10) {
vfd.print("0");
}
}*/
//For initial setting of clock
void setClock() {
//prompt for time settings
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte mode = 0;
byte ampm = 0;
year = 12;
month = 12;
dayOfMonth = 30;
dayOfWeek = 1;
mode = Mode12;
ampm = AM;
hour = decToBcd(1);
minute = 56;
second = 10;
if (mode == Mode12) {
bitSet(hour, 6);
if (ampm == PM) {
bitSet(hour, 5);
}
}
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(hour); //already formatted with bits 6 and 5
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
int x=2; //for setting hours
int y=30; // for setting minutes
int z=0; // for setting seconds
byte q= PM; // for setting AM or PM
// For occasional setting of clock without change of date!
void setClock1() {
//prompt for time settings
byte second1, minute1, hour1;
byte mode1 = 0;
byte ampm1 = 0;
mode1 = Mode12;
ampm1 = q;
hour1 = decToBcd(x);
minute1 = y;
second1 = z;
if (mode1 == Mode12) {
bitSet(hour1, 6);
if (ampm1 == PM) {
bitSet(hour1, 5);
}
}
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second1)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute1));
Wire.send(hour1); //already formatted with bits 6 and 5
Wire.endTransmission();
}
void getClock(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year,
byte *ampm)
{
byte work;
byte mode; //12 or 24 hour
byte ap_ind; //am or pm indicator
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f); //mask CH bit (bit 7)
*minute = bcdToDec(Wire.receive());
// *hour = bcdToDec(Wire.receive());
work = Wire.receive(); // get hour byte
mode = bitRead(work, 6); // if bit 6 set, running 12 hour mode
if (mode == Mode12) {
ap_ind = bitRead(work, 5); // if bit 5 set, time is PM
*hour = bcdToDec(work & 0x1f); // mask bits 5 thru 7 for 12 hour clock
if (ap_ind == PM) {
*ampm = 'P';
}else{
*ampm = 'A';
}
}else{
*hour = bcdToDec(work & 0x3f); // mask bits 6 and 7 for 24 hour clock
*ampm = ' ';
}
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
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 setHour()
{
x++;
if(x > 12) //change this to 12 hour mode (if 24 hour mode then it will be x>23)
{
x = 0;
if (q==AM){
q=PM;
}else if(q==PM){
q=AM;
}
//seconds = 0; not needed?
//minutes = 0;
}
setClock1();
}
void setMinutes()
{
y++;
if(y > 59)
{
y = 0;
}
setClock1();
}
void check_buttons()
{
hourButtonState = digitalRead(hourButtonPin);
minButtonState = digitalRead(minButtonPin);
if(hourButtonState == HIGH){
setHour();
}
if(minButtonState == HIGH){
setMinutes();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment