makispaiktis

Transmitter 1

May 14th, 2021 (edited)
804
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 12
  5. #define DESTINATION_ADDRESS 5
  6.  
  7. #define echoPin 8 // attach pin D2 Arduino to pin Echo of HC-SR04
  8. #define trigPin 9 //attach pin D3 Arduino to pin Trig of HC-SR04
  9.  
  10.  
  11. RF22Router rf22(MY_ADDRESS);
  12.  
  13. // Aloha
  14. int sensorVal = 10;
  15. long randNumber;
  16. boolean successful_packet = false;
  17. int max_delay=3000;
  18. int dist_limit = 50;
  19. int level = 1;
  20.  
  21. // defines distance sensor variables
  22. long duration; // variable for the duration of sound wave travel
  23. int distance = 5; // variable for the distance measurement
  24.  
  25.  
  26. void setup() {
  27.  
  28.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  29.   pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  30.  
  31.   Serial.begin(9600);
  32.   if (!rf22.init())
  33.     Serial.println("RF22 init failed");
  34.   // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
  35.  
  36.   if (!rf22.setFrequency(431.0))
  37.     Serial.println("setFrequency Fail");
  38.   rf22.setTxPower(RF22_TXPOW_20DBM);
  39.   //1,2,5,8,11,14,17,20 DBM
  40.   //rf22.setModemConfig(RF22::OOK_Rb40Bw335  );
  41.   rf22.setModemConfig(RF22::GFSK_Rb125Fd125);
  42.   //modulation
  43.  
  44.   // Manually define the routes for this network
  45.   rf22.addRouteTo(DESTINATION_ADDRESS, DESTINATION_ADDRESS);
  46.   sensorVal = analogRead(A0);                             // Metraei kathe fora kati diaforetiko kai ara bgazei allo seed
  47.   randomSeed(sensorVal);// (μία μόνο φορά μέσα στην setup)          
  48. }
  49.  
  50. void loop() {
  51.  
  52.   // Clears the trigPin condition
  53.   digitalWrite(trigPin, LOW);
  54.   delayMicroseconds(2);
  55.   // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  56.   digitalWrite(trigPin, HIGH);
  57.   delayMicroseconds(10);
  58.   digitalWrite(trigPin, LOW);
  59.   // Reads the echoPin, returns the sound wave travel time in microseconds
  60.   duration = pulseIn(echoPin, HIGH);
  61.   // Calculating the distance
  62.   distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  63.  
  64.  
  65.   Serial.print("Distance: ");
  66.   Serial.println(distance);
  67.  
  68.   if(distance <= dist_limit)
  69.   {
  70.     Serial.println("MPHKA");
  71.    
  72.     char data_read[RF22_ROUTER_MAX_MESSAGE_LEN];
  73.     uint8_t data_send[RF22_ROUTER_MAX_MESSAGE_LEN];
  74.     memset(data_read, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  75.     memset(data_send, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  76.     sprintf(data_read, "%d", level);
  77.     data_read[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
  78.     memcpy(data_send, data_read, RF22_ROUTER_MAX_MESSAGE_LEN);
  79.  
  80.    
  81.     successful_packet = false;
  82.     while (!successful_packet)
  83.     {    
  84.       if (rf22.sendtoWait(data_send, sizeof(data_send), DESTINATION_ADDRESS) != RF22_ROUTER_ERROR_NONE)
  85.       {
  86.         Serial.println("sendtoWait failed");
  87.         randNumber=random(200,max_delay);
  88.         Serial.println(randNumber);
  89.         delay(randNumber);
  90.       }
  91.       else
  92.       {
  93.         successful_packet = true;
  94.         Serial.println("sendtoWait Succesful");
  95.       }
  96.     }
  97.   }
  98.  
  99.   delay(150);
  100. }
RAW Paste Data