Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <pthread.h>
  2. #include "DHT.h";
  3. #include "NewPing.h"
  4.  
  5. #define SONARS 3
  6. #define DHTPIN 4
  7. #define DHTTYPE DHT22
  8. #define TRIGGER_PIN 5
  9. #define TRIGGER_PIN1 17
  10. #define TRIGGER_PIN2 14
  11. #define ECHO_PIN 16
  12. #define ECHO_PIN1 18
  13. #define ECHO_PIN2 13
  14. #define MAX_DISTANCE 150
  15. #define MIN_DISTANCE 15
  16. #define MAX_DUTY 512
  17. #define PWM_FREQUENCY 2.5e3
  18. #define ITERATIONS 5
  19.  
  20. NewPing sonars[SONARS] = {
  21.     NewPing(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE),
  22.     NewPing(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE),
  23.     NewPing(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE)};
  24. DHT dht(DHTPIN, DHTTYPE);
  25.  
  26. const int vibroPins[SONARS] = {27, 32, 33};
  27. float hum, temp,
  28.     distance[SONARS],
  29.     soundsp, soundcm;
  30.  
  31. void serialPrint(void);
  32.  
  33. void *measureThread(void *)
  34. {
  35.     while (true)
  36.     {
  37.         hum = dht.readHumidity();
  38.         temp = dht.readTemperature();
  39.  
  40.         soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
  41.         soundcm = soundsp / 10e3;
  42.         delay(1000); // Temperature and humidity update delay
  43.         serialPrint();
  44.     }
  45. }
  46.  
  47. void *sonarThread(void *sonarId)
  48. {
  49.     int id = (int)sonarId;
  50.     float dutyCycle;
  51.     while (true)
  52.     {
  53.         delay(1e3); // Sonars check delay
  54.         distance[id] = (sonars[id].ping_median(ITERATIONS) / 2.0) * soundcm;
  55.         dutyCycle = 0;
  56.         if (distance[id] <= MAX_DISTANCE && distance[id] >= MIN_DISTANCE)
  57.         {
  58.             dutyCycle = (int)((distance[id] - 150) * (MAX_DUTY - 50) / (-150) + 50);
  59.         }
  60.         else if (distance[id] <= MIN_DISTANCE)
  61.         {
  62.             dutyCycle = MAX_DUTY;
  63.         }
  64.         ledcWrite(id, dutyCycle);
  65.     }
  66. }
  67.  
  68. void setup()
  69. {
  70.     pthread_t sonars[SONARS], measure;
  71.     Serial.begin(115200);
  72.     dht.begin();
  73.     pthread_create(&measure, NULL, measureThread, nullptr);
  74.     for (int i = 0; i < SONARS; i++)
  75.     {
  76.         ledcSetup(i, PWM_FREQUENCY, 10);
  77.         ledcAttachPin(vibroPins[i], i);
  78.         pthread_create(&sonars[i], NULL, sonarThread, (void *)i);
  79.     }
  80. }
  81.  
  82. void loop() {}
  83.  
  84. void serialPrint(void)
  85. {
  86.     char tmp[100];
  87.     sprintf(tmp, "Sound %f m/s, Humid: %f%%, Temp: %f", soundsp, hum, temp);
  88.     Serial.println(tmp);
  89.     sprintf(tmp, "Distance: %f, Distance1: %f, Distance2: %f", distance[0], distance[1], distance[2]);
  90.     Serial.println(tmp);
  91.     Serial.println(" ");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement