Advertisement
makispaiktis

Transmitter 3 by makis

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