Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <RF24Network.h>
  2. #include <RF24.h>
  3. #include <SPI.h>
  4.  
  5. #define btn_red     2
  6. #define btn_white   3
  7. #define dip1        9
  8. #define dip2        10
  9. #define led1        12
  10. #define led2        13
  11. #define channel     90
  12.  
  13. #if defined( __AVR_ATtiny84__ )
  14. const static uint8_t SS   = PA7;
  15. const static uint8_t MOSI = PA6;
  16. const static uint8_t MISO = PA5;
  17. const static uint8_t SCK  = PA4;
  18. #endif
  19.  
  20. RF24 radio(7,8); //CE,CSN
  21. RF24Network network(radio);
  22. const uint16_t  base_station = 00;
  23. const uint16_t  judge1 = 01;
  24. const uint16_t  judge2 = 02;
  25. const uint16_t  judge3 = 03;
  26. const long      interval = 500;
  27.  
  28. uint16_t node;
  29. unsigned long btn_send;
  30. unsigned long previousMillis;
  31. bool call_wait;
  32. bool call_confirm;
  33. bool test_mode;
  34.  
  35. void setup() {
  36.  
  37.   pinMode(dip1, INPUT);
  38.   pinMode(dip2, INPUT);
  39.   pinMode(btn_red, INPUT);
  40.   pinMode(btn_white, INPUT);
  41.   pinMode(led1, OUTPUT);
  42.   pinMode(led2, OUTPUT);
  43.  
  44.   bool mode_sel1 = digitalRead(dip1);
  45.   bool mode_sel2 = digitalRead(dip2);
  46.  
  47.   previousMillis = 0;
  48.  
  49. //Use the DIP switch to set which judge the remote is assigned to
  50.   if ( (mode_sel1 == LOW) && (mode_sel2 == LOW) ) {
  51.     node = judge1;
  52.   }
  53.   if ( (mode_sel1 == LOW) && (mode_sel2 == HIGH) ) {
  54.     node = judge2;
  55.   }
  56.   if ( (mode_sel1 == HIGH) && (mode_sel2 == HIGH) ) {
  57.     node = judge3;
  58.   }
  59.   if ( (mode_sel1 == HIGH) && (mode_sel2 == HIGH) ) {
  60.     node = judge1;
  61.   }
  62.  
  63. //Set up the radio network    
  64.   radio.begin();
  65.   network.begin(channel, node);
  66.   radio.setDataRate(RF24_250KBPS);
  67.   radio.setPALevel(RF24_PA_HIGH);
  68.  
  69. }
  70.  
  71. void loop() {
  72.  
  73.   radio_comms();
  74.  
  75.   if (test_mode == true){
  76.     blink_led(led1);
  77.     blink_led(led2);
  78.     if ((digitalRead(btn_red) == LOW) && (digitalRead(btn_white) == HIGH)) {
  79.         btn_send = 1;
  80.       }
  81.       else if ((digitalRead(btn_red) == HIGH) && (digitalRead(btn_white) == LOW)) {
  82.         btn_send = 2;
  83.       }
  84.       else btn_send = 0;
  85.   }
  86.   else {
  87.     if (btn_send == 0) {
  88.       if ((digitalRead(btn_red) == LOW) && (digitalRead(btn_white) == HIGH)) {
  89.         btn_send = 1;
  90.       }
  91.       else if ((digitalRead(btn_red) == HIGH) && (digitalRead(btn_white) == LOW)) {
  92.         btn_send = 2;
  93.       }
  94.     }  
  95.  
  96.     if (call_wait == true) digitalWrite(led1,HIGH);
  97.     else digitalWrite(led1,LOW);
  98.  
  99.     if (call_confirm == true) {
  100.       digitalWrite(led2, HIGH);
  101.       btn_send = 0;
  102.     }
  103.     else digitalWrite(led2,LOW);
  104.   }
  105.  
  106.  
  107. }
  108.  
  109. void radio_comms()  {
  110.   network.update();
  111.  
  112.   //RECEIVING
  113.   while (network.available()){
  114.     RF24NetworkHeader header_rec;
  115.     unsigned long incomingData;
  116.     network.read(header_rec, &incomingData, sizeof(incomingData));
  117.     if(header_rec.from_node == base_station){
  118.         if (incomingData == 1){
  119.           test_mode = false;
  120.           call_wait = true;
  121.           call_confirm = false;
  122.         }
  123.         else if (incomingData == 2){
  124.           test_mode = false;
  125.           call_wait = true;
  126.           call_confirm = true;
  127.         }        
  128.         else if (incomingData == 3){
  129.           test_mode = true;
  130.           call_wait = false;
  131.           call_confirm = false;
  132.         }
  133.         else {
  134.           test_mode = false;
  135.           call_wait = false;
  136.           call_confirm = false;
  137.         }
  138.     }
  139.  
  140.     }
  141.   //SENDING
  142.   RF24NetworkHeader header_snd(judge1);
  143.   bool ok = network.write(header_snd, &btn_send, sizeof(btn_send));
  144.  
  145. }
  146.  
  147. void blink_led(int ledPin) {
  148.   unsigned long currentMillis = millis();
  149.   int ledState = LOW;
  150.  
  151.   if (currentMillis - previousMillis >= interval) {
  152.     // save the last time you blinked the LED
  153.     previousMillis = currentMillis;
  154.  
  155.     // if the LED is off turn it on and vice-versa:
  156.     if (digitalRead(ledPin)== LOW) {
  157.       ledState = HIGH;
  158.     } else {
  159.       ledState = LOW;
  160.     }
  161.  
  162.     // set the LED with the ledState of the variable:
  163.     digitalWrite(ledPin, ledState);
  164.   }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement