Advertisement
makispaiktis

Rx

May 16th, 2021 (edited)
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <RF22.h>
  2. #include <RF22Router.h>
  3.  
  4. #define MY_ADDRESS 5
  5. #define NODE_ADDRESS_1 12
  6. #define NODE_ADDRESS_2 13
  7. #define NODE_ADDRESS_3 14
  8. RF22Router rf22(MY_ADDRESS);
  9.  
  10. int counter = 15;
  11. int before = 0;
  12. float start;
  13. float start_2;
  14.  
  15. // LEDs
  16. int pin1 = 4;
  17. int pin2 = 5;
  18. int pin3 = 6;
  19.  
  20. int pins[] = {pin1,pin2,pin3};
  21.  
  22. void setup() {
  23.  
  24.   pinMode(pin1, OUTPUT);
  25.   pinMode(pin2, OUTPUT);
  26.   pinMode(pin3, OUTPUT);
  27.  
  28.  
  29.   Serial.begin(9600);
  30.   if (!rf22.init())
  31.     Serial.println("RF22 init failed");
  32.   // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
  33.   if (!rf22.setFrequency(431.0))
  34.     Serial.println("setFrequency Fail");
  35.   rf22.setTxPower(RF22_TXPOW_20DBM);
  36.   //1,2,5,8,11,14,17,20 DBM
  37.   rf22.setModemConfig(RF22::GFSK_Rb125Fd125);
  38.   //modulation
  39.  
  40.   // Manually define the routes for this network
  41.   rf22.addRouteTo(NODE_ADDRESS_1, NODE_ADDRESS_1);
  42.   rf22.addRouteTo(NODE_ADDRESS_2, NODE_ADDRESS_2);
  43.   rf22.addRouteTo(NODE_ADDRESS_3, NODE_ADDRESS_3);
  44.  
  45. }
  46.  
  47.  
  48. void loop() {
  49.   uint8_t buf[RF22_ROUTER_MAX_MESSAGE_LEN];
  50.   char incoming[RF22_ROUTER_MAX_MESSAGE_LEN];
  51.   memset(buf, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  52.   memset(incoming, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  53.   uint8_t len = sizeof(buf);
  54.   uint8_t from;
  55.   int received_value = 0;
  56.  
  57.   if (rf22.recvfromAck(buf, &len, &from))
  58.   {
  59.     buf[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
  60.     memcpy(incoming, buf, RF22_ROUTER_MAX_MESSAGE_LEN);
  61.     //Serial.print("got request from : ");
  62.     //Serial.println(from, DEC);
  63.     received_value = atoi((char*)incoming);
  64.   }
  65.  
  66.    
  67.     if(received_value == 0 && before == 0)
  68.     {
  69.       Serial.println("All Quiet");
  70.     }
  71.     else if(received_value == 0 && before != 0 && before != 3)
  72.     {
  73.       if(millis() - start >= 2000)
  74.       {
  75.         digitalWrite(pins[before-1], LOW);
  76.         before = 0;
  77.       }
  78.     }
  79.     else if(before == 3 && received_value == 0)
  80.     {
  81.  
  82.       if(counter > 0) // if(millis()-start_2 <= 15000)
  83.       {
  84.         Serial.println(counter);
  85.         counter--;
  86.         digitalWrite(pin3, HIGH);
  87.         delay(450);
  88.         digitalWrite(pin3, LOW);
  89.         delay(450);
  90.       }
  91.      
  92.     }
  93.     else if(received_value != 0)
  94.     {
  95.       start = millis();
  96.       before = received_value;
  97.  
  98.       switch (before)
  99.       {
  100.         case 1:
  101.           digitalWrite(pin1, HIGH);
  102.           digitalWrite(pin2, LOW);
  103.           digitalWrite(pin3, LOW);
  104.  
  105.           Serial.println("Level 1 reached! ");  
  106.           break;
  107.         case 2:
  108.           digitalWrite(pin1, LOW);
  109.           digitalWrite(pin2, HIGH);
  110.           digitalWrite(pin3, LOW);
  111.  
  112.           Serial.println("Level 2 reached! ");
  113.           break;
  114.         case 3:
  115.           digitalWrite(pin1, LOW);
  116.           digitalWrite(pin2, LOW);
  117.           digitalWrite(pin3, HIGH);
  118.  
  119.           Serial.println("Level 3 reached!");
  120.           Serial.println("Password requested. Countdown sequency started:");
  121.          
  122.           break;
  123.         case 4:
  124.           Serial.println("Password was given correct");
  125.          
  126.           digitalWrite(pin3, LOW);
  127.           for(int i = 0; i < 110; i++){
  128.              digitalWrite(pin1, HIGH);
  129.              delay(50);
  130.              digitalWrite(pin1, LOW);
  131.              delay(50);
  132.           }
  133.          
  134.           counter = 15;
  135.           before = 0;
  136.           delay(5000);
  137.           break;
  138.         case 5:
  139.           Serial.println("Password was not given correctly!!! Police on the way");
  140.  
  141.           digitalWrite(pin3, LOW);
  142.           for(int i = 0; i < 160; i++)
  143.           {
  144.             digitalWrite(pin1, HIGH);
  145.             digitalWrite(pin2, HIGH);
  146.             digitalWrite(pin3, HIGH);
  147.             delay(50);
  148.             digitalWrite(pin1, LOW);
  149.             digitalWrite(pin2, LOW);
  150.             digitalWrite(pin3, LOW);
  151.             delay(50);
  152.           }
  153.          
  154.           counter = 15;
  155.           before = 0;
  156.           delay(5000);
  157.           break;
  158.       }
  159.     }
  160.      delay(100);
  161.   } //end of void loop()
  162.    
  163.  
  164.  
  165.  
  166.      
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement