Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <pthread.h>
  2. #include "DHT.h";
  3. #include "NewPing.h"
  4.  
  5. #define DHTPIN 4
  6. #define DHTTYPE DHT22
  7. #define TRIGGER_PIN 5
  8. #define TRIGGER_PIN1 17
  9. #define TRIGGER_PIN2 14
  10. #define ECHO_PIN 16
  11. #define ECHO_PIN1 18
  12. #define ECHO_PIN2 13
  13. #define MAX_DISTANCE 150
  14. #define ITERATIONS 5
  15.  
  16. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  17. NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);
  18. NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
  19. DHT dht(DHTPIN, DHTTYPE);
  20.  
  21. float hum, temp,
  22.     duration, duration1, duration2,
  23.     distance, distance1, distance2,
  24.     soundsp, soundcm;
  25.  
  26. void serialPrint(void);
  27.  
  28. void *measureThread(void *)
  29. {
  30.     while (true)
  31.     {
  32.         hum = dht.readHumidity();
  33.         temp = dht.readTemperature();
  34.  
  35.         soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
  36.         soundcm = soundsp / 10e3;
  37.         delay(1000); // Temperature and humidity update delay
  38.     }
  39. }
  40.  
  41. void *sonarsThread(void *)
  42. {
  43.     while (true)
  44.     {
  45.         delay(1e3); // Sonars check delay
  46.         float distance = (sonar.ping_median(ITERATIONS) / 2) * soundcm;
  47.         float distance1 = (sonar1.ping_median(ITERATIONS) / 2) * soundcm;
  48.         float distance2 = (sonar2.ping_median(ITERATIONS) / 2) * soundcm;
  49.  
  50.         calcDistanceAndVibrate(distance, 27);
  51.         calcDistanceAndVibrate(distance1, 32);
  52.         calcDistanceAndVibrate(distance2, 33);
  53.         serialPrint();
  54.     }
  55. }
  56.  
  57. void setup()
  58. {
  59.     pthread_t sonars, measure;
  60.     Serial.begin(115200);
  61.     dht.begin();
  62.     pinMode(33, OUTPUT);
  63.     pinMode(32, OUTPUT);
  64.     pinMode(27, OUTPUT);
  65.     pthread_create(&measure, NULL, measureThread, nullptr);
  66.     pthread_create(&sonars, NULL, sonarsThread, nullptr);
  67. }
  68.  
  69. void calcDistanceAndVibrate(float dis, const int pin)
  70. {
  71.     if (dis > MAX_DISTANCE || dis == 0)
  72.     {
  73.         digitalWrite(pin, LOW); //це перетворення відстані в вібрації тіпа як на бейсболці якій я робив тоді
  74.     }
  75.     else
  76.     {
  77.         digitalWrite(pin, HIGH);
  78.         delay(100);
  79.         digitalWrite(pin, LOW);
  80.         delay(dis * dis / 15);
  81.     }
  82. }
  83.  
  84. void loop() {}
  85.  
  86. void serialPrint(void)
  87. {
  88.     Serial.print("Sound: ");
  89.     Serial.print(soundsp);
  90.     Serial.print(" m/s, ");
  91.     Serial.print("Humid: ");
  92.     Serial.print(hum);
  93.     Serial.print(" %, Temp: ");
  94.     Serial.print(temp);
  95.     Serial.print(" C, ");
  96.     Serial.print("Distance: ");
  97.  
  98.     Serial.print(distance);
  99.     Serial.print("см ");
  100.     Serial.print(distance1);
  101.     Serial.print("см ");
  102.     Serial.print(distance2);
  103.     Serial.print("см ");
  104.  
  105.     Serial.println(" ");
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement