prismaardvark

SLAVE

Dec 29th, 2021
1,544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1.  
  2.  
  3. #include <SPI.h>
  4. #include <nRF24L01.h>
  5. #include <RF24.h>
  6.  
  7. #define CE_PIN   7
  8. #define CSN_PIN 8
  9. #define RF_PWR_HIGH
  10.  
  11. const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'B', 'A'};
  12.  
  13. RF24 radio(CE_PIN, CSN_PIN);
  14.  
  15. char dataReceived[10]; // this must match dataToSend in the TX
  16. int ackData[2] = {0, 0}; // the two values to be sent to the master
  17. bool newData = false;
  18. bool blueLightState = 0;
  19. const int ledPinA =  3;      // the number of the LED pin
  20. int digitReceived = 0;
  21. const int buttonPinA = 2;     // the number of the pushbutton pin A
  22. int buttonStateA = 0;         // variable for reading the pushbutton status
  23. bool buttonAActive = 0;
  24. int slaveMode = 0;
  25. unsigned int currentMillis = 0;
  26. unsigned int lastCanx = 0;
  27. int canxMS;
  28. unsigned int lightOnTime;
  29. unsigned int lightOffTime;
  30. unsigned int prevSerial;
  31.  
  32. const int serialUpdateInterval = 500;
  33. const int canxPersist = 1000;
  34.  
  35.  
  36. //==============
  37.  
  38. void setup() {
  39.   Serial.begin(9600);
  40.   radio.begin();
  41.   radio.setDataRate( RF24_250KBPS );
  42.   radio.openReadingPipe(1, thisSlaveAddress);
  43.   radio.enableAckPayload();
  44.   radio.startListening();
  45.   radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
  46.   pinMode(ledPinA, OUTPUT);
  47. }
  48.  
  49. //==========
  50.  
  51. void loop() {
  52.   currentMillis = millis();
  53.   getData();
  54.   updateLight();
  55.   checkButtonState();
  56.   updateSerial();
  57. }
  58.  
  59. void checkButtonState() {
  60.   buttonStateA = digitalRead(buttonPinA);
  61.   if (buttonStateA == HIGH) {
  62.     doDeactivate();
  63.   }
  64. }
  65.  
  66. //==========
  67.  
  68. void updateLight() {
  69.   if (currentMillis - lastCanx < canxPersist) {
  70.     digitalWrite(ledPinA, LOW);
  71.     digitalWrite(LED_BUILTIN, LOW);
  72.   } else {
  73.     if (slaveMode == 1) {
  74.       digitalWrite(ledPinA, HIGH);
  75.     } else {
  76.       digitalWrite(ledPinA, LOW);
  77.     }
  78.   }
  79. }
  80.  
  81. //============
  82.  
  83. void getData() {
  84.   // Serial.println ("getData running");
  85.   if ( radio.available() ) {
  86.     radio.read( &dataReceived, sizeof(dataReceived) );
  87.     updateAck();
  88.     newData = true;
  89.     digitReceived = dataReceived[0];
  90.  
  91.     switch (digitReceived) {
  92.       case 48: // a '0' has been received - go into standby
  93.         // Serial.println("Scenario 0 - Standby");
  94.         doStandby();
  95.         break;
  96.       case 49: // turn your light on
  97.         // Serial.println("Scenario 1 - Turn light on");
  98.         doActivate();
  99.         break;
  100.     }
  101.   }
  102. }
  103.  
  104. //================
  105.  
  106. void doStandby() {
  107.  // Serial.println("doStandby running");
  108.   if (currentMillis - lastCanx < canxPersist) {
  109.     slaveMode = 2;
  110.   } else {
  111.     slaveMode = 0;
  112.   }
  113.   blueLightState = 0;
  114.   ackData[0] = 0;
  115. }
  116. //================
  117.  
  118. void doActivate() {
  119.  //   Serial.println("doActivate running");
  120.   if (slaveMode == 0) {
  121.     lightOnTime = currentMillis;
  122.   }
  123.   if (currentMillis - lastCanx < canxPersist) {
  124.     slaveMode = 2;
  125.   } else {
  126.     slaveMode = 1;
  127.   }
  128.   blueLightState = 1;
  129.   ackData[0] = 1; // acknowledge that light is on
  130. }
  131. //================
  132.  
  133. void doDeactivate() {
  134.   //  Serial.println("doDeactivate running");
  135.   lightOffTime = currentMillis;
  136.   //Serial.print("lightOffTime: ");
  137.   //Serial.println(lightOffTime);
  138.   canxMS = (lightOffTime - lightOnTime);
  139.   slaveMode = 2;
  140.   lastCanx = currentMillis;
  141. }
  142.  
  143.  
  144.  
  145. //================
  146.  
  147. void updateAck() {
  148.   if (slaveMode == 2) {
  149.     ackData[0] = 2;
  150.     ackData[1] = (canxMS);
  151.   } else {
  152.     ackData[0] = slaveMode;
  153.     ackData[1] = 0;
  154.   }
  155.   // ackData[1] = millis();
  156.   radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
  157. }
  158.  
  159. //================
  160.  
  161. void updateSerial() {
  162.   if ((currentMillis - prevSerial) >= serialUpdateInterval) {
  163.     Serial.print("Mode:  ");
  164.     Serial.print(slaveMode);
  165.     Serial.print(" canxMS: ");
  166.     Serial.print(canxMS);
  167.     Serial.print(" digitReceived: ");
  168.     Serial.print(digitReceived);
  169.     //Serial.print("lightOnTime: ");
  170.     //Serial.print(lightOnTime);
  171.     Serial.println();
  172.     if (newData == true) {
  173.       //Serial.print("Data received ");
  174.       //Serial.println(dataReceived[8]);
  175.       //Serial.print(" ackPayload sent ");
  176.       //Serial.print(ackData[0]);
  177.       //Serial.print(", ");
  178.       // Serial.println(ackData[1]);
  179.       newData = false;
  180.     }
  181.     prevSerial = currentMillis;
  182.   }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment