@BearWithBeard said in Relay Actuator with momentary (pulse) action:
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON)
Many thanks for coming back @BearWithBeard, its a huge help.
So my logic for correcting the State problem at the bottom would be to do the same calculation to get them in to 0 and 1.
saveState(message.getSensor()-7, message.getBool());
Compiled correctly and then uploaded and it works! Fantastic. The following sketch has 3 dimmers, 4 buttons and 2 relay outputs.
Thank you very much - I'm learning
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - February 15, 2014 - Bruce Lacey
* Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
*
* DESCRIPTION
* This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
* <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
* Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
*
* The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
* The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
* to the LED negative terminal and the MOSFET Source pin is connected to ground.
*
*/
// Button
// The below includes 4 buttons using code examples from here: https://forum.mysensors.org/topic/9538/multiple-switch-inputs/8
// Button
// Relay
// The below includes 2 relays from https://www.mysensors.org/build/relay
// https://forum.mysensors.org/topic/5906/relay-actuator-with-momentary-pulse-action/2
// Relay
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
// Enable RS485 transport layer
#define MY_RS485
// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
#define MY_NODE_ID 4
#include <SPI.h>
#include <MySensors.h>
// Button
#include <Bounce2.h>
//
#define SN "N004"
#define SV "1.0"
#define noLEDs 2
const int LED_Pin[] = {5, 6};
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
static int currentLevel1 = 0; // Current dim level...
static int currentLevel2 = 0; // Current dim level...
MyMessage dimmer1Msg(1, V_DIMMER);
MyMessage light1Msg(1, V_LIGHT);
MyMessage dimmer2Msg(2, V_DIMMER);
MyMessage light2Msg(2, V_LIGHT);
// Button
#define FIRST_BUTTON_ID 3
#define MAX_BUTTON 4
const uint8_t buttonPin[] = {4, 10, 11, 12}; // switch around pins to your desire
Bounce debouncer[MAX_BUTTON];
MyMessage buttonMsg(0, V_TRIPPED);
bool oldButton[MAX_BUTTON] = {false};
// Button
// Relay
#define FIRST_RELAY_ID 7
#define MAX_RELAY 2
const uint8_t relayPin[] = {3, 7}; // Pins of relays
MyMessage relayMsg(0, V_STATUS);
#define RELAY_ON 1
#define RELAY_OFF 0
// Relay
void before()
{
}
/***
* Dimmable LED initialization method
*/
void setup()
{
// LEDS
// Pull the gateway's current dim level - restore light level upon sendor node power-up
for (int sensor=1; sensor<=noLEDs; sensor++){
request( sensor, V_DIMMER );
}
// Button
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i] = Bounce(); // initialize debouncer
debouncer[i].attach(buttonPin[i], INPUT_PULLUP);
debouncer[i].interval(5);
oldButton[i] = debouncer[i].read();
}
// Button
// Relay
for (uint8_t i = 0; i < MAX_RELAY; i++) {
pinMode(relayPin[i], OUTPUT);
//digitalWrite(relayPin, loadState(i)?RELAY_ON:RELAY_OFF);
if (loadState(i) == RELAY_ON) {
digitalWrite(relayPin, RELAY_ON);
wait(500);
digitalWrite(relayPin, RELAY_OFF);
}
}
// Relay
}
void presentation() {
// Register the LED Dimmable Light with the gateway
for (int sensor=1; sensor<=noLEDs; sensor++){
present(sensor, S_DIMMER);
wait(2);
}
// Button
for (int i = 0; i < MAX_BUTTON; i++) { //i < numSensors &&
present(FIRST_BUTTON_ID + i, S_DOOR);
}
// Button
// Relay
for (int i = 0; i < MAX_RELAY; i++) {
present(FIRST_RELAY_ID + i, S_BINARY);
}
// Relay
sendSketchInfo(SN, SV);
}
/***
* Dimmable LED main processing loop
*/
void loop()
{
// Button
bool button[MAX_BUTTON];
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i].update();
button[i] = debouncer[i].read();
if (button[i] != oldButton[i]) {
send(buttonMsg.setSensor(FIRST_BUTTON_ID + i).set( button[i])); // Send tripped value to gw
oldButton[i] = button[i];
}
}
// Button
}
void receive(const MyMessage &message) {
if (message.type == V_LIGHT || message.type == V_DIMMER) {
if (message.sensor == 1) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel1 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel1 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel1 = requestedLevel1 > 100 ? 100 : requestedLevel1;
requestedLevel1 = requestedLevel1 < 0 ? 0 : requestedLevel1;
fadeToLevel1( requestedLevel1, message.sensor );
send(light1Msg.set(currentLevel1 > 0 ? 1 : 0));
send(dimmer1Msg.set(currentLevel1) );}
if (message.sensor == 2) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel2 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel2 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel2 = requestedLevel2 > 100 ? 100 : requestedLevel2;
requestedLevel2 = requestedLevel2 < 0 ? 0 : requestedLevel2;
fadeToLevel2( requestedLevel2, message.sensor );
send(light2Msg.set(currentLevel2 > 0 ? 1 : 0));
send(dimmer2Msg.set(currentLevel2) );}
}
// Relay
if (message.type == V_STATUS) {
if (message.sensor == 7) {
if (message.getBool() == RELAY_ON) {
//digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON);
wait(500);
//digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
digitalWrite(relayPin[message.getSensor()-7], RELAY_OFF);
}
saveState(message.getSensor()-7, message.getBool());
}
if (message.sensor == 8) {
if (message.getBool() == RELAY_ON) {
//digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON);
wait(500);
//digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
digitalWrite(relayPin[message.getSensor()-7], RELAY_OFF);
}
saveState(message.getSensor()-7, message.getBool());
}
}
// Relay
}
/***
* This method provides a graceful fade up/down effect
*/
void fadeToLevel1( int toLevel1, int ledid1 ) {
int delta1 = ( toLevel1 - currentLevel1 ) < 0 ? -1 : 1;
while ( currentLevel1 != toLevel1 ) {
currentLevel1 += delta1;
analogWrite(LED_Pin[ledid1-1], (int)(currentLevel1 / 100. * 255) );
wait( FADE_DELAY );
}
}
void fadeToLevel2( int toLevel2, int ledid2 ) {
int delta2 = ( toLevel2 - currentLevel2 ) < 0 ? -1 : 1;
while ( currentLevel2 != toLevel2 ) {
currentLevel2 += delta2;
analogWrite(LED_Pin[ledid2-1], (int)(currentLevel2 / 100. * 255) );
wait( FADE_DELAY );
}
}