Advertisement
makispaiktis

Transmitter 2

May 15th, 2021 (edited)
782
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 13
  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 = 2;
  20.  
  21. float start;
  22.  
  23. // LEDs
  24. int pin1 = 4;
  25. int pin2 = 5;
  26.  
  27. // defines distance sensor variables
  28. long duration; // variable for the duration of sound wave travel
  29. int distance = 5; // variable for the distance measurement
  30.  
  31.  
  32. void setup() {
  33.  
  34.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  35.   pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  36.   pinMode(pin1, OUTPUT);
  37.   pinMode(pin2, OUTPUT);
  38.  
  39.  
  40.   Serial.begin(9600);
  41.   if (!rf22.init())
  42.     Serial.println("RF22 init failed");
  43.   // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
  44.  
  45.   if (!rf22.setFrequency(431.0))
  46.     Serial.println("setFrequency Fail");
  47.   rf22.setTxPower(RF22_TXPOW_20DBM);
  48.   //1,2,5,8,11,14,17,20 DBM
  49.   //rf22.setModemConfig(RF22::OOK_Rb40Bw335  );
  50.   rf22.setModemConfig(RF22::GFSK_Rb125Fd125);
  51.   //modulation
  52.  
  53.   // Manually define the routes for this network
  54.   rf22.addRouteTo(DESTINATION_ADDRESS, DESTINATION_ADDRESS);
  55.   sensorVal = analogRead(A0);                             // Metraei kathe fora kati diaforetiko kai ara bgazei allo seed
  56.   randomSeed(sensorVal);// (μία μόνο φορά μέσα στην setup)          
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. void loop() {
  65.  
  66.   // Clears the trigPin condition
  67.   digitalWrite(trigPin, LOW);
  68.   delayMicroseconds(2);
  69.   // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  70.   digitalWrite(trigPin, HIGH);
  71.   delayMicroseconds(10);
  72.   digitalWrite(trigPin, LOW);
  73.   // Reads the echoPin, returns the sound wave travel time in microseconds
  74.   duration = pulseIn(echoPin, HIGH);
  75.   // Calculating the distance
  76.   distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  77.  
  78.  
  79.  
  80.      
  81.   if(millis() - start >= 2000)
  82.   {
  83.     digitalWrite(pin1, LOW);
  84.     digitalWrite(pin2, LOW);
  85.   }
  86.    
  87.  
  88.  
  89.   Serial.print("Distance: ");
  90.   Serial.println(distance);
  91.  
  92.   if(distance <= dist_limit)
  93.   {
  94.  
  95.  
  96.     start = millis();
  97.     Serial.println(start);
  98.     digitalWrite(pin1, HIGH);
  99.     digitalWrite(pin2, HIGH);
  100.    
  101.     char data_read[RF22_ROUTER_MAX_MESSAGE_LEN];
  102.     uint8_t data_send[RF22_ROUTER_MAX_MESSAGE_LEN];
  103.     memset(data_read, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  104.     memset(data_send, '\0', RF22_ROUTER_MAX_MESSAGE_LEN);
  105.     sprintf(data_read, "%d", level);
  106.     data_read[RF22_ROUTER_MAX_MESSAGE_LEN - 1] = '\0';
  107.     memcpy(data_send, data_read, RF22_ROUTER_MAX_MESSAGE_LEN);
  108.  
  109.    
  110.     successful_packet = false;
  111.     while (!successful_packet)
  112.     {    
  113.       if (rf22.sendtoWait(data_send, sizeof(data_send), DESTINATION_ADDRESS) != RF22_ROUTER_ERROR_NONE)
  114.       {
  115.         Serial.println("sendtoWait failed");
  116.         randNumber=random(200,max_delay);
  117.         Serial.println(randNumber);
  118.         delay(randNumber);
  119.       }
  120.       else
  121.       {
  122.         successful_packet = true;
  123.         Serial.println("sendtoWait Succesful");
  124.       }
  125.     }
  126.   }
  127.  
  128.   delay(150);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement