Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- #define CE_PIN 7
- #define CSN_PIN 8
- #define RF_PWR_HIGH
- const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'B', 'A'};
- RF24 radio(CE_PIN, CSN_PIN);
- char dataReceived[10]; // this must match dataToSend in the TX
- int ackData[2] = {0, 0}; // the two values to be sent to the master
- bool newData = false;
- bool blueLightState = 0;
- const int ledPinA = 3; // the number of the LED pin
- int digitReceived = 0;
- const int buttonPinA = 2; // the number of the pushbutton pin A
- int buttonStateA = 0; // variable for reading the pushbutton status
- bool buttonAActive = 0;
- int slaveMode = 0;
- unsigned int currentMillis = 0;
- unsigned int lastCanx = 0;
- int canxMS;
- unsigned int lightOnTime;
- unsigned int lightOffTime;
- unsigned int prevSerial;
- const int serialUpdateInterval = 500;
- const int canxPersist = 1000;
- //==============
- void setup() {
- Serial.begin(9600);
- radio.begin();
- radio.setDataRate( RF24_250KBPS );
- radio.openReadingPipe(1, thisSlaveAddress);
- radio.enableAckPayload();
- radio.startListening();
- radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
- pinMode(ledPinA, OUTPUT);
- }
- //==========
- void loop() {
- currentMillis = millis();
- getData();
- updateLight();
- checkButtonState();
- updateSerial();
- }
- void checkButtonState() {
- buttonStateA = digitalRead(buttonPinA);
- if (buttonStateA == HIGH) {
- doDeactivate();
- }
- }
- //==========
- void updateLight() {
- if (currentMillis - lastCanx < canxPersist) {
- digitalWrite(ledPinA, LOW);
- digitalWrite(LED_BUILTIN, LOW);
- } else {
- if (slaveMode == 1) {
- digitalWrite(ledPinA, HIGH);
- } else {
- digitalWrite(ledPinA, LOW);
- }
- }
- }
- //============
- void getData() {
- // Serial.println ("getData running");
- if ( radio.available() ) {
- radio.read( &dataReceived, sizeof(dataReceived) );
- updateAck();
- newData = true;
- digitReceived = dataReceived[0];
- switch (digitReceived) {
- case 48: // a '0' has been received - go into standby
- // Serial.println("Scenario 0 - Standby");
- doStandby();
- break;
- case 49: // turn your light on
- // Serial.println("Scenario 1 - Turn light on");
- doActivate();
- break;
- }
- }
- }
- //================
- void doStandby() {
- // Serial.println("doStandby running");
- if (currentMillis - lastCanx < canxPersist) {
- slaveMode = 2;
- } else {
- slaveMode = 0;
- }
- blueLightState = 0;
- ackData[0] = 0;
- }
- //================
- void doActivate() {
- // Serial.println("doActivate running");
- if (slaveMode == 0) {
- lightOnTime = currentMillis;
- }
- if (currentMillis - lastCanx < canxPersist) {
- slaveMode = 2;
- } else {
- slaveMode = 1;
- }
- blueLightState = 1;
- ackData[0] = 1; // acknowledge that light is on
- }
- //================
- void doDeactivate() {
- // Serial.println("doDeactivate running");
- lightOffTime = currentMillis;
- //Serial.print("lightOffTime: ");
- //Serial.println(lightOffTime);
- canxMS = (lightOffTime - lightOnTime);
- slaveMode = 2;
- lastCanx = currentMillis;
- }
- //================
- void updateAck() {
- if (slaveMode == 2) {
- ackData[0] = 2;
- ackData[1] = (canxMS);
- } else {
- ackData[0] = slaveMode;
- ackData[1] = 0;
- }
- // ackData[1] = millis();
- radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
- }
- //================
- void updateSerial() {
- if ((currentMillis - prevSerial) >= serialUpdateInterval) {
- Serial.print("Mode: ");
- Serial.print(slaveMode);
- Serial.print(" canxMS: ");
- Serial.print(canxMS);
- Serial.print(" digitReceived: ");
- Serial.print(digitReceived);
- //Serial.print("lightOnTime: ");
- //Serial.print(lightOnTime);
- Serial.println();
- if (newData == true) {
- //Serial.print("Data received ");
- //Serial.println(dataReceived[8]);
- //Serial.print(" ackPayload sent ");
- //Serial.print(ackData[0]);
- //Serial.print(", ");
- // Serial.println(ackData[1]);
- newData = false;
- }
- prevSerial = currentMillis;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment