Skip to content

Instantly share code, notes, and snippets.

@2N2222A
Last active December 10, 2015 07:28
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/4401347 to your computer and use it in GitHub Desktop.
Save 2N2222A/4401347 to your computer and use it in GitHub Desktop.
Arduino climiate monitor and control system that uses an Sensirion temperature and humidity sensor, a Maxim (Dallas) RTC and a Samsung 20x2 VFD display. RTC time is in 12 hour mode.
/* 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.
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;
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);
//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("%");
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("%");
delay(1000);
}
float convertToFahrenheit(float c) {
return ((9.0 / 5.0) * c) + 32.0;
}
/*void leadzero(byte val) {
if (val < 10) {
vfd.print("0");
}
}*/ //Part of original code but was not needed and was commented out.
void setClock() {
//prompt for time settings
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte mode = 0;
byte ampm = 0;
year = 12; //Set the year
month = 12; //Set the month 1-12
dayOfMonth = 12; //Set the day 1-31
dayOfWeek = 5; //Set the day of week 1-7
mode = Mode12; //12 hour mode
ampm = PM; //Set part of day AM or PM
hour = decToBcd(2); //Set the hour 1-12
minute = 28; //Set the minute 0-59
second = 8; //Set the seconds 0-59
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();
}
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) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment