BR add led

This commit is contained in:
2021-02-14 16:45:23 +03:00
parent e156485cb8
commit e1948d3615
2 changed files with 87 additions and 34 deletions

40
BigRoomMyS/include/main.h Normal file
View File

@@ -0,0 +1,40 @@
#ifndef __MAIN__
#define __MAIN__
#include <Arduino.h>
#include <Wire.h>
#include "MHZ19.h"
#define MY_DEBUG
#define MY_RADIO_RF24
#define MY_RF24_CHANNEL (105)
#define MY_RF24_PA_LEVEL RF24_PA_MAX
#define MY_DEFAULT_ERR_LED_PIN (4) // Error led pin
#define MY_DEFAULT_RX_LED_PIN (5) // Receive led pin
#define MY_DEFAULT_TX_LED_PIN (6) // the PCB, on board LED
#include <MySensors.h>
#include <SoftwareSerial.h> // Remove if using HardwareSerial or non-uno compatabile device
#include <HTU21D.h>
#define RX_PIN 8
#define TX_PIN 7
#define BAUDRATE 9600
#define MOTION (7)
#define LED_WHITE (5)
#define LED_BLUE (6)
MHZ19 myMHZ19;
SoftwareSerial mySerial(RX_PIN, TX_PIN);
HTU21D myHTU21D(HTU21D_RES_RH10_TEMP13);
uint8_t periodMinCO2;
uint8_t periodMotion;
uint8_t curDelay;
uint8_t spLight, dbLight;
uint16_t levelBlue, levelWhite;
bool move, lightWhite;
#endif

View File

@@ -1,32 +1,4 @@
#include <Arduino.h> #include "main.h"
#include <Wire.h>
#include <AHT10.h>
#include "MHZ19.h"
#define MY_DEBUG
#define MY_RADIO_RF24
#define MY_RF24_CHANNEL (105)
#define MY_RF24_PA_LEVEL RF24_PA_MAX
#define MY_DEFAULT_ERR_LED_PIN (4) // Error led pin
#define MY_DEFAULT_RX_LED_PIN (5) // Receive led pin
#define MY_DEFAULT_TX_LED_PIN (6) // the PCB, on board LED
#include <MySensors.h>
#include <SoftwareSerial.h> // Remove if using HardwareSerial or non-uno compatabile device
#define RX_PIN 8
#define TX_PIN 7
#define BAUDRATE 9600
#define MOTION (6)
MHZ19 myMHZ19;
SoftwareSerial mySerial(RX_PIN, TX_PIN);
uint8_t periodMin;
uint8_t periodMotion;
uint8_t curDelay;
void presentation(); void presentation();
void sendDataI(MyMessage msg, uint32_t status); void sendDataI(MyMessage msg, uint32_t status);
@@ -66,25 +38,60 @@ void setup() {
Serial.print("Temperature Cal: "); Serial.print("Temperature Cal: ");
Serial.println(myMHZ19.getTempAdjustment()); Serial.println(myMHZ19.getTempAdjustment());
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");*/ Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");*/
periodMin = loadState(0); periodMinCO2 = loadState(0);
if(periodMin == 0) periodMin = 1; if(periodMinCO2 == 0) periodMinCO2 = 1;
periodMotion = loadState(1); periodMotion = loadState(1);
if(periodMotion == 0) periodMotion = 60; if(periodMotion == 0) periodMotion = 60;
spLight = (loadState(2) << 8) + loadState(3);
spLight = (loadState(4) << 8) + loadState(5);
levelBlue = (loadState(6) << 8) + loadState(7);
levelWhite = (loadState(8) << 8) + loadState(9);
myHTU21D.begin();
} }
void loop() { void loop() {
static uint32_t cRun = millis(); static uint32_t cRun = millis();
static uint8_t sec = 0; static uint8_t sec = 0;
static uint8_t minuts = 0; static uint8_t minuts = 0;
float temp, hum;
if(digitalRead(MOTION) > 0){
if(curDelay == -1) sendDataB(msgMove, true);
move = true;
curDelay = periodMotion;
}
uint16_t LightLev = analogRead(A0); uint16_t LightLev = analogRead(A0);
if (LightLev < spLight && move){
analogWrite(LED_BLUE, levelBlue);
}
else if((LightLev > (spLight + dbLight)) || (move == 0)){
analogWrite(LED_BLUE, 0);
}
if(lightWhite)
analogWrite(LED_WHITE, levelWhite);
else
analogWrite(LED_WHITE, 0);
if((cRun + 1000) <= millis()){ //Once per second if((cRun + 1000) <= millis()){ //Once per second
cRun = millis(); cRun = millis();
sec++; sec++;
if(curDelay > 0) curDelay--;
if(curDelay == 0){
sendDataB(msgMove, false);
move = false;
curDelay = -1;
}
if((sec + 2) % 30 == 0){
temp = myHTU21D.readTemperature();
hum = myHTU21D.readCompensatedHumidity();
}
if(sec == 59){ if(sec == 59){
minuts++; minuts++;
sec = 0; sec = 0;
} }
if(minuts == periodMin){ if(minuts == periodMinCO2){
int CO2; // Buffer for CO2 int CO2; // Buffer for CO2
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm) CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
@@ -94,6 +101,12 @@ void loop() {
sendDataI(msgCO2, CO2); sendDataI(msgCO2, CO2);
wait(50); wait(50);
} }
if(temp != HTU21D_ERROR){
sendDataF(msgTemp, temp);
wait(50);
sendDataF(msgHum, hum);
wait(50);
}
sendDataI(msgTimeWork, cRun); sendDataI(msgTimeWork, cRun);
minuts = 0; minuts = 0;
} }
@@ -116,8 +129,8 @@ void receive(const MyMessage &message)
saveState(1, periodMotion); saveState(1, periodMotion);
} }
if((message.sensor == 3) && (message.type == V_VAR5)){ if((message.sensor == 3) && (message.type == V_VAR5)){
periodMin = message.getByte(); periodMinCO2 = message.getByte();
saveState(0, periodMin); saveState(0, periodMinCO2);
} }
} }