Advertisement
makispaiktis

Tx 2

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