Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #include <BH1750.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6.  
  7.  
  8. #define ONE_WIRE_BUS_1 2
  9. #define ONE_WIRE_BUS_2 4
  10.  
  11. LiquidCrystal_I2C lcd(0x27, 20, 4);
  12. OneWire oneWire_out(ONE_WIRE_BUS_2);
  13. DallasTemperature sensors( & oneWire_out);
  14. BH1750 lightMeter(0x23);
  15.  
  16. int targetToOpen = 1000;
  17. int targetToClose = 10;
  18. bool nightPassed = true;
  19. bool dayPassed = true;
  20. bool switchedUP = false;
  21. bool switchedDOWN = false;
  22. bool button1 = HIGH;
  23. bool button2 = HIGH;
  24. bool up = false;
  25. bool down= false;
  26. bool isDown = false;
  27. bool isUp = false;
  28.  
  29. void setup(void) {
  30. pinMode(11, INPUT_PULLUP); //Przycisk jako wejście
  31. pinMode(12, INPUT_PULLUP); //Przycisk jako wejście
  32. pinMode(2, OUTPUT); //Przekaznik jako wyjscie -> gora
  33. digitalWrite(2, HIGH); //Ustawienie stanu niskiego na procie 8
  34. pinMode(3, OUTPUT); //Przekaznik jako wyjscie -> dol
  35. digitalWrite(3, HIGH); //Ustawienie stanu niskiego na procie 8
  36. Serial.begin(9600); // po co?
  37. sensors.setResolution(9);
  38. sensors.begin();
  39. sensors.requestTemperatures();
  40. lightMeter.begin(BH1750_CONTINUOUS_HIGH_RES_MODE);
  41. lcd.init();
  42. lcd.backlight();
  43. lcd.begin(16, 2);
  44. lcd.clear();
  45. }
  46.  
  47. void loop(void) {
  48. sensors.requestTemperatures(); //Pobranie temperatury czujnika
  49. double temperature = ((double) sensors.getTempCByIndex(0));
  50. uint16_t lux = lightMeter.readLightLevel();
  51. lcd.setCursor(0, 0);
  52. lcd.print("Temp : ");
  53. lcd.print(temperature);
  54. lcd.print(" ");
  55. lcd.print((char) 223);
  56. lcd.print("C");
  57. lcd.setCursor(0, 1);
  58. lcd.print("Light: ");
  59. lcd.print(lux);
  60. lcd.print(" lx");
  61.  
  62.  
  63. if (targetToOpen < lux) { //dzien
  64. up = true; //jasno
  65. }
  66. if (targetToClose > lux) { // noc
  67. down = true; //ciemno
  68. }
  69. if (debounceButton(button1, 11) == HIGH && button1 == LOW) { //Jeśli przycisk wciśnięty, podnies rolete
  70. button1 = HIGH;
  71. if(!isUp){
  72. digitalWrite(2, LOW);
  73. delay(5000);
  74. Serial.print("Przycisk w gore\n");
  75. digitalWrite(2, HIGH);
  76. isUp=true;
  77. isDown=false;
  78. }
  79.  
  80. } else if (debounceButton(button1, 11) == LOW && button1 == HIGH) {
  81. button1 = LOW;
  82. }
  83. if (debounceButton(button2, 12) == HIGH && button2 == LOW) { //Jeśli przycisk wciśnięty, opusc rolete
  84. button2 = HIGH;
  85. if(!isDown){
  86. digitalWrite(3, LOW);
  87. delay(5000);
  88. Serial.print("Przycisk w dół\n");
  89. digitalWrite(3, HIGH);
  90. isUp=false;
  91. isDown=true;
  92. }
  93. } else if (debounceButton(button2, 12) == LOW && button2 == HIGH) {
  94. button2 = LOW;
  95. }
  96.  
  97. //przelaczanie
  98. if(switchedDOWN){ //przycisk wciśnięty w dół
  99. if(down == true){
  100. nightPassed == true; //jeśli jest jest ciemno, oznacza to, że trwa noc
  101. }
  102. if (nightPassed){
  103. switchedDOWN = false;
  104. nightPassed = false;
  105. }
  106. }else if(switchedUP){
  107. if(up == true){
  108. dayPassed == true;
  109. }
  110. if (dayPassed){
  111. switchedUP = false;
  112. dayPassed= false;
  113. }
  114. }
  115. else{
  116. if(up){ //up=day
  117. if(isUp){ //isUp = wciągnięta roleta do góry
  118. up=false;
  119. }else{
  120. digitalWrite(2, LOW);
  121. delay(5000);
  122. Serial.print("Czujnik w gore\n");
  123. digitalWrite(2, HIGH);
  124. up = false;
  125. isUp=true;
  126. isDown=false;
  127. }
  128.  
  129. }else if(down){
  130. if(isDown){
  131. down=false;
  132. }else{
  133. digitalWrite(3, LOW);
  134. delay(5000);
  135. Serial.print("Czujnik w dol\n");
  136. digitalWrite(3, HIGH);
  137. down = false;
  138. isUp=false;
  139. isDown=true;
  140. }
  141. }
  142. }
  143. }
  144.  
  145. bool debounceButton(bool state, int pin) {
  146. bool now = digitalRead(pin);
  147. if (state != now) {
  148. delay(10);
  149. now = digitalRead(pin);
  150. }
  151. return now;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement