electrotwelve

Untitled

Aug 17th, 2024
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.70 KB | Source Code | 0 0
  1. #include <HX711_ADC.h>
  2. #include <DFRobotDFPlayerMini.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. #define SERIAL_BAUD 9600
  6. #define SOFTWARE_SERIAL_BAUD 9600
  7.  
  8. //pins:
  9. const int DOUT_1 = 3;   //mcu > HX711 no 1 dout pin
  10. const int SCK_1 = 2;    //mcu > HX711 no 1 sck pin
  11. const int DOUT_2 = 11;  //mcu > HX711 no 2 dout pin
  12. const int SCK_2 = 10;   //mcu > HX711 no 2 sck pin
  13.  
  14. const int GRN_2 = A0;
  15. const int BLU_2 = A1;
  16. const int RED_2 = A2;
  17. const int GRN_1 = A3;
  18. const int BLU_1 = A4;
  19. const int RED_1 = A5;
  20. const int POTENTIOMETER_PIN = A6;
  21.  
  22. // Define colors as hex values
  23. const uint32_t RED = 0xFF0000;
  24. const uint32_t GREEN = 0x00FF00;
  25. const uint32_t BLUE = 0x0000FF;
  26. const uint32_t CYAN = 0x00FFFF;
  27. const uint32_t MAGENTA = 0xFF00FF;
  28. const uint32_t OFF = 0x000000;
  29.  
  30. const int SOFTWARE_SERIAL_RX = 13;
  31. const int SOFTWARE_SERIAL_TX = 12;
  32.  
  33. // Calibration factors for the load cells
  34. // const float CAL_FACTOR_LOADCELL1 = 376.98;
  35. // const float CAL_FACTOR_LOADCELL2 = 373.96;
  36. const float CAL_FACTOR_LOADCELL1 = 433.04;
  37. const float CAL_FACTOR_LOADCELL2 = 413.49;
  38.  
  39. // Minimum weight threshold as per our game weights
  40. // const int MINIMUM_LOAD = 65;
  41.  
  42. unsigned long t = 0;
  43.  
  44. // Flag to signal measurement completion
  45. bool measureComplete;
  46.  
  47. int prev_a = 0;
  48. int prev_b = 0;
  49.  
  50. //HX711 constructor (dout pin, sck pin)
  51. HX711_ADC LoadCell_1(DOUT_1, SCK_1);
  52. HX711_ADC LoadCell_2(DOUT_2, SCK_2);
  53.  
  54. // DFPlayer setup
  55. SoftwareSerial mySoftwareSerial(SOFTWARE_SERIAL_RX, SOFTWARE_SERIAL_TX);  // RX, TX
  56. DFRobotDFPlayerMini myDFPlayer;
  57.  
  58. // Function to set the color of the RGB LED
  59. void setColor(uint32_t color) {
  60.   uint8_t redValue = (color >> 16) & 0xFF;
  61.   uint8_t greenValue = (color >> 8) & 0xFF;
  62.   uint8_t blueValue = color & 0xFF;
  63.  
  64.   analogWrite(RED_1, redValue);
  65.   analogWrite(RED_2, redValue);
  66.   analogWrite(GRN_1, greenValue);
  67.   analogWrite(GRN_2, greenValue);
  68.   analogWrite(BLU_1, blueValue);
  69.   analogWrite(BLU_2, blueValue);
  70. }
  71.  
  72. // Ensure that the cell readings are non-zero and compare the weight difference
  73. bool compareCells(int cell1, int cell2, int rangeLow, int rangeHigh) {
  74.   return ((cell1) && (cell2) && abs((cell1) - (cell2)) >= (rangeLow) && abs((cell1) - (cell2)) <= (rangeHigh));
  75. }
  76.  
  77. // Check if the weight measurement has stabilized
  78. bool measureCompleteCheck(int cell1curr, int cell1prev, int cell2curr, int cell2prev) {
  79.   return ((abs(cell1curr - cell1prev) == 0) && (abs(cell2curr - cell2prev) == 0));
  80. }
  81.  
  82. void setup() {
  83.   Serial.begin(SERIAL_BAUD);
  84.   mySoftwareSerial.begin(SOFTWARE_SERIAL_BAUD);
  85.   delay(10);
  86.  
  87.   Serial.println("Starting...");
  88.   Serial.println();
  89.  
  90.   pinMode(RED_1, OUTPUT);
  91.   pinMode(GRN_1, OUTPUT);
  92.   pinMode(BLU_1, OUTPUT);
  93.   pinMode(RED_2, OUTPUT);
  94.   pinMode(GRN_2, OUTPUT);
  95.   pinMode(BLU_2, OUTPUT);
  96.  
  97.   LoadCell_1.begin();
  98.   LoadCell_2.begin();
  99.  
  100.   unsigned long stabilizingtime = 2000;  // tare preciscion can be improved by adding a few seconds of stabilizing time
  101.  
  102.   boolean _tare = true;  //set this to false if you don't want tare to be performed in the next step
  103.  
  104.   byte loadcell_1_rdy = 0;
  105.   byte loadcell_2_rdy = 0;
  106.  
  107.   while ((loadcell_1_rdy + loadcell_2_rdy) < 2) {  //run startup, stabilization and tare, both modules simultaniously
  108.     if (!loadcell_1_rdy) loadcell_1_rdy = LoadCell_1.startMultiple(stabilizingtime, _tare);
  109.     if (!loadcell_2_rdy) loadcell_2_rdy = LoadCell_2.startMultiple(stabilizingtime, _tare);
  110.   }
  111.  
  112.   if (LoadCell_1.getTareTimeoutFlag()) {
  113.     Serial.println("Timeout, check MCU>HX711 no.1 wiring and pin designations");
  114.   }
  115.   if (LoadCell_2.getTareTimeoutFlag()) {
  116.     Serial.println("Timeout, check MCU>HX711 no.2 wiring and pin designations");
  117.   }
  118.  
  119.   LoadCell_1.setCalFactor(CAL_FACTOR_LOADCELL1);  // user set calibration value (float)
  120.   LoadCell_2.setCalFactor(CAL_FACTOR_LOADCELL2);  // user set calibration value (float)
  121.  
  122.   // Initialize the DFPlayer Mini
  123.   if (!myDFPlayer.begin(mySoftwareSerial)) {
  124.     Serial.println("DFPlayer Mini not detected. Please check connections.");
  125.     while (true) {
  126.       ;
  127.     }
  128.   }
  129.  
  130.   // Set initial volume
  131.   myDFPlayer.volume(20);
  132.   delay(50);
  133.   myDFPlayer.play(2);  // Play a short tone to indicate startup complete.
  134.  
  135.   Serial.println("Startup complete!");
  136. }
  137.  
  138. void loop() {
  139.   static boolean newDataReady = 0;
  140.   const int serialPrintInterval = 1000;  //increase value to slow down serial print activity
  141.  
  142.   // check for new data/start next conversion:
  143.   if (LoadCell_1.update() && LoadCell_2.update()) {
  144.     newDataReady = true;
  145.   }
  146.  
  147.   //get smoothed value from data set
  148.   if ((newDataReady)) {
  149.     if (millis() > t + serialPrintInterval) {
  150.       int vol = map(analogRead(POTENTIOMETER_PIN), 0, 1023, 0, 30);
  151.       Serial.print("Volume: ");
  152.       Serial.println(vol);
  153.       myDFPlayer.volume(vol);
  154.  
  155.       int a = LoadCell_1.getData();
  156.       int b = LoadCell_2.getData();
  157.       Serial.print("Weight 1: ");
  158.       Serial.print(a);
  159.       Serial.print("   Weight 2: ");
  160.       Serial.println(b);
  161.  
  162.       if (measureCompleteCheck(a, prev_a, b, prev_b)) {
  163.         if (compareCells(a, b, 3, 9)) {
  164.           setColor(GREEN);
  165.           myDFPlayer.play(3);
  166.           delay(1000);
  167.         } else if (compareCells(a, b, 9, 14)) {
  168.           setColor(BLUE);
  169.           myDFPlayer.play(3);
  170.           delay(1000);
  171.         } else if (compareCells(a, b, 14, 20)) {
  172.           setColor(CYAN);
  173.           myDFPlayer.play(3);
  174.           delay(1000);
  175.         } else if (compareCells(a, b, 20, 26)) {
  176.           setColor(MAGENTA);
  177.           myDFPlayer.play(3);
  178.           delay(1000);
  179.         }
  180.       } else {
  181.         Serial.println("Measurement still in progress...");
  182.         setColor(OFF);
  183.       }
  184.  
  185.       prev_a = a;
  186.       prev_b = b;
  187.  
  188.       newDataReady = 0;
  189.       t = millis();
  190.     }
  191.   }
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment