makispaiktis

Transmitter 3

May 15th, 2021 (edited)
451
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // #include <SPI.h>
  2. #include <RF22.h>
  3. #include <RF22Router.h>
  4. #define MY_ADDRESS 14
  5. #define DESTINATION_ADDRESS 5
  6. #include "pitches.h"
  7.  
  8.  
  9. RF22Router rf22(MY_ADDRESS);
  10.  
  11. // Aloha
  12. int sensorVal = 10;
  13. long randNumber;
  14. boolean successful_packet = false;
  15. int max_delay=3000;
  16. float vib_limit = 0.05;
  17. int level = 3;
  18. int corPass = 4;
  19. int policeComing = 5;
  20.  
  21. // Pass
  22. int password;
  23. int correct = 1010;
  24. int wrongGuess = 0;
  25. int limit = 3;
  26.  
  27. // Melody
  28. int melody[] = {
  29.   NOTE_FS5, NOTE_FS5, NOTE_D5, NOTE_B4, NOTE_B4, NOTE_E5,
  30.   NOTE_E5, NOTE_E5, NOTE_GS5, NOTE_GS5, NOTE_A5, NOTE_B5,
  31.   NOTE_A5, NOTE_A5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5,
  32.   NOTE_FS5, NOTE_FS5, NOTE_E5, NOTE_E5, NOTE_FS5, NOTE_E5
  33. };
  34.  
  35. int durations[] = {
  36.   8, 8, 8, 4, 4, 4,
  37.   4, 5, 8, 8, 8, 8,
  38.   8, 8, 8, 4, 4, 4,
  39.   4, 5, 8, 8, 8, 8
  40. };
  41.  
  42. int songLength = sizeof(melody)/sizeof(melody[0]);
  43.  
  44. // timer
  45. float start;
  46.  
  47. // define meas vibration sensor variable
  48. const int PIEZO_PIN = A0; // Piezo output
  49. // Buzzer
  50. int buzzerPin = 9;
  51. // LEDs
  52. int pin1 = 4;
  53. int pin2 = 5;
  54. int pin3 = 6;
  55. int pin4 = 7;
  56.  
  57.  
  58. void serialFlush(){
  59.   while(Serial.available() > 0) {
  60.     char t = Serial.read();
  61.   }
  62. }
  63.  
  64.  
  65. void play(){
  66.  
  67.   for (int thisNote = 0; thisNote < songLength; thisNote++){
  68.     // determine the duration of the notes that the computer understands
  69.     // divide 1000 by the value, so the first note lasts for 1000/8 milliseconds
  70.     int duration = 1000/ durations[thisNote];
  71.     tone(buzzerPin, melody[thisNote], duration);
  72.     // pause between notes
  73.     int pause = duration * 1.3;
  74.     delay(pause);
  75.     // stop the tone
  76.     noTone(8);
  77.   }
  78. }
  79.  
  80. void police()
  81. {
  82.      for(int i=0; i < 8; i++)
  83.       {
  84.         tone(buzzerPin, 3000);
  85.         digitalWrite(pin1, HIGH);
  86.         digitalWrite(pin2, HIGH);
  87.         digitalWrite(pin3, LOW);
  88.         digitalWrite(pin4, LOW);
  89.         delay(500);
  90.         tone(buzzerPin, 1000);
  91.         digitalWrite(pin1, LOW);
  92.         digitalWrite(pin2, LOW);
  93.         digitalWrite(pin3, HIGH);
  94.         digitalWrite(pin4, HIGH);
  95.         delay(500);
  96.       }
  97.       noTone(buzzerPin);
  98.       digitalWrite(pin1, LOW);
  99.       digitalWrite(pin2, LOW);
  100.       digitalWrite(pin3, LOW);
  101.       digitalWrite(pin4, LOW);
  102. }
  103.  
  104. void sendData(int data){
  105.  
  106.     char data_read[RF22_ROUTER_MAX_MESSAGE_LEN];
  107.     uint8_t data_send[RF22_ROUTER_MAX_MESSAGE_LEN];
  108.     memset(data_read, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  109.     memset(data_send, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  110.     sprintf(data_read, "%d", data);
  111.     data_read[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
  112.     memcpy(data_send, data_read, RF22_ROUTER_MAX_MESSAGE_LEN);
  113.  
  114.    
  115.    
  116.     successful_packet = false;
  117.     while (!successful_packet)
  118.     {    
  119.       if (rf22.sendtoWait(data_send, sizeof(data_send), DESTINATION_ADDRESS) != RF22_ROUTER_ERROR_NONE)
  120.       {
  121.         Serial.println("sendtoWait failed");
  122.         randNumber=random(200,max_delay);
  123.         Serial.println(randNumber);
  124.         delay(randNumber);
  125.       }
  126.       else
  127.       {
  128.         successful_packet = true;
  129.         Serial.println("sendtoWait Succesful");
  130.       }
  131.     }
  132.  
  133. }
  134.  
  135.  
  136. void setup() {
  137.  
  138.   Serial.begin(9600);
  139.   pinMode(buzzerPin, OUTPUT);
  140.   pinMode(pin1, OUTPUT);
  141.   pinMode(pin2, OUTPUT);
  142.   pinMode(pin3, OUTPUT);
  143.   pinMode(pin4, OUTPUT);
  144.  
  145.   if (!rf22.init())
  146.     Serial.println("RF22 init failed");
  147.   // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
  148.  
  149.   if (!rf22.setFrequency(431.0))
  150.     Serial.println("setFrequency Fail");
  151.   rf22.setTxPower(RF22_TXPOW_20DBM);
  152.   //1,2,5,8,11,14,17,20 DBM
  153.   //rf22.setModemConfig(RF22::OOK_Rb40Bw335  );
  154.   rf22.setModemConfig(RF22::GFSK_Rb125Fd125);
  155.   //modulation
  156.  
  157.   // Manually define the routes for this network
  158.   rf22.addRouteTo(DESTINATION_ADDRESS, DESTINATION_ADDRESS);
  159.   sensorVal = analogRead(A0);                             // Metraei kathe fora kati diaforetiko kai ara bgazei allo seed
  160.   randomSeed(sensorVal);// (μία μόνο φορά μέσα στην setup)          
  161. }
  162.  
  163.  
  164. void loop() {
  165.  
  166.   // Read Piezo ADC value in, and convert it to a voltage
  167.   int piezoADC = analogRead(PIEZO_PIN);
  168.   float piezoV = piezoADC / 1023.0 * 5.0;
  169.   Serial.print("Vibration: "); // Print the voltage
  170.   Serial.println(piezoV); // Print the voltage.
  171.  
  172.   if(piezoV >= vib_limit)
  173.   {
  174.     Serial.print("Vibration detected ( ");
  175.     Serial.print(piezoV);
  176.     Serial.println(" )");
  177.    
  178.     start = millis();
  179.  
  180.     sendData(level);
  181.    
  182.     Serial.println("Give a password!!");
  183.     wrongGuess = 0;
  184.    
  185.     while(millis() - start < 15000)
  186.     {
  187.        
  188.         tone(buzzerPin, 1000);
  189.         delay(500);
  190.         noTone(buzzerPin);
  191.         delay(500);
  192.      
  193.      if(Serial.available() > 0)
  194.       {
  195.  
  196.         //pass = Serial.readString();
  197.         password = Serial.parseInt();
  198.        
  199.         // say what you got:
  200.         Serial.print("You gave password: ");
  201.         Serial.println(password);
  202.      
  203.         if(password == correct)
  204.         {
  205.           Serial.println("Correct Password");
  206.          
  207.           sendData(corPass);
  208.           play();
  209.           play();
  210.           serialFlush();
  211.           break;
  212.         }
  213.         else if(password != correct)
  214.         {
  215.           wrongGuess++;
  216.           Serial.print(limit - wrongGuess);
  217.           Serial.println(" Tries left");
  218.           serialFlush();
  219.          
  220.         }
  221.  
  222.         if(wrongGuess == limit)
  223.           {
  224.             Serial.println("Limit Passed");
  225.             Serial.println("Police is on the way!");
  226.             sendData(policeComing);
  227.             police();
  228.             serialFlush();
  229.             break;
  230.           }      
  231.      }
  232.      
  233.       else if(millis() - start >= 15000)
  234.       {
  235.         Serial.println("Timeout!");
  236.         Serial.println("Police is on the way!");
  237.         sendData(policeComing);
  238.         police();
  239.         serialFlush();
  240.         break;
  241.       }
  242.      
  243.     }
  244.  
  245.     delay(150); // do not erase
  246.     Serial.println("");
  247.   }
  248. }
RAW Paste Data