@hard-shovel said in Water Sensor:
typical types are H11AA1 or LTV814 etc
Thank you! Seems like the best way to for me than. Will order and wait for them to arrive
@hard-shovel said in Water Sensor:
typical types are H11AA1 or LTV814 etc
Thank you! Seems like the best way to for me than. Will order and wait for them to arrive
@hard-shovel said in Water Sensor:
Your diagram is reversed as you need to drive the led in the optocoupler from the relay coil connections.
Your ingenious scheme worked like a charm. Thank you!
Don't want to push but in such a rare possibility when you have access to such a marvelous help...
The fan is of asynchronous type with 3 wires (neutral and 2 speeds). Currently i have just connected the output for speed1 to relay NC contact and speed2 to fan to NO contact. So it looks like this.

With such connection i still have the manual switch to off - 1st speed. But with such connection i cannot use manual switch on humidifier to choose the second switch. I have already use a multi meter to find out contacts
i was thinking of making my sensors node completely parallel to the board and use 4n35 to sense 220V AC on different rotary switch positions and drive relays appropriately. All the schematics i found on the web use bridge rectifier. Wonder if there is any way to go without one - and if not - what to order from aliexpress?
@neverdie Do o really need the TLP222A as i already have 4N35 lying around
@hard-shovel said in Water Sensor:
If you have the arduino analogue pin connected to Pin3, where do you have the arduino GND connected?
Well actually nowhere :) Thought that as the analogue pin is pulled to ground that will suffice.
Was happy at first but still seems the readings are erratic. So will follow your great advice with the optocoupler.
Do i get the connection scheme correctly?

Ok took the measurements once again on the led. This time measuriong only LED pins and not the live neutral wire. Between 1 and 3 gave me 0,1AC and 1,8DC when red and 0,1AC and 0,1DC when green. So just connected the anlogue pin of arduino to pin3 of LED and it works. Readings 19-70 when red led turned on. And 0-3 when off.
Just wonder how this led works from AC and is it still a good advice to put optocoupler into it?
@hard-shovel said in Water Sensor:
Wow! Thank you! You just made a lot of stuff clear.
On the upper left part of the pannel - is actually a status LED. Which turns red when no water detected and green when it is. So i believe i should just dump the idea of getting anything from the water sensor and turn my view on the LED? This is the backside of the board.
AC is totally confusing me. Especially the LED part. When trying to measure ac voltage with my multimeter i have seen something like 30V between the live neutral and one of the LED legs. So any recommendation is very appreciated

Nope. Will it help in any way?
The chip on the original circuit reads HEF4093BT
I have an Electrolux air washer which i decided to convert to a smart one. So i have already added necessary relays and can control fan speed and power... One thing left. It has a water control built in. I want to keep the original functionality of the board so i need to connect to the water sensor in parallel.
This water control consists of two metal pieces submerged in water. And 2 wires going to the control board (from now on let's call them water wires)
I have taken the pic of the board and highlighted the part where this water wires connect. This circuit is driven by a 9V ac-ac transformer. Multimeter shows around 1,5-3V of AC current on the metal plates.

So going the easy way i've just connected to the metal plates +3,3V from arduino and then to the analog pin (pulled to ground via 10k). Actually it almost worked. Had reading of around 158 when water present and 60 when not.
But i lost the functionality of the control board. With +3,3V from arduino connected it always "detected" water even when there was none. (i have tried switching sides with no luck).
So i removed the 3,3V from arduino and just left the analogue pin connected to one of the metal plates. This gives me fluctuation reading from 2 to 30.
So i wonder what is the correct way to go?
Thanks. Will await eagerly for v1.8 as it seems this is the only great RGB code left for mysensors
Need a node with a button input (doesn't do anything to relay state) and a single relay. As this is connected to Home Assistant have to provide feedback and present the relay.
The problem is with the code below it just starts sending the relay state in a loop without stopping.
mys-out/67/1/1/0/2 1
Tried everything and seems got stuck somewhere
#define SN "MYS Vannaya Gidro"
#define SV "2.1.1"
//System settings
#define MY_NODE_ID 67
#define MY_RADIO_NRF24
#define MY_DEBUG
#include <Bounce2.h>
#include <math.h>
#include <MySensors.h>
#include <SPI.h>
#include <Wire.h>
//SETUP PINS
#define BUTTON_PIN1 7
const int MQ_Pin = A3;
#define RELAY_1 5
//Define CHILD_IDS
#define CHILD_ID_RELAY 1
#define CHILD_ID_BUT1 2
#define CHILD_ID_MQ 3
//Relay VARS
#define RELAY_ON 1
#define RELAY_OFF 0
//SENSORS
long SENSOR_Millis = 0;
long SENSOR_interval = 60000;
//Buttons
Bounce debouncer1 = Bounce();
int oldValue1=-1;
bool state;
//Messages
MyMessage msgRelay(CHILD_ID_RELAY, V_STATUS);
MyMessage msgMQ(CHILD_ID_MQ, V_LEVEL);
MyMessage msgBut1(CHILD_ID_BUT1,V_TRIPPED);
//Initial for HA
bool initialValueSent = false;
void before()
{
pinMode(BUTTON_PIN1, INPUT_PULLUP);
debouncer1.attach(BUTTON_PIN1);
debouncer1.interval(5);
pinMode(RELAY_1, OUTPUT);
digitalWrite(RELAY_1, loadState(1)?RELAY_ON:RELAY_OFF);
}
void setup()
{ Serial.begin(115200); }
void presentation()
{
sendSketchInfo(SN, SV);
wait(50);
present(CHILD_ID_RELAY, S_BINARY);
wait(50);
present(CHILD_ID_MQ, S_AIR_QUALITY);
wait(50);
present(CHILD_ID_BUT1, S_WATER_LEAK);
}
void loop()
{
if (!initialValueSent)
{
send(msgRelay.set(loadState(1)?RELAY_OFF:RELAY_ON));
}
unsigned long SENSOR_Current_Millis = millis();
if((unsigned long)(SENSOR_Current_Millis - SENSOR_Millis) >= SENSOR_interval)
{
SENSOR_Millis = SENSOR_Current_Millis;
float mq_reading = analogRead(MQ_Pin);
send(msgMQ.set(mq_reading, 1));
}
debouncer1.update();
int value = debouncer1.read();
if (value != oldValue1)
{
// Send in the new value
send(msgBut1.set(value==HIGH ? 1 : 0));
oldValue1 = value;
}
}
void receive(const MyMessage &message)
{
if (message.type==V_STATUS)
{
if (!initialValueSent)
{
initialValueSent = true;
}
// Update relay state to HA
state = message.getBool();
digitalWrite(RELAY_1, state?RELAY_ON:RELAY_OFF);
saveState(CHILD_ID_RELAY, state);
send(msgRelay.set(state?RELAY_ON:RELAY_OFF));
}
}
@bjacobse There is nothing difficult in setting it up. Just connect the irda to the 3rd PWM leg
ESP is the best solution although couldn't find the code with sensors (only clean/dock commands). I recently made a post on HomeAssistant forum - how wemos D1 fits nicely inside. You only need a buck converter and 4 cat5 wires soldered. And 5V serial output is not such a big problem as you need a voltage divider only on rx pin (didn't put it)
P.S. But then one of those "machine revolt weeks happened" so my esproomba didn't respond and then i burned it down. Next day my RPi3 went down with HA and lots of goodies. And today ESXi...
@jkandasa Thank you!
One more question. Now need to use a 3,3V arduino Pro (chinese version) (Nano and 5V version flashed via the uno without a hitch)
This is what i put into boards.txt The MYSBootloader_8Mhz.hex is from the dev branch
proMYSBL8.name=ATmega328 8Mhz MYSBootloader
proMYSBL8.name=ATmega328 internal 8Mhz with MYSBootloader
proMYSBL8.upload.tool=avrdude
proMYSBL8.upload.protocol=arduino
proMYSBL8.upload.maximum_size=30720
proMYSBL8.upload.maximum_data_size=2048
proMYSBL8.upload.speed=57600
proMYSBL8.bootloader.tool=avrdude
proMYSBL8.bootloader.low_fuses=0xE2
proMYSBL8.bootloader.high_fuses=0xDA
proMYSBL8.bootloader.extended_fuses=0x06
proMYSBL8.bootloader.unlock_bits=0x3F
proMYSBL8.bootloader.lock_bits=0xFF
proMYSBL8.bootloader.file=MySensors/MYSBootloader_8Mhz.hex
proMYSBL8.build.mcu=atmega328p
proMYSBL8.build.f_cpu=8000000L
proMYSBL8.build.board=AVR_UNO
proMYSBL8.build.core=arduino
proMYSBL8.build.variant=standard
Tried to flash the mysbootloader but get the following error
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM7 -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0x06:m -Uhfuse:w:0xDA:m -Ulfuse:w:0xE2:m
avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch
System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"
Using Port : COM7
Using Programmer : stk500v1
Overriding Baud Rate : 19200
AVR Part : ATmega328P
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PC2
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff
flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
Programmer Type : STK500
Description : Atmel STK500 Version 1.x firmware
Hardware Version: 2
Firmware Version: 1.18
Topcard : Unknown
Vtarget : 0.0 V
Varef : 0.0 V
Oscillator : Off
SCK period : 0.1 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.03s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip
avrdude: reading input file "0x3F"
avrdude: writing lock (1 bytes):
Writing | ################################################## | 100% 0.01s
avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3F:
avrdude: load data lock data from input file 0x3F:
avrdude: input file 0x3F contains 1 bytes
avrdude: reading on-chip lock data:
Reading | ################################################## | 100% 0.01s
avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0x06"
avrdude: writing efuse (1 bytes):
Writing | ***failed;
################################################## | 100% 0.07s
avrdude: 1 bytes of efuse written
avrdude: verifying efuse memory against 0x06:
avrdude: load data efuse data from input file 0x06:
avrdude: input file 0x06 contains 1 bytes
avrdude: reading on-chip efuse data:
Reading | ################################################## | 100% 0.01s
avrdude: verifying ...
avrdude: WARNING: invalid value for unused bits in fuse "efuse", should be set to 1 according to datasheet
This behaviour is deprecated and will result in an error in future version
You probably want to use 0xfe instead of 0x06 (double check with your datasheet first).
avrdude: 1 bytes of efuse verified
avrdude: reading input file "0xDA"
avrdude: writing hfuse (1 bytes):
Writing | ################################################## | 100% 0.01s
avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xDA:
avrdude: load data hfuse data from input file 0xDA:
avrdude: input file 0xDA contains 1 bytes
avrdude: reading on-chip hfuse data:
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -cstk500v1 -PCOM7 -b19200 -Uflash:w:C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/MySensors/MYSBootloader_8Mhz.hex:i -Ulock:w:0xFF:m
avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
Reading | ################################################## | 100% 0.01s
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch
avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"
avrdude: reading input file "0xE2"
avrdude: writing lfuse (1 bytes):
Writing | ################################################## | 100% 0.01s
avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xE2:
avrdude: load data lfuse data from input file 0xE2:
avrdude: input file 0xE2 contains 1 bytes
avrdude: reading on-chip lfuse data:
Reading | ################################################## | 100% 0.01s
avrdude: verifying ...
avrdude: 1 bytes of lfuse verified
avrdude done. Thank you.
Using Port : COM7
Using Programmer : stk500v1
Overriding Baud Rate : 19200
AVR Part : ATmega328P
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PC2
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff
flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
Programmer Type : STK500
Description : Atmel STK500 Version 1.x firmware
Hardware Version: 2
Firmware Version: 1.18
Topcard : Unknown
Vtarget : 0.0 V
Varef : 0.0 V
Oscillator : Off
SCK period : 0.1 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/MySensors/MYSBootloader_8Mhz.hex"
avrdude: writing flash (32754 bytes):
Writing | ################################################## | 100% 0.00s
avrdude: 32754 bytes of flash written
avrdude: verifying flash memory against C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/MySensors/MYSBootloader_8Mhz.hex:
avrdude: load data flash data from input file C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/MySensors/MYSBootloader_8Mhz.hex:
avrdude: input file C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/MySensors/MYSBootloader_8Mhz.hex contains 32754 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.00s
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x7800
0x00 != 0x11
avrdude: verification error; content mismatch
avrdude done. Thank you.
Error while burning bootloader.
@parachutesj Mind posting your finished code?
Also wonder what is the correct MQTT payload to start for example cleaning?
Upon start i see the following
mys-out/15/255/3/0/11 MYS Roomba
mys-out/15/255/3/0/12 2.1
mys-out/15/0/0/0/3 Roomba-Stop
mys-out/15/1/0/0/3 Roomba-Dock
mys-out/15/2/0/0/3 Roomba-Clean
mys-out/15/3/0/0/3 Roomba-Spot
mys-out/15/4/0/0/3 Roomba-Max
mys-out/15/5/0/0/3 Roomba-Power
mys-out/15/6/0/0/3 Roomba-Play
mys-out/15/100/0/0/6 Roomba-Batt-Temp
mys-out/15/101/0/0/39 Roomba-Batt-Proc
mys-out/15/102/0/0/30 Roomba-Batt-Volt
mys-out/15/103/0/0/30 Roomba-Batt-mAH
mys-out/15/104/0/0/36 Roomba-Batt-CHt
mys-out/15/105/0/0/36 Roomba-Batt-CHs
But publishing to
mys-in/15/2/1/0/3
mys-in/15/2/1/0/2
with payload 1 doesn't do anything
@gohan So is it first reboot. Or start flashing - reboot?
Well after much trial and error - seems that pin5 is behaving erratically. Don't know why. Rewired to an external dimmer board. Changed arduinos. Nothing helps which is extremly strange
No it's powered off mains so no sleep functions added
Nano also doesn't need any fuses. So everything works. Except to begin the flash procedure i have to unplug and plug the node. Is this intended behaviour? Otherwise i only see in MQTT one line of code and no response from node
@BulldogLowell Read about them. But still seems i address the array correctly. Or i'm wrong?
Had a code which was working for a long time. Decided to add another dimmer. And now having hard time. Seems to work light switches on - then never goes off. One dimmer is on 100% of the time. Any help much appreciated. Added comments on changed lines //ADDED
Guess screwed up with currentLevel but not sure
#define SN "MYS Kitchen Hood"
#define SV "2.1"
//System settings
#define MY_RADIO_NRF24
#define MY_RF24_CE_PIN 8 //ADDED
// Include all the libraries
#include <MySensors.h>
#include <SPI.h>
#include <DHT.h>
#include <math.h>
#include <Wire.h>
#include <Bounce2.h>
#define MY_NODE_ID 71
//DEFINE CHILD_IDS
#define DIMMER_NODE_1 0
#define DIMMER_NODE_2 1
#define DIMMER_NODE_3 2
#define DIMMER_NODE_4 3 //ADDED
#define CHILD_ID_HUM 4
#define CHILD_ID_TEMP 5
#define CHILD_ID_MQ 6
#define CHILD_BUT1 7
#define CHILD_BUT2 8
#define CHILD_BUT3 9
#define CHILD_BUT4 10
//BUTTONS
#define BUTTON_PIN1 A0
#define BUTTON_PIN2 A1
#define BUTTON_PIN3 A2
#define BUTTON_PIN4 A3
//SENSORS
#define DHT_PIN 8
const int MQ_Pin = A4;
//DIMMER
#define LED_PIN_1 3
#define LED_PIN_2 5
#define LED_PIN_3 6
#define LED_PIN_4 9 //ADDED
#define FADE_DELAY 5
DHT dht;
//BUTTONS
Bounce debouncer_1 = Bounce();
Bounce debouncer_2 = Bounce();
Bounce debouncer_3 = Bounce();
Bounce debouncer_4 = Bounce();
int oldValue_1=-1;
int oldValue_2=-1;
int oldValue_3=-1;
int oldValue_4=-1;
bool state1;
bool state2;
bool state3;
bool state4;
MyMessage msgbut1(CHILD_BUT1,V_STATUS);
MyMessage msgbut2(CHILD_BUT2,V_STATUS);
MyMessage msgbut3(CHILD_BUT3,V_STATUS);
MyMessage msgbut4(CHILD_BUT4,V_STATUS);
//SENSORS
float lastTemp;
float lastHum;
float hum_floa;
float last_mq_reading;
long Millis = 0;
long Millis_interval = 30000;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgMQ(CHILD_ID_MQ, V_LEVEL);
//DIMMER
//byte currentLevel[3] = {0,0,0};
byte currentLevel[4] = {0,0,0,0}; //added
MyMessage dimmerMsg0(0, V_PERCENTAGE);
MyMessage lightMsg0(0, V_STATUS);
MyMessage dimmerMsg1(1, V_PERCENTAGE);
MyMessage lightMsg1(1, V_STATUS);
MyMessage dimmerMsg2(2, V_PERCENTAGE);
MyMessage lightMsg2(2, V_STATUS);
MyMessage dimmerMsg3(3, V_PERCENTAGE); //ADDED
MyMessage lightMsg2(3, V_STATUS); //ADDED
void before()
{
dht.setup(DHT_PIN);
analogWrite( LED_PIN_1, 0);
analogWrite( LED_PIN_2, 0);
analogWrite( LED_PIN_3, 0);
analogWrite( LED_PIN_4, 0); //ADDED
pinMode(BUTTON_PIN1,INPUT);
digitalWrite(BUTTON_PIN1, HIGH);
pinMode(BUTTON_PIN2,INPUT);
digitalWrite(BUTTON_PIN2, HIGH);
pinMode(BUTTON_PIN3,INPUT);
digitalWrite(BUTTON_PIN3, HIGH);
pinMode(BUTTON_PIN4,INPUT);
digitalWrite(BUTTON_PIN4, HIGH);
debouncer_1.attach(BUTTON_PIN1);
debouncer_1.interval(5);
debouncer_2.attach(BUTTON_PIN2);
debouncer_2.interval(5);
debouncer_3.attach(BUTTON_PIN3);
debouncer_3.interval(5);
debouncer_4.attach(BUTTON_PIN4);
debouncer_4.interval(5);
}
void setup()
{ }
void presentation()
{
sendSketchInfo(SN, SV);
present(CHILD_ID_HUM, S_HUM);
present(CHILD_ID_TEMP, S_TEMP);
present(CHILD_ID_MQ, S_AIR_QUALITY);
present( DIMMER_NODE_1, S_DIMMER );
send(dimmerMsg0.set(0));
present( DIMMER_NODE_2, S_DIMMER );
send(dimmerMsg1.set(0));
present( DIMMER_NODE_3, S_DIMMER );
send(dimmerMsg2.set(0));
present( DIMMER_NODE_4, S_DIMMER ); //ADDED
send(dimmerMsg3.set(0)); //ADDED
present(CHILD_BUT1, S_BINARY);
present(CHILD_BUT2, S_BINARY);
present(CHILD_BUT3, S_BINARY);
present(CHILD_BUT4, S_BINARY);
}
void loop()
{
int value_but_1 = debouncer_1.read();
int value_but_2 = debouncer_2.read();
int value_but_3 = debouncer_3.read();
int value_but_4 = debouncer_4.read();
//DHT+MQ
unsigned long Current_Millis = millis();
if((unsigned long)(Current_Millis - Millis) >= Millis_interval)
{
Millis = Current_Millis;
delay(dht.getMinimumSamplingPeriod());
float temperature = dht.getTemperature();
float humidity = dht.getHumidity();
float mq_reading = analogRead(MQ_Pin);
if (isnan(temperature))
{Serial.println("Failed reading temperature from DHT");}
if (isnan(humidity))
{Serial.println("Failed reading humidity from DHT");}
if (isnan(mq_reading))
{ Serial.println("Failed mq_reading"); }
else
{
send(msgTemp.set(temperature, 1));
send(msgHum.set(humidity, 1));
send(msgMQ.set(mq_reading, 1));
}
}
//BUTTONS
debouncer_1.update();
if (value_but_1 != oldValue_1)
{
if ( value_but_1==0)
{
state1 = !state1;
send(msgbut1.set(state1));
}
oldValue_1 = value_but_1;
}
debouncer_2.update();
if (value_but_2 != oldValue_2)
{
if ( value_but_2==0)
{
state2 = !state2;
send(msgbut2.set(state2));
}
oldValue_2 = value_but_2;
}
debouncer_3.update();
if (value_but_3 != oldValue_3)
{
if ( value_but_3==0)
{
state3 = !state3;
send(msgbut3.set(state3));
}
oldValue_3 = value_but_3;
}
debouncer_4.update();
if (value_but_4 != oldValue_4)
{
if ( value_but_4==0)
{
state4 = !state4;
send(msgbut4.set(state4));
}
oldValue_4 = value_but_4;
}
}
//DIMMER
void receive(const MyMessage &message)
{
if (message.type == V_STATUS || message.type == V_PERCENTAGE)
{
int requestedLevel = atoi( message.data );
requestedLevel *= ( message.type == V_STATUS ? 100 : 1 );
requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
fadeToLevel( requestedLevel, message.sensor);
switch(message.sensor)
{
case 0:
send(lightMsg0.set(currentLevel[0] > 0 ? 1 : 0));
send( dimmerMsg0.set(currentLevel[0]) );
break;
case 1:
send(lightMsg1.set(currentLevel[1] > 0 ? 1 : 0));
send( dimmerMsg1.set(currentLevel[1]) );
break;
case 2:
send(lightMsg2.set(currentLevel[2] > 0 ? 1 : 0));
send( dimmerMsg2.set(currentLevel[2]) );
break;
//ADDED CASE 3
case 3:
send(lightMsg3.set(currentLevel[3] > 0 ? 1 : 0));
send( dimmerMsg3.set(currentLevel[3]) );
break;
}
}
}
void fadeToLevel( int toLevel, byte sensorId )
{
int delta = ( toLevel - currentLevel[sensorId] ) < 0 ? -1 : 1;
while ( currentLevel[sensorId] != toLevel )
{
currentLevel[sensorId] += delta;
switch(sensorId)
{
case 0:
analogWrite( LED_PIN_1, (int)(currentLevel[sensorId] / 100. * 255) );
break;
case 1:
analogWrite( LED_PIN_2, (int)(currentLevel[sensorId] / 100. * 255) );
break;
case 2:
analogWrite( LED_PIN_3, (int)(currentLevel[sensorId] / 100. * 255) );
break;
//ADDED CASE3
case 3:
analogWrite( LED_PIN_4, (int)(currentLevel[sensorId] / 100. * 255) );
break;
}
delay( FADE_DELAY );
}
}