Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <BH1750.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #define ONE_WIRE_BUS_1 2
- #define ONE_WIRE_BUS_2 4
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- OneWire oneWire_out(ONE_WIRE_BUS_2);
- DallasTemperature sensors( & oneWire_out);
- BH1750 lightMeter(0x23);
- int targetToOpen = 1000;
- int targetToClose = 10;
- bool nightPassed = true;
- bool dayPassed = true;
- bool switchedUP = false;
- bool switchedDOWN = false;
- bool button1 = HIGH;
- bool button2 = HIGH;
- bool up = false;
- bool down= false;
- bool isDown = false;
- bool isUp = false;
- void setup(void) {
- pinMode(11, INPUT_PULLUP); //Przycisk jako wejście
- pinMode(12, INPUT_PULLUP); //Przycisk jako wejście
- pinMode(2, OUTPUT); //Przekaznik jako wyjscie -> gora
- digitalWrite(2, HIGH); //Ustawienie stanu niskiego na procie 8
- pinMode(3, OUTPUT); //Przekaznik jako wyjscie -> dol
- digitalWrite(3, HIGH); //Ustawienie stanu niskiego na procie 8
- Serial.begin(9600); // po co?
- sensors.setResolution(9);
- sensors.begin();
- sensors.requestTemperatures();
- lightMeter.begin(BH1750_CONTINUOUS_HIGH_RES_MODE);
- lcd.init();
- lcd.backlight();
- lcd.begin(16, 2);
- lcd.clear();
- }
- void loop(void) {
- sensors.requestTemperatures(); //Pobranie temperatury czujnika
- double temperature = ((double) sensors.getTempCByIndex(0));
- uint16_t lux = lightMeter.readLightLevel();
- lcd.setCursor(0, 0);
- lcd.print("Temp : ");
- lcd.print(temperature);
- lcd.print(" ");
- lcd.print((char) 223);
- lcd.print("C");
- lcd.setCursor(0, 1);
- lcd.print("Light: ");
- lcd.print(lux);
- lcd.print(" lx");
- if (targetToOpen < lux) { //dzien
- up = true; //jasno
- }
- if (targetToClose > lux) { // noc
- down = true; //ciemno
- }
- if (debounceButton(button1, 11) == HIGH && button1 == LOW) { //Jeśli przycisk wciśnięty, podnies rolete
- button1 = HIGH;
- if(!isUp){
- digitalWrite(2, LOW);
- delay(5000);
- Serial.print("Przycisk w gore\n");
- digitalWrite(2, HIGH);
- isUp=true;
- isDown=false;
- }
- } else if (debounceButton(button1, 11) == LOW && button1 == HIGH) {
- button1 = LOW;
- }
- if (debounceButton(button2, 12) == HIGH && button2 == LOW) { //Jeśli przycisk wciśnięty, opusc rolete
- button2 = HIGH;
- if(!isDown){
- digitalWrite(3, LOW);
- delay(5000);
- Serial.print("Przycisk w dół\n");
- digitalWrite(3, HIGH);
- isUp=false;
- isDown=true;
- }
- } else if (debounceButton(button2, 12) == LOW && button2 == HIGH) {
- button2 = LOW;
- }
- //przelaczanie
- if(switchedDOWN){ //przycisk wciśnięty w dół
- if(down == true){
- nightPassed == true; //jeśli jest jest ciemno, oznacza to, że trwa noc
- }
- if (nightPassed){
- switchedDOWN = false;
- nightPassed = false;
- }
- }else if(switchedUP){
- if(up == true){
- dayPassed == true;
- }
- if (dayPassed){
- switchedUP = false;
- dayPassed= false;
- }
- }
- else{
- if(up){ //up=day
- if(isUp){ //isUp = wciągnięta roleta do góry
- up=false;
- }else{
- digitalWrite(2, LOW);
- delay(5000);
- Serial.print("Czujnik w gore\n");
- digitalWrite(2, HIGH);
- up = false;
- isUp=true;
- isDown=false;
- }
- }else if(down){
- if(isDown){
- down=false;
- }else{
- digitalWrite(3, LOW);
- delay(5000);
- Serial.print("Czujnik w dol\n");
- digitalWrite(3, HIGH);
- down = false;
- isUp=false;
- isDown=true;
- }
- }
- }
- }
- bool debounceButton(bool state, int pin) {
- bool now = digitalRead(pin);
- if (state != now) {
- delay(10);
- now = digitalRead(pin);
- }
- return now;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement