Kor fade out

This commit is contained in:
2021-02-14 16:46:12 +03:00
parent e1948d3615
commit df47c18ed3
2 changed files with 23 additions and 7 deletions

View File

@@ -23,7 +23,7 @@
#include <SoftwareSerial.h>
unsigned long cRun;
int cSec, adc, move, oldmov, minLight, minLightDB, LightInt, timeDelay, curDelay;
int cSec, adc, move, oldmov, minLight, minLightDB, LightInt, timeDelay, curDelay, fadeTime;
uint8_t sdsPeriod;
bool lamp;
//float avgL;
@@ -67,6 +67,7 @@ void setup()
LightInt = (loadState(4) << 8) + loadState(5);
sdsPeriod = loadState(6);
timeDelay = (loadState(8) << 8) + loadState(9);
fadeTime = (loadState(10) << 8) + loadState(11);
curDelay = -1;
Serial.print(F("Min Light"));Serial.println(minLight);
Serial.print(F("Min LightDB"));Serial.println(minLightDB);
@@ -96,6 +97,7 @@ void presentation()
void loop()
{
float p25, p10;
uint32_t curTime;
//move = digitalRead(A1);
if(digitalRead(A1) > 0){
@@ -125,7 +127,8 @@ void loop()
}
}
else if((adc > (minLight + minLightDB)) || (move == 0)){
analogWrite(LAMP_OUT, 0);
//analogWrite(LAMP_OUT, 0);
curDelay = 2;
if(lamp == true){
Serial.println("Lamp OFF");
Serial.print("ADC: ");Serial.print(adc);
@@ -136,6 +139,7 @@ void loop()
sendData(msgLamp, false);
//send(msgLamp.set(false));
lamp = false;
curTime = millis() + fadeTime * 1000 ;
}
}
if(cRun + 999 < millis()){
@@ -146,28 +150,33 @@ void loop()
curDelay = -1;
}
if(curDelay > 0) curDelay--;
if(curDelay == 1) curTime = cRun + fadeTime * 1000;
int error = sds.read(&p25, &p10);
//Serial.print("ADC: ");Serial.print(adc);Serial.print(", Move: ");Serial.println(move);
if (!error) {
Serial.println(millis()/1000);
Serial.println("P2.5: " + String(p25));
Serial.println("P10: " + String(p10));
sendData(msgHum25, p25);
//send(msgHum25.set(p25, 1));
wait(100);
sendData(msgHum10, p10);
//send(msgHum10.set(p10, 1));
}
if (++cSec > 19){
cSec = 0;
wait(100);
sendData(msgLightLev, adc);
//send(msgLightLev.set(adc));
wait(100);
uint32_t ms = millis();
send(msgMillis.set(ms));
}
}
if(lamp && curTime > millis()){
uint8_t ll = int(LightInt - LightInt * (1 - curTime - millis() / float(fadeTime * 1000)));
analogWrite(LAMP_OUT, ll);
if(curTime - millis() < 10){
lamp = false;
analogWrite(LAMP_OUT, 0);
}
}
}
void receive(const MyMessage &message)
@@ -207,6 +216,13 @@ void receive(const MyMessage &message)
Serial.print("time period:");
Serial.println(timeDelay);
}
if((message.sensor == 5) && (message.type == V_VAR3)){
fadeTime = message.getInt();
saveState(10, (fadeTime>>8) & 0xFF);
saveState(11, fadeTime & 0xFF);
Serial.print("fade period:");
Serial.println(fadeTime);
}
}
////////////////// sends work mode command to SDS011 //////////////////////