#define BUTTON_PIN 1 // the number of the pushbutton pin #define LED_PIN 2 // the number of the LED pin // transmitter Include Libraries #include #include #include //create an RF24 object RF24 radio(0, 6); // CE, CSN //address through which two modules communicate. const byte address1[6] = "00001"; const byte address2[6] = "00002"; const byte address3[6] = "00003"; const int nodeNumber = 1; // modify the nodeNumber as per the node you program 1,2,3 const byte address[6] = "00001"; // modify the address as per the node you program "00001" , "00002", "00003" int targetNode = 3; int counter = 1; int nodeCount = 3; bool node1LedStatus = false; bool node2LedStatus = false; bool node3LedStatus = false; bool ledState = false; // the current state of the LED int buttonState; // the current reading from the input pin bool lastButtonState = false; // the previous reading from the input pin // the following variables are long because the time, measured in milliseconds, // will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(LED_PIN, OUTPUT); Serial.begin(115200); // set initial LED state digitalWrite(LED_PIN, ledState); radio.begin(); radio.openReadingPipe(0, address); radio.startListening(); } void loop() { if (radio.available()) { char text[32] = {0}; radio.read(&text, sizeof(text)); String Data = String(text); Serial.println(Data); // if ( Data == "ON") { // ledState = true; // } // if (Data == "OFF") { // ledState = false; // } } // read the state of the switch into a local variable: int reading = digitalRead(BUTTON_PIN); // check to see if you just pressed the button // (i.e., the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: // if the button state has changed: if (reading != buttonState) { buttonState = reading; // only toggle the LED if the new button state is HIGH if (buttonState == HIGH) { // Serial.println(counter); // if (counter >= (nodeCount+1)) { // counter = 1; // } // if (counter == nodeNumber) { // if(counter != nodeCount) // { // counter++; // } // else // { // counter = 1; // } // } // sendTrigger(counter); sendTrigger(targetNode); // counter++; ledState = !ledState; } } } // set the LED: digitalWrite(LED_PIN, ledState); // save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading; } void sendTrigger(int nodeSelect) { bool triggerStatus = false; Serial.println(nodeSelect); switch (nodeSelect) { case 1: triggerStatus = node1LedStatus; node1LedStatus = !node1LedStatus; radio.openWritingPipe(address1); break; case 2: triggerStatus = node2LedStatus; node2LedStatus = !node2LedStatus; radio.openWritingPipe(address2); break; case 3: triggerStatus = node3LedStatus; node3LedStatus = !node3LedStatus; radio.openWritingPipe(address3); break; } radio.stopListening(); if (triggerStatus) { char text[32] = "8 1"; radio.write(&text, sizeof(text)); delay(100); } else { char text[32] = "8 0"; radio.write(&text, sizeof(text)); delay(100); } radio.openReadingPipe(0, address); radio.startListening(); }