Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. //Made by Tymek Grabowski
  2.  
  3. #include <Arduino.h>
  4. #include <Wire.h>
  5. #include <dht.h>
  6. #include <TM1637Display.h>
  7.  
  8. #define DS3231_I2C_ADDRESS 0x68
  9.  
  10. #define DHT11_PIN 7
  11.  
  12. #define CLK 9
  13. #define DIO 8
  14.  
  15. byte decToBcd(byte val){
  16.   return((val/10*16) + (val%10));
  17. }
  18.  
  19. byte bcdToDec(byte val){
  20.   return((val/16*10) + (val%16));
  21. }
  22.  
  23. dht DHT;
  24.  
  25. TM1637Display display(CLK, DIO);
  26.  
  27. float tempIN;
  28. float tempOUT;
  29.  
  30. const int tempINPin = 2;
  31. const int tempOUTPin = 3;
  32. const int buttonPin = 2;
  33. const int buttonPin2 = 3;
  34.  
  35. const long interval = 10000;
  36. const long debounceDelay = 350;
  37.  
  38. int reading;
  39. int reading2;
  40. int buttonState = LOW;
  41. int buttonState2 = LOW;
  42. int varByte = 1;
  43. int priority = 1;
  44. int segmentsOFF = 0;
  45.  
  46.  
  47. unsigned long timedigit;
  48. unsigned long datedigit;
  49. unsigned long previousMillis = 0;
  50. unsigned long lastDebounceTime = 0;
  51. unsigned long lastDebounceTime2 = 0;
  52.  
  53. uint8_t data[] = {0x0, 0x0, 0x0, 0x0};
  54. const uint8_t segto = 0x80;
  55.  
  56. uint8_t in[] = { SEG_B | SEG_C };
  57. uint8_t out[] = { SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F };
  58.  
  59. void setup()
  60. {
  61. analogReference(INTERNAL);
  62.  
  63. Wire.begin();
  64. Serial.begin(9600);
  65. display.setBrightness(7);
  66.  
  67. pinMode(buttonPin, INPUT);
  68. pinMode(buttonPin2, INPUT);
  69. //setDS3231time(45, 9, 19, 1, 19, 3, 18);
  70. }
  71.  
  72. void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
  73. {
  74.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  75.   Wire.write(0);
  76.   Wire.write(decToBcd(second));
  77.   Wire.write(decToBcd(minute));
  78.   Wire.write(decToBcd(hour));
  79.   Wire.write(decToBcd(dayOfWeek));
  80.   Wire.write(decToBcd(dayOfMonth));
  81.   Wire.write(decToBcd(month));
  82.   Wire.write(decToBcd(year));
  83.   Wire.endTransmission();
  84. }
  85. void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
  86.   {
  87.     Wire.beginTransmission(DS3231_I2C_ADDRESS);
  88.     Wire.write(0);
  89.     Wire.endTransmission();
  90.     Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  91.  
  92.     *second = bcdToDec(Wire.read() & 0x7f);
  93.     *minute = bcdToDec(Wire.read());
  94.     *hour = bcdToDec(Wire.read() & 0x3f);
  95.     *dayOfWeek = bcdToDec(Wire.read());
  96.     *dayOfMonth = bcdToDec(Wire.read());
  97.     *month = bcdToDec(Wire.read());
  98.     *year = bcdToDec(Wire.read());
  99.   }
  100.  
  101. void loop()
  102. {  
  103.   buttonState = digitalRead(buttonPin);
  104.   buttonState2 = digitalRead(buttonPin2);
  105.  
  106.   if((millis() - lastDebounceTime) > debounceDelay){
  107.    
  108.     if(buttonState == HIGH && segmentsOFF == 0){
  109.       display.setSegments(data);
  110.       segmentsOFF = 1;
  111.       lastDebounceTime = millis();
  112.     }else if(buttonState == HIGH && segmentsOFF == 1){
  113.       segmentsOFF = 0;
  114.       priority = 1;
  115.       lastDebounceTime = millis();
  116.     }
  117.   }
  118.  
  119.   if((millis() - lastDebounceTime2) > debounceDelay){
  120.    
  121.     if(buttonState2 == HIGH && varByte == 1){
  122.       Serial.println("Page 2");
  123.       varByte = 2;
  124.       lastDebounceTime2 = millis();
  125.       display.setSegments(data);
  126.      
  127.     }else if(buttonState2 == HIGH && varByte == 2){
  128.       Serial.println("Page 3");
  129.       varByte = 3;  
  130.       lastDebounceTime2 = millis();
  131.       priority = 1;
  132.       display.setSegments(data);
  133.      
  134.     }else if(buttonState2 == HIGH && varByte == 3){
  135.       Serial.println("Page 4");
  136.       varByte = 4;  
  137.       lastDebounceTime2 = millis();
  138.       priority = 1;
  139.       display.setSegments(data);
  140.      
  141.     }else if(buttonState2 == HIGH && varByte == 4){
  142.       Serial.println("Page 1");
  143.       varByte = 1;  
  144.       lastDebounceTime2 = millis();
  145.       display.setSegments(data);
  146.     }
  147.   }
  148.  
  149.   switch(varByte){
  150.     case 1:
  151.       Page1();
  152.       break;
  153.      
  154.     case 2:
  155.       Page2();
  156.       break;
  157.  
  158.     case 3:
  159.       Page3();
  160.       break;
  161.  
  162.     case 4:
  163.       Page4();
  164.       break;
  165.     }
  166. }
  167.  
  168. void Page1()
  169. {
  170.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  171.   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  172.   timedigit = hour * 100 + minute;
  173.  
  174.   if(segmentsOFF == 0){
  175.     display.showNumberDecEx(timedigit, (0x80 >> 1), true);
  176.   }
  177.  
  178.   if(priority == 0){
  179.     priority = 1;
  180.   }
  181. }
  182.  
  183. void Page2(){
  184.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  185.   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  186.  
  187.   if(segmentsOFF == 0){
  188.     datedigit = (dayOfMonth * 100) + month;
  189.     display.showNumberDecEx(datedigit, false);
  190.   }
  191. }
  192.  
  193. void Page3()
  194. {
  195.   reading = analogRead(tempINPin);
  196.   tempIN =  reading / 9.31;
  197.  
  198.   if(segmentsOFF == 0){
  199.     unsigned long currentMillis = millis();
  200.  
  201.     if(currentMillis - previousMillis > interval){
  202.       previousMillis = currentMillis;
  203.       if(segmentsOFF == 0){
  204.         display.showNumberDecEx(tempIN, false, 0, 2);
  205.         display.setSegments(in, 1, 3);
  206.       }
  207.     }
  208.  
  209.     if(priority == 1){
  210.         display.showNumberDecEx(tempIN, false, 0, 2);
  211.         display.setSegments(in, 1, 3);
  212.         priority = 0;
  213.     }
  214.   }
  215. }
  216.  
  217. void Page4(){
  218.   reading2 = analogRead(tempOUTPin);
  219.   tempOUT =  reading2 / 9.31;
  220.  
  221.   if(segmentsOFF == 0){
  222.     unsigned long currentMillis = millis();
  223.  
  224.     if(currentMillis - previousMillis > interval){
  225.       previousMillis = currentMillis;
  226.       display.showNumberDecEx(tempOUT, false, 0, 2);
  227.       display.setSegments(out, 1, 3);
  228.     }
  229.    
  230.     if(priority == 1){
  231.       display.showNumberDecEx(tempOUT, false, 0, 2);
  232.       display.setSegments(out, 1, 2);
  233.       priority = 0;
  234.     }
  235.   }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement