Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- B number order
- --3--
- 2 | | 4
- |--1--|
- 8 | | 6
- --7--o = 5
- */
- float temperature = 0, kelvin = 0;
- int sensorPin = 0;
- #include "Wire.h"
- #define DS1307_ADDRESS 0x68
- byte zero = 0x00; //workaround for issue #527
- const byte symbols[3] = {
- B00000000, B10000000, B11110000
- };
- const byte ledCharSet[10] =
- {
- B01110111, B00010100, B10110011, B10110110, B11010100,
- B11100110, B11100111, B00110100, B11110111, B11110110
- };
- const byte ledCharSetDot[10] =
- {
- B01111111, B00011100, B10111011, B10111110, B11011100,
- B11101110, B11101111, B00111100, B11111111, B11111110
- };
- const int latchPin = 8;
- const int clockPin = 12;
- const int dataPin = 11;
- void setup() {
- Wire.begin();
- setDateTime(); //MUST CONFIGURE IN FUNCTION
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(sensorPin, INPUT);
- pinMode(10, OUTPUT);
- }
- void loop() {
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 7);
- int second = bcdToDec(Wire.read());
- int minute = bcdToDec(Wire.read());
- int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
- int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
- int monthDay = bcdToDec(Wire.read());
- int month = bcdToDec(Wire.read());
- int year = bcdToDec(Wire.read());
- if((second/10)%2 == 1) {
- DisplayNumber();
- }
- if((second/10)%2 == 0) {
- kelvin = analogRead(sensorPin)*0.004882812*100;
- temperature = kelvin - 12.5 - 273.15;
- displayTemp(temperature);
- delay(10000);
- }
- }
- void DisplayNumber() {
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 7);
- int second = bcdToDec(Wire.read());
- int minute = bcdToDec(Wire.read());
- int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
- int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
- int monthDay = bcdToDec(Wire.read());
- int month = bcdToDec(Wire.read());
- int year = bcdToDec(Wire.read());
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[minute%10]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[minute/10]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSetDot[hour%10]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[hour/10]);
- digitalWrite(latchPin, 1);
- }
- void displayTemp(int num) {
- if (num < -10) {
- num = num*(-1);
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSetDot[num%10]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[(num%100)/10]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[1]);
- digitalWrite(latchPin, 1);
- }
- if (num < 0 && num > -10) {
- num = num*(-1);
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[(num%1)*10]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSetDot[num%10]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[1]);
- digitalWrite(latchPin, 1);
- }
- if (num < 10 && num >= 0) {
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[num%10]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- digitalWrite(latchPin, 1);
- }
- if (num >= 10) {
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[2]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[num%10]);
- shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[(num%100)/10]);
- shiftOut(dataPin, clockPin, MSBFIRST, symbols[0]);
- digitalWrite(latchPin, 1);
- }
- }
- void setDateTime(){
- byte second = 00; //0-59
- byte minute = 18; //0-59
- byte hour = 12; //0-23
- byte weekDay = 3; //1-7
- byte monthDay = 25; //1-31
- byte month = 4; //1-12
- byte year = 12; //0-99
- Wire.beginTransmission(DS1307_ADDRESS);
- Wire.write(zero); //stop Oscillator
- Wire.write(decToBcd(second));
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour));
- Wire.write(decToBcd(weekDay));
- Wire.write(decToBcd(monthDay));
- Wire.write(decToBcd(month));
- Wire.write(decToBcd(year));
- Wire.write(zero); //start
- Wire.endTransmission();
- }
- byte decToBcd(byte val){
- // Convert normal decimal numbers to binary coded decimal
- return ( (val/10*16) + (val%10) );
- }
- byte bcdToDec(byte val) {
- // Convert binary coded decimal to normal decimal numbers
- return ( (val/16*10) + (val%16) );
- }
Advertisement
Add Comment
Please, Sign In to add comment