Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HX711_ADC.h>
- #include <DFRobotDFPlayerMini.h>
- #include <SoftwareSerial.h>
- #define SERIAL_BAUD 9600
- #define SOFTWARE_SERIAL_BAUD 9600
- //pins:
- const int DOUT_1 = 3; //mcu > HX711 no 1 dout pin
- const int SCK_1 = 2; //mcu > HX711 no 1 sck pin
- const int DOUT_2 = 11; //mcu > HX711 no 2 dout pin
- const int SCK_2 = 10; //mcu > HX711 no 2 sck pin
- const int GRN_2 = A0;
- const int BLU_2 = A1;
- const int RED_2 = A2;
- const int GRN_1 = A3;
- const int BLU_1 = A4;
- const int RED_1 = A5;
- const int POTENTIOMETER_PIN = A6;
- // Define colors as hex values
- const uint32_t RED = 0xFF0000;
- const uint32_t GREEN = 0x00FF00;
- const uint32_t BLUE = 0x0000FF;
- const uint32_t CYAN = 0x00FFFF;
- const uint32_t MAGENTA = 0xFF00FF;
- const uint32_t OFF = 0x000000;
- const int SOFTWARE_SERIAL_RX = 13;
- const int SOFTWARE_SERIAL_TX = 12;
- // Calibration factors for the load cells
- // const float CAL_FACTOR_LOADCELL1 = 376.98;
- // const float CAL_FACTOR_LOADCELL2 = 373.96;
- const float CAL_FACTOR_LOADCELL1 = 433.04;
- const float CAL_FACTOR_LOADCELL2 = 413.49;
- // Minimum weight threshold as per our game weights
- // const int MINIMUM_LOAD = 65;
- unsigned long t = 0;
- // Flag to signal measurement completion
- bool measureComplete;
- int prev_a = 0;
- int prev_b = 0;
- //HX711 constructor (dout pin, sck pin)
- HX711_ADC LoadCell_1(DOUT_1, SCK_1);
- HX711_ADC LoadCell_2(DOUT_2, SCK_2);
- // DFPlayer setup
- SoftwareSerial mySoftwareSerial(SOFTWARE_SERIAL_RX, SOFTWARE_SERIAL_TX); // RX, TX
- DFRobotDFPlayerMini myDFPlayer;
- // Function to set the color of the RGB LED
- void setColor(uint32_t color) {
- uint8_t redValue = (color >> 16) & 0xFF;
- uint8_t greenValue = (color >> 8) & 0xFF;
- uint8_t blueValue = color & 0xFF;
- analogWrite(RED_1, redValue);
- analogWrite(RED_2, redValue);
- analogWrite(GRN_1, greenValue);
- analogWrite(GRN_2, greenValue);
- analogWrite(BLU_1, blueValue);
- analogWrite(BLU_2, blueValue);
- }
- // Ensure that the cell readings are non-zero and compare the weight difference
- bool compareCells(int cell1, int cell2, int rangeLow, int rangeHigh) {
- return ((cell1) && (cell2) && abs((cell1) - (cell2)) >= (rangeLow) && abs((cell1) - (cell2)) <= (rangeHigh));
- }
- // Check if the weight measurement has stabilized
- bool measureCompleteCheck(int cell1curr, int cell1prev, int cell2curr, int cell2prev) {
- return ((abs(cell1curr - cell1prev) == 0) && (abs(cell2curr - cell2prev) == 0));
- }
- void setup() {
- Serial.begin(SERIAL_BAUD);
- mySoftwareSerial.begin(SOFTWARE_SERIAL_BAUD);
- delay(10);
- Serial.println("Starting...");
- Serial.println();
- pinMode(RED_1, OUTPUT);
- pinMode(GRN_1, OUTPUT);
- pinMode(BLU_1, OUTPUT);
- pinMode(RED_2, OUTPUT);
- pinMode(GRN_2, OUTPUT);
- pinMode(BLU_2, OUTPUT);
- LoadCell_1.begin();
- LoadCell_2.begin();
- unsigned long stabilizingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilizing time
- boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
- byte loadcell_1_rdy = 0;
- byte loadcell_2_rdy = 0;
- while ((loadcell_1_rdy + loadcell_2_rdy) < 2) { //run startup, stabilization and tare, both modules simultaniously
- if (!loadcell_1_rdy) loadcell_1_rdy = LoadCell_1.startMultiple(stabilizingtime, _tare);
- if (!loadcell_2_rdy) loadcell_2_rdy = LoadCell_2.startMultiple(stabilizingtime, _tare);
- }
- if (LoadCell_1.getTareTimeoutFlag()) {
- Serial.println("Timeout, check MCU>HX711 no.1 wiring and pin designations");
- }
- if (LoadCell_2.getTareTimeoutFlag()) {
- Serial.println("Timeout, check MCU>HX711 no.2 wiring and pin designations");
- }
- LoadCell_1.setCalFactor(CAL_FACTOR_LOADCELL1); // user set calibration value (float)
- LoadCell_2.setCalFactor(CAL_FACTOR_LOADCELL2); // user set calibration value (float)
- // Initialize the DFPlayer Mini
- if (!myDFPlayer.begin(mySoftwareSerial)) {
- Serial.println("DFPlayer Mini not detected. Please check connections.");
- while (true) {
- ;
- }
- }
- // Set initial volume
- myDFPlayer.volume(20);
- delay(50);
- myDFPlayer.play(2); // Play a short tone to indicate startup complete.
- Serial.println("Startup complete!");
- }
- void loop() {
- static boolean newDataReady = 0;
- const int serialPrintInterval = 1000; //increase value to slow down serial print activity
- // check for new data/start next conversion:
- if (LoadCell_1.update() && LoadCell_2.update()) {
- newDataReady = true;
- }
- //get smoothed value from data set
- if ((newDataReady)) {
- if (millis() > t + serialPrintInterval) {
- int vol = map(analogRead(POTENTIOMETER_PIN), 0, 1023, 0, 30);
- Serial.print("Volume: ");
- Serial.println(vol);
- myDFPlayer.volume(vol);
- int a = LoadCell_1.getData();
- int b = LoadCell_2.getData();
- Serial.print("Weight 1: ");
- Serial.print(a);
- Serial.print(" Weight 2: ");
- Serial.println(b);
- if (measureCompleteCheck(a, prev_a, b, prev_b)) {
- if (compareCells(a, b, 3, 9)) {
- setColor(GREEN);
- myDFPlayer.play(3);
- delay(1000);
- } else if (compareCells(a, b, 9, 14)) {
- setColor(BLUE);
- myDFPlayer.play(3);
- delay(1000);
- } else if (compareCells(a, b, 14, 20)) {
- setColor(CYAN);
- myDFPlayer.play(3);
- delay(1000);
- } else if (compareCells(a, b, 20, 26)) {
- setColor(MAGENTA);
- myDFPlayer.play(3);
- delay(1000);
- }
- } else {
- Serial.println("Measurement still in progress...");
- setColor(OFF);
- }
- prev_a = a;
- prev_b = b;
- newDataReady = 0;
- t = millis();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment