Advertisement
pleasedontcode

**Sensor Measurement** rev_12

Jan 22nd, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Sensor Measurement**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-22 14:27:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* serial print output with errors */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  27. #include <LoadCell.h>  // Assuming LoadCell library is needed for LoadCell.update() and LoadCell.getData()
  28. #include <Arduino.h>   // Required for Arduino functions like Serial
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void temp_density_me(void *pvParameters); // Prototype for the new function
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. // Assuming LoadCell and other necessary instances are defined here
  37. LoadCell LoadCell; // Uncomment and configure as necessary
  38. SemaphoreHandle_t mutex; // Uncomment if using FreeRTOS semaphores
  39.  
  40. void setup(void)
  41. {
  42.     // put your setup code here, to run once:
  43.     Serial.begin(115200); // Initialize serial communication
  44.     // Initialize LoadCell and other necessary components here
  45.     if (!LoadCell.begin()) {
  46.         Serial.println("Error: LoadCell initialization failed!");
  47.     }
  48.     // LoadCell.begin(); // Uncomment and configure as necessary
  49.     // xSemaphoreCreateMutex(); // Uncomment if using FreeRTOS semaphores
  50. }
  51.  
  52. void loop(void)
  53. {
  54.     // put your main code here, to run repeatedly:
  55.     // You might want to create a task for temperature and density measurement
  56.     // xTaskCreate(temp_density_me, "TempDensityTask", 2048, NULL, 1, NULL); // Uncomment and configure as necessary
  57. }
  58.  
  59. /****** TEMPERATURE AND DENSITY MEASUREMENT FUNCTION *****/
  60. void temp_density_me(void *pvParameters) {
  61.     // temperature and density measurement
  62.     static boolean newDataReady = 0;
  63.     for (;;) {
  64.         if (xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) { // check for new data/start next conversion:
  65.             if (LoadCell.update()) {
  66.                 newDataReady = true; // get smoothed value from the dataset:
  67.             } else {
  68.                 Serial.println("Error: LoadCell update failed!");
  69.             }
  70.            
  71.             if (newDataReady) {
  72.                 float i = LoadCell.getData();
  73.                 sensors.requestTemperatures();
  74.                 float temperature_Celsius = sensors.getTempCByIndex(0);
  75.                
  76.                 if (temperature_Celsius == DEVICE_DISCONNECTED) {
  77.                     Serial.println("Error: Temperature sensor disconnected!");
  78.                 } else {
  79.                     float iSG = (i / (258.6 - ((i / 258.2) * 227.1))) + 1; // convert measured Plato to SG
  80.                     float iRSG = iSG / (1 + kt * (ti - temperature_Celsius)); // real density SG
  81.                     float iR = (((182.4601 * iRSG - 775.6821) * iRSG + 1262.7794) * iRSG - 669.5622); // real density Plato
  82.                    
  83.                     Serial.print("Плотность измеренная: ");
  84.                     Serial.print(i);
  85.                     Serial.print(" %");
  86.                     Serial.print("    ");
  87.                     Serial.print(iSG, 5);
  88.                     Serial.print("    ");
  89.                     Serial.println(iRSG, 5);
  90.                    
  91.                     Serial.print("Плотность скорректированная: ");
  92.                     Serial.print(iR, 2);
  93.                     Serial.println(" %");
  94.                     Serial.print("Температура: ");
  95.                     Serial.print(temperature_Celsius);
  96.                     Serial.println(" C");
  97.                     Serial.println();
  98.                 }
  99.  
  100.                 newDataReady = 0;
  101.             }
  102.             xSemaphoreGive(mutex);
  103.         } else {
  104.             Serial.println("Error: Failed to take semaphore!");
  105.         }
  106.         vTaskDelay(5000);
  107.     }
  108.     vTaskDelete(NULL);
  109. }
  110.  
  111. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement