makispaiktis

Receiver

May 15th, 2021 (edited)
362
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.print("Level 1 reached! ");
  106.           Serial.println(before);
  107.          
  108.           break;
  109.         case 2:
  110.           digitalWrite(pin1, LOW);
  111.           digitalWrite(pin2, HIGH);
  112.           digitalWrite(pin3, LOW);
  113.  
  114.           Serial.print("Level 2 reached! ");
  115.           Serial.println(before);
  116.          
  117.           break;
  118.         case 3:
  119.           digitalWrite(pin1, LOW);
  120.           digitalWrite(pin2, LOW);
  121.           digitalWrite(pin3, HIGH);
  122.  
  123.           Serial.println("Level 3 reached!");
  124.           Serial.println("Password requested. Countdown sequency started:");
  125.          
  126.           break;
  127.         case 4:
  128.           Serial.println("Password was given correct");
  129.          
  130.           digitalWrite(pin3, LOW);
  131.           for(int i = 0; i < 200; i++){
  132.              digitalWrite(pin1, HIGH);
  133.              delay(50);
  134.              digitalWrite(pin1, LOW);
  135.              delay(50);
  136.           }
  137.          
  138.           counter = 15;
  139.           before = 0;
  140.           delay(5000);
  141.           break;
  142.         case 5:
  143.           Serial.println("Password was not given correctly!!! Police on the way");
  144.  
  145.           digitalWrite(pin3, LOW);
  146.           for(int i = 0; i < 160; i++)
  147.           {
  148.             digitalWrite(pin1, HIGH);
  149.             digitalWrite(pin2, HIGH);
  150.             digitalWrite(pin3, HIGH);
  151.             delay(50);
  152.             digitalWrite(pin1, LOW);
  153.             digitalWrite(pin2, LOW);
  154.             digitalWrite(pin3, LOW);
  155.             delay(50);
  156.           }
  157.  
  158.           before = 0;
  159.           delay(5000);
  160.           break;
  161.       }
  162.     }
  163.      delay(100);
  164.   } //end of void loop()
  165.    
  166.  
  167.  
  168.  
  169.      
  170.  
RAW Paste Data