Advertisement
makispaiktis

RF22 Distance

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