Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Made by Tymek Grabowski
- #include <Arduino.h>
- #include <Wire.h>
- #include <dht.h>
- #include <TM1637Display.h>
- #define DS3231_I2C_ADDRESS 0x68
- #define DHT11_PIN 7
- #define CLK 9
- #define DIO 8
- byte decToBcd(byte val){
- return((val/10*16) + (val%10));
- }
- byte bcdToDec(byte val){
- return((val/16*10) + (val%16));
- }
- dht DHT;
- TM1637Display display(CLK, DIO);
- float tempIN;
- float tempOUT;
- const int tempINPin = 2;
- const int tempOUTPin = 3;
- const int buttonPin = 2;
- const int buttonPin2 = 3;
- const long interval = 10000;
- const long debounceDelay = 350;
- int reading;
- int reading2;
- int buttonState = LOW;
- int buttonState2 = LOW;
- int varByte = 1;
- int priority = 1;
- int segmentsOFF = 0;
- unsigned long timedigit;
- unsigned long datedigit;
- unsigned long previousMillis = 0;
- unsigned long lastDebounceTime = 0;
- unsigned long lastDebounceTime2 = 0;
- uint8_t data[] = {0x0, 0x0, 0x0, 0x0};
- const uint8_t segto = 0x80;
- uint8_t in[] = { SEG_B | SEG_C };
- uint8_t out[] = { SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F };
- void setup()
- {
- analogReference(INTERNAL);
- Wire.begin();
- Serial.begin(9600);
- display.setBrightness(7);
- pinMode(buttonPin, INPUT);
- pinMode(buttonPin2, INPUT);
- //setDS3231time(45, 9, 19, 1, 19, 3, 18);
- }
- void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
- {
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0);
- Wire.write(decToBcd(second));
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour));
- Wire.write(decToBcd(dayOfWeek));
- Wire.write(decToBcd(dayOfMonth));
- Wire.write(decToBcd(month));
- Wire.write(decToBcd(year));
- Wire.endTransmission();
- }
- void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
- {
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0);
- Wire.endTransmission();
- Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
- *second = bcdToDec(Wire.read() & 0x7f);
- *minute = bcdToDec(Wire.read());
- *hour = bcdToDec(Wire.read() & 0x3f);
- *dayOfWeek = bcdToDec(Wire.read());
- *dayOfMonth = bcdToDec(Wire.read());
- *month = bcdToDec(Wire.read());
- *year = bcdToDec(Wire.read());
- }
- void loop()
- {
- buttonState = digitalRead(buttonPin);
- buttonState2 = digitalRead(buttonPin2);
- if((millis() - lastDebounceTime) > debounceDelay){
- if(buttonState == HIGH && segmentsOFF == 0){
- display.setSegments(data);
- segmentsOFF = 1;
- lastDebounceTime = millis();
- }else if(buttonState == HIGH && segmentsOFF == 1){
- segmentsOFF = 0;
- priority = 1;
- lastDebounceTime = millis();
- }
- }
- if((millis() - lastDebounceTime2) > debounceDelay){
- if(buttonState2 == HIGH && varByte == 1){
- Serial.println("Page 2");
- varByte = 2;
- lastDebounceTime2 = millis();
- display.setSegments(data);
- }else if(buttonState2 == HIGH && varByte == 2){
- Serial.println("Page 3");
- varByte = 3;
- lastDebounceTime2 = millis();
- priority = 1;
- display.setSegments(data);
- }else if(buttonState2 == HIGH && varByte == 3){
- Serial.println("Page 4");
- varByte = 4;
- lastDebounceTime2 = millis();
- priority = 1;
- display.setSegments(data);
- }else if(buttonState2 == HIGH && varByte == 4){
- Serial.println("Page 1");
- varByte = 1;
- lastDebounceTime2 = millis();
- display.setSegments(data);
- }
- }
- switch(varByte){
- case 1:
- Page1();
- break;
- case 2:
- Page2();
- break;
- case 3:
- Page3();
- break;
- case 4:
- Page4();
- break;
- }
- }
- void Page1()
- {
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
- timedigit = hour * 100 + minute;
- if(segmentsOFF == 0){
- display.showNumberDecEx(timedigit, (0x80 >> 1), true);
- }
- if(priority == 0){
- priority = 1;
- }
- }
- void Page2(){
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
- if(segmentsOFF == 0){
- datedigit = (dayOfMonth * 100) + month;
- display.showNumberDecEx(datedigit, false);
- }
- }
- void Page3()
- {
- reading = analogRead(tempINPin);
- tempIN = reading / 9.31;
- if(segmentsOFF == 0){
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval){
- previousMillis = currentMillis;
- if(segmentsOFF == 0){
- display.showNumberDecEx(tempIN, false, 0, 2);
- display.setSegments(in, 1, 3);
- }
- }
- if(priority == 1){
- display.showNumberDecEx(tempIN, false, 0, 2);
- display.setSegments(in, 1, 3);
- priority = 0;
- }
- }
- }
- void Page4(){
- reading2 = analogRead(tempOUTPin);
- tempOUT = reading2 / 9.31;
- if(segmentsOFF == 0){
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval){
- previousMillis = currentMillis;
- display.showNumberDecEx(tempOUT, false, 0, 2);
- display.setSegments(out, 1, 3);
- }
- if(priority == 1){
- display.showNumberDecEx(tempOUT, false, 0, 2);
- display.setSegments(out, 1, 2);
- priority = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement