@AWI
thanks, still small problems to figure out though.
when I send a command to rollershutter 1 then rollershutter 2 receives the command. I can not control shutter 1, only 2 and 3 (with the buttons of 1 and 2)
output in serial monitor is right and physical buttons work the right way too
is there a problem in the registration or am I missing something (again)?
the physical buttons work the right way.
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE
#include <MySensors.h>
#include <SPI.h>
// Rollladensteuerung by jurs for German Arduino-Forum
#define BUTTONPRESSED LOW
#define RELAYACTIVE LOW
MyMessage sensorMsg0(0, V_DIMMER);
MyMessage sensorMsg1(1, V_DIMMER);
MyMessage sensorMsg2(2, V_DIMMER);
enum {NOACTION, UPACTION, DOWNACTION};
struct roll_t {
byte downPin;
byte upPin;
boolean downPressed;
boolean upPressed;
byte action;
unsigned long actionStarttime;
unsigned long actionTimeout;
byte relayOnePin;
byte relayTwoPin;
};
roll_t rollladen[] = {
{2, 3, false, false, NOACTION, 0, 60000L, A0, A1}, // Button-Pins 2, 3, Relay-Pins A0, A1, Timeout 60 secons
{4, 5, false, false, NOACTION, 0, 45000L, A2, A3}, // Button-Pins 4, 5, Relay-Pins A2, A3, Timeout 45 secons
{6, 7, false, false, NOACTION, 0, 45000L, A4, A5}, // Button-Pins 6, 7, Relay-Pins A4, A5, Timeout 45 secons
};
#define ANZAHLROLLLADEN sizeof(rollladen)/sizeof(rollladen[0])
void setup()
{
Serial.begin(115200);
for (int i = 0; i < ANZAHLROLLLADEN; i++)
{
if (BUTTONPRESSED == HIGH)
{
pinMode(rollladen[i].downPin, INPUT);
pinMode(rollladen[i].upPin, INPUT);
}
else
{
pinMode(rollladen[i].downPin, INPUT_PULLUP);
pinMode(rollladen[i].upPin, INPUT_PULLUP);
}
if (RELAYACTIVE == LOW)
{
digitalWrite(rollladen[i].relayOnePin, HIGH);
digitalWrite(rollladen[i].relayTwoPin, HIGH);
}
pinMode(rollladen[i].relayOnePin, OUTPUT);
pinMode(rollladen[i].relayTwoPin, OUTPUT);
}
}
void switchRelay(byte i)
{
Serial.print("Schalte Rolladen-");
Serial.print(i);
switch (rollladen[i].action)
{
case NOACTION: // STOP
digitalWrite(rollladen[i].relayOnePin, !RELAYACTIVE);
digitalWrite(rollladen[i].relayTwoPin, !RELAYACTIVE);
Serial.println(" STOP");
break;
case UPACTION: // UP
digitalWrite(rollladen[i].relayOnePin, RELAYACTIVE);
digitalWrite(rollladen[i].relayTwoPin, !RELAYACTIVE);
Serial.println(" UP");
break;
case DOWNACTION: // DOWN
digitalWrite(rollladen[i].relayOnePin, !RELAYACTIVE);
digitalWrite(rollladen[i].relayTwoPin, RELAYACTIVE);
Serial.println(" DOWN");
break;
}
}
void loop(void)
{
byte state;
for (int i = 0; i < ANZAHLROLLLADEN; i++)
{
// Prüfen, ob Down gedrückt wurde
state = digitalRead(rollladen[i].downPin);
if (BUTTONPRESSED == LOW) state = !state;
if (state && !rollladen[i].downPressed)
{
switch (rollladen[i].action)
{
case NOACTION: rollladen[i].action = DOWNACTION;
rollladen[i].actionStarttime = millis();
switchRelay(i);
break;
default: if (rollladen[i].action != NOACTION)
{
rollladen[i].action = NOACTION;
switchRelay(i);
}
}
}
rollladen[i].downPressed = state;
// Prüfen, ob Up gedrückt wurde
state = digitalRead(rollladen[i].upPin);
if (BUTTONPRESSED == LOW) state = !state;
if (state && !rollladen[i].upPressed)
{
switch (rollladen[i].action)
{
case NOACTION: rollladen[i].action = UPACTION;
rollladen[i].actionStarttime = millis();
switchRelay(i);
break;
default: if (rollladen[i].action != NOACTION)
{
rollladen[i].action = NOACTION;
switchRelay(i);
}
}
}
rollladen[i].upPressed = state;
// Prüfen auf Timeout
if (rollladen[i].action != NOACTION && millis() - rollladen[i].actionStarttime >= rollladen[i].actionTimeout)
{
rollladen[i].action = NOACTION;
switchRelay(i);
}
}
delay(5); // kleines Delay zum Entprellen der Buttons
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Test", "1.0");
// Fetch relay status
for (int sensor = 1, pin = A0; sensor <= ANZAHLROLLLADEN; sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
present(sensor, S_COVER);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
}
}
void receive(const MyMessage &message) {
byte state;
int i = message.sensor ;
if (message.type == V_UP) {
{
switch (rollladen[i].action)
{
case NOACTION: rollladen[i].action = UPACTION;
rollladen[i].actionStarttime = millis();
switchRelay(i);
break;
default: if (rollladen[i].action != NOACTION)
{
rollladen[i].action = NOACTION;
switchRelay(i);
}
}
}
rollladen[i].upPressed = state;
Serial.println("going up:" + String(message.type));
}
else if (message.type == V_DOWN) {
{
switch (rollladen[i].action)
{
case NOACTION: rollladen[i].action = DOWNACTION;
rollladen[i].actionStarttime = millis();
switchRelay(i);
break;
default: if (rollladen[i].action != NOACTION)
{
rollladen[i].action = NOACTION;
switchRelay(i);
}
}
}
rollladen[i].downPressed = state;
Serial.println("going down:" + String(message.type));
}
else if (message.type == V_STOP) {
{
switch (rollladen[i].action)
{
case DOWNACTION: rollladen[i].action = NOACTION;
rollladen[i].actionStarttime = millis();
switchRelay(i);
break;
case UPACTION: rollladen[i].action = NOACTION;
rollladen[i].actionStarttime = millis();
switchRelay(i);
break;
}
}
Serial.println("stopping:" + String(message.type));
}
}
hope it's clear what I mean. It is a bit weird...