Advertisement
makispaiktis

Tx 1

May 16th, 2021 (edited)
523
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 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 = 20;
  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. void sendData(int data){
  26.  
  27.     char data_read[RF22_ROUTER_MAX_MESSAGE_LEN];
  28.     uint8_t data_send[RF22_ROUTER_MAX_MESSAGE_LEN];
  29.     memset(data_read, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  30.     memset(data_send, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  31.     sprintf(data_read, "%d", data);
  32.     data_read[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
  33.     memcpy(data_send, data_read, RF22_ROUTER_MAX_MESSAGE_LEN);
  34.  
  35.    
  36.    
  37.     successful_packet = false;
  38.     while (!successful_packet)
  39.     {    
  40.       if (rf22.sendtoWait(data_send, sizeof(data_send), DESTINATION_ADDRESS) != RF22_ROUTER_ERROR_NONE)
  41.       {
  42.         Serial.println("sendtoWait failed");
  43.         randNumber=random(200,max_delay);
  44.         Serial.println(randNumber);
  45.         delay(randNumber);
  46.       }
  47.       else
  48.       {
  49.         successful_packet = true;
  50.         Serial.println("sendtoWait Succesful");
  51.       }
  52.     }
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. void setup() {
  63.  
  64.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  65.   pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  66.  
  67.   Serial.begin(9600);
  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 loop() {
  87.  
  88.   // Clears the trigPin condition
  89.   digitalWrite(trigPin, LOW);
  90.   delayMicroseconds(2);
  91.   // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  92.   digitalWrite(trigPin, HIGH);
  93.   delayMicroseconds(10);
  94.   digitalWrite(trigPin, LOW);
  95.   // Reads the echoPin, returns the sound wave travel time in microseconds
  96.   duration = pulseIn(echoPin, HIGH);
  97.   // Calculating the distance
  98.   distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  99.  
  100.  
  101.   Serial.print("Distance: ");
  102.   Serial.println(distance);
  103.  
  104.   if(distance <= dist_limit)
  105.   {
  106.     sendData(level);
  107.     delay(150);
  108.   }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement