Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #include <SPI.h>
- #include <RF22.h>
- #include <RF22Router.h>
- #define MY_ADDRESS 14
- #define DESTINATION_ADDRESS 5
- #include "pitches.h"
- RF22Router rf22(MY_ADDRESS);
- // Aloha
- int sensorVal = 10;
- long randNumber;
- boolean successful_packet = false;
- int max_delay=3000;
- float vib_limit = 0.05;
- int level = 3;
- int corPass = 4;
- int policeComing = 5;
- // Pass
- int password;
- int correct = 1010;
- int wrongGuess = 0;
- int limit = 3;
- // Melody
- int melody[] = {
- NOTE_FS5, NOTE_FS5, NOTE_D5, NOTE_B4, NOTE_B4, NOTE_E5,
- NOTE_E5, NOTE_E5, NOTE_GS5, NOTE_GS5, NOTE_A5, NOTE_B5,
- NOTE_A5, NOTE_A5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5,
- NOTE_FS5, NOTE_FS5, NOTE_E5, NOTE_E5, NOTE_FS5, NOTE_E5
- };
- int durations[] = {
- 8, 8, 8, 4, 4, 4,
- 4, 5, 8, 8, 8, 8,
- 8, 8, 8, 4, 4, 4,
- 4, 5, 8, 8, 8, 8
- };
- int songLength = sizeof(melody)/sizeof(melody[0]);
- // timer
- float start;
- // define meas vibration sensor variable
- const int PIEZO_PIN = A0; // Piezo output
- // Buzzer
- int buzzerPin = 9;
- // LEDs
- int pin1 = 4;
- int pin2 = 5;
- int pin3 = 6;
- int pin4 = 7;
- void serialFlush(){
- while(Serial.available() > 0) {
- char t = Serial.read();
- }
- }
- void play(){
- for (int thisNote = 0; thisNote < songLength; thisNote++){
- // determine the duration of the notes that the computer understands
- // divide 1000 by the value, so the first note lasts for 1000/8 milliseconds
- int duration = 1000/ durations[thisNote];
- tone(buzzerPin, melody[thisNote], duration);
- // pause between notes
- int pause = duration * 1.3;
- delay(pause);
- // stop the tone
- noTone(8);
- }
- }
- void police()
- {
- for(int i=0; i < 8; i++)
- {
- tone(buzzerPin, 3000);
- digitalWrite(pin1, HIGH);
- digitalWrite(pin2, HIGH);
- digitalWrite(pin3, LOW);
- digitalWrite(pin4, LOW);
- delay(500);
- tone(buzzerPin, 1000);
- digitalWrite(pin1, LOW);
- digitalWrite(pin2, LOW);
- digitalWrite(pin3, HIGH);
- digitalWrite(pin4, HIGH);
- delay(500);
- }
- noTone(buzzerPin);
- digitalWrite(pin1, LOW);
- digitalWrite(pin2, LOW);
- digitalWrite(pin3, LOW);
- digitalWrite(pin4, LOW);
- }
- void sendData(int data){
- char data_read[RF22_ROUTER_MAX_MESSAGE_LEN];
- uint8_t data_send[RF22_ROUTER_MAX_MESSAGE_LEN];
- memset(data_read, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
- memset(data_send, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
- sprintf(data_read, "%d", data);
- data_read[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
- memcpy(data_send, data_read, RF22_ROUTER_MAX_MESSAGE_LEN);
- successful_packet = false;
- while (!successful_packet)
- {
- if (rf22.sendtoWait(data_send, sizeof(data_send), DESTINATION_ADDRESS) != RF22_ROUTER_ERROR_NONE)
- {
- Serial.println("sendtoWait failed");
- randNumber=random(200,max_delay);
- Serial.println(randNumber);
- delay(randNumber);
- }
- else
- {
- successful_packet = true;
- Serial.println("sendtoWait Succesful");
- }
- }
- }
- void setup() {
- Serial.begin(9600);
- pinMode(buzzerPin, OUTPUT);
- pinMode(pin1, OUTPUT);
- pinMode(pin2, OUTPUT);
- pinMode(pin3, OUTPUT);
- pinMode(pin4, OUTPUT);
- if (!rf22.init())
- Serial.println("RF22 init failed");
- // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
- if (!rf22.setFrequency(431.0))
- Serial.println("setFrequency Fail");
- rf22.setTxPower(RF22_TXPOW_20DBM);
- //1,2,5,8,11,14,17,20 DBM
- //rf22.setModemConfig(RF22::OOK_Rb40Bw335 );
- rf22.setModemConfig(RF22::GFSK_Rb125Fd125);
- //modulation
- // Manually define the routes for this network
- rf22.addRouteTo(DESTINATION_ADDRESS, DESTINATION_ADDRESS);
- sensorVal = analogRead(A0); // Metraei kathe fora kati diaforetiko kai ara bgazei allo seed
- randomSeed(sensorVal);// (μία μόνο φορά μέσα στην setup)
- }
- void loop() {
- // Read Piezo ADC value in, and convert it to a voltage
- int piezoADC = analogRead(PIEZO_PIN);
- float piezoV = piezoADC / 1023.0 * 5.0;
- Serial.print("Vibration: "); // Print the voltage
- Serial.println(piezoV); // Print the voltage.
- if(piezoV >= vib_limit)
- {
- Serial.print("Vibration detected ( ");
- Serial.print(piezoV);
- Serial.println(" )");
- start = millis();
- sendData(level);
- Serial.println("Give a password!!");
- wrongGuess = 0;
- while(millis() - start < 15000)
- {
- tone(buzzerPin, 1000);
- delay(500);
- noTone(buzzerPin);
- delay(500);
- if(Serial.available() > 0)
- {
- //pass = Serial.readString();
- password = Serial.parseInt();
- // say what you got:
- Serial.print("You gave password: ");
- Serial.println(password);
- if(password == correct)
- {
- Serial.println("Correct Password");
- sendData(corPass);
- play();
- play();
- serialFlush();
- break;
- }
- else if(password != correct)
- {
- wrongGuess++;
- Serial.print(limit - wrongGuess);
- Serial.println(" Tries left");
- serialFlush();
- }
- if(wrongGuess == limit)
- {
- Serial.println("Limit Passed");
- Serial.println("Police is on the way!");
- sendData(policeComing);
- police();
- serialFlush();
- break;
- }
- }
- else if(millis() - start >= 15000)
- {
- Serial.println("Timeout!");
- Serial.println("Police is on the way!");
- sendData(policeComing);
- police();
- serialFlush();
- break;
- }
- }
- delay(150); // do not erase
- Serial.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement