// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached //#define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 // Set LOW transmit power level as default, if you have an amplified NRF-module and // power your radio separately with a good regulator you can turn up PA level. //#define MY_RF24_PA_LEVEL RF24_PA_LOW // Enable serial gateway #define MY_GATEWAY_SERIAL // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender) //#if F_CPU == 8000000L //#define MY_BAUD_RATE 38400 //#endif // Flash leds on rx/tx/err // #define MY_LEDS_BLINKING_FEATURE // Set blinking period // #define MY_DEFAULT_LED_BLINK_PERIOD 300 // Inverses the behavior of leds // #define MY_WITH_LEDS_BLINKING_INVERSE // Enable inclusion mode #define MY_INCLUSION_MODE_FEATURE // Enable Inclusion mode button on gateway #define MY_INCLUSION_BUTTON_FEATURE // Inverses behavior of inclusion button (if using external pullup) //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP // Set inclusion mode duration (in seconds) #define MY_INCLUSION_MODE_DURATION 60 // Digital pin used for inclusion mode button #define MY_INCLUSION_MODE_BUTTON_PIN 3 // Uncomment to override default HW configurations //#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin //#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin //#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED #include #include #include // Enable repeater functionality for this node #define MY_REPEATER_FEATURE #define RELAY_1 22 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_2 23 #define RELAY_3 24 #define RELAY_4 25 #define RELAY_5 26 #define RELAY_6 27 #define RELAY_7 28 #define RELAY_8 29 #define RELAY_9 30 #define RELAY_10 31 #define RELAY_11 32 #define RELAY_12 33 #define RELAY_13 34 #define RELAY_14 35 #define RELAY_15 36 #define RELAY_16 37 #define RELAY_17 38 #define RELAY_18 39 #define RELAY_19 40 #define RELAY_20 41 #define RELAY_21 42 #define RELAY_22 43 #define RELAY_23 44 #define RELAY_24 45 #define RELAY_25 46 #define RELAY_26 47 #define RELAY_27 48 #define RELAY_28 49 #define RELAY_29 50 #define RELAY_30 51 #define RELAY_31 52 #define RELAY_32 53 #define NUMBER_OF_RELAYS 32 // Total number of attached relays #define RELAY_ON 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay #define BUTTON1_PIN A0 // stan wysoki! #define BUTTON2_PIN A1 #define BUTTON3_PIN A2 #define BUTTON4_PIN A3 #define BUTTON5_PIN A3 #define BUTTON6_PIN 8 #define BUTTON7_PIN 9 #define BUTTON8_PIN 10 #define BUTTON9_PIN 11 #define BUTTON10_PIN 12 void before() { for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) { // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(pin, loadState(sensor) ? RELAY_ON : RELAY_OFF); } } Bounce debouncer1 = Bounce(); Bounce debouncer2 = Bounce(); Bounce debouncer3 = Bounce(); Bounce debouncer4 = Bounce(); Bounce debouncer5 = Bounce(); Bounce debouncer6 = Bounce(); Bounce debouncer7 = Bounce(); Bounce debouncer8 = Bounce(); Bounce debouncer9 = Bounce(); Bounce debouncer10 = Bounce(); void setup() { // Setup locally attached sensors delay(10000); // Setup the button. pinMode(BUTTON1_PIN, INPUT); pinMode(BUTTON2_PIN, INPUT_PULLUP); pinMode(BUTTON3_PIN, INPUT_PULLUP); pinMode(BUTTON4_PIN, INPUT_PULLUP); pinMode(BUTTON5_PIN, INPUT_PULLUP); pinMode(BUTTON6_PIN, INPUT_PULLUP); pinMode(BUTTON7_PIN, INPUT_PULLUP); pinMode(BUTTON8_PIN, INPUT_PULLUP); pinMode(BUTTON9_PIN, INPUT_PULLUP); pinMode(BUTTON10_PIN, INPUT_PULLUP); // After setting up the button, setup debouncer. debouncer1.attach(BUTTON1_PIN); debouncer1.interval(5); debouncer2.attach(BUTTON2_PIN); debouncer2.interval(5); debouncer3.attach(BUTTON3_PIN); debouncer3.interval(5); debouncer4.attach(BUTTON4_PIN); debouncer4.interval(5); debouncer5.attach(BUTTON5_PIN); debouncer5.interval(5); debouncer6.attach(BUTTON6_PIN); debouncer6.interval(5); debouncer7.attach(BUTTON7_PIN); debouncer7.interval(5); debouncer8.attach(BUTTON8_PIN); debouncer8.interval(5); debouncer9.attach(BUTTON9_PIN); debouncer9.interval(5); debouncer10.attach(BUTTON10_PIN); debouncer10.interval(5); } void presentation(){ // Send the sketch version information to the gateway and Controller sendSketchInfo("Przekazniki_1", "1.0"); for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_LIGHT); } } MyMessage msg1(1, V_LIGHT); MyMessage msg2(2, V_LIGHT); MyMessage msg3(3, V_LIGHT); MyMessage msg4(4, V_LIGHT); MyMessage msg5(5, V_LIGHT); MyMessage msg6(6, V_LIGHT); MyMessage msg7(7, V_LIGHT); MyMessage msg8(8, V_LIGHT); MyMessage msg9(9, V_LIGHT); MyMessage msg10(10, V_LIGHT); MyMessage msg11(11, V_LIGHT); MyMessage msg12(12, V_LIGHT); MyMessage msg13(13, V_LIGHT); MyMessage msg14(14, V_LIGHT); MyMessage msg15(15, V_LIGHT); MyMessage msg16(16, V_LIGHT); MyMessage msg17(17, V_LIGHT); MyMessage msg18(18, V_LIGHT); MyMessage msg19(19, V_LIGHT); MyMessage msg20(20, V_LIGHT); MyMessage msg21(21, V_LIGHT); MyMessage msg22(22, V_LIGHT); MyMessage msg23(23, V_LIGHT); MyMessage msg24(24, V_LIGHT); MyMessage msg25(25, V_LIGHT); MyMessage msg26(26, V_LIGHT); MyMessage msg27(27, V_LIGHT); MyMessage msg28(28, V_LIGHT); MyMessage msg29(29, V_LIGHT); MyMessage msg30(30, V_LIGHT); MyMessage msg31(31, V_LIGHT); MyMessage msg32(32, V_LIGHT); void loop() { // Send locally attached sensor data here if (debouncer6.update()) { // Get the update value. int value6 = debouncer6.read(); // Send in the new value. if (value6 == LOW) { saveState(17, !loadState(17)); digitalWrite(RELAY_17, loadState(17) ? RELAY_ON : RELAY_OFF); send(msg17.set(loadState(17))); } } if (debouncer7.update()) { int value7 = debouncer7.read(); if (value7 == LOW) { saveState(19, !loadState(19)); digitalWrite(RELAY_19, loadState(19) ? RELAY_ON : RELAY_OFF); send(msg19.set(loadState(19))); } } if (debouncer8.update()) { int value8 = debouncer8.read(); if (value8 == LOW) { saveState(21, !loadState(21)); digitalWrite(RELAY_21, loadState(21) ? RELAY_ON : RELAY_OFF); send(msg21.set(loadState(21))); } } if (debouncer9.update()) { int value9 = debouncer9.read(); if (value9 == LOW) { saveState(13, !loadState(13)); digitalWrite(RELAY_13, loadState(13) ? RELAY_ON : RELAY_OFF); send(msg13.set(loadState(13))); } } if (debouncer1.update()) { int value1 = debouncer1.read(); if (value1 == HIGH) { saveState(3, !loadState(3)); digitalWrite(RELAY_3, loadState(3) ? RELAY_ON : RELAY_OFF); send(msg3.set(loadState(3))); } } } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type == V_LIGHT) { // Change relay state digitalWrite(message.sensor - 1 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }