The following code, measures two load cells and then depending on the weight difference, prints out a case number and plays a specific audio file.
I am not able to figure out why this won't loop. I see only the first reading and then it stops with nothing on the screen. What am I missing? I tried the same logic with if-else-if loops and that works flawlessly.
#include <HX711.h>
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
#define SOFTWARE_SERIAL_BAUD 9600
#define SERIAL_BAUD 9600
#define MIN_WT 45
#define MAX_WT 130
#define BUSY LOW
#define VOLUME 10
const uint8_t dataPin1 = 7;
const uint8_t clockPin1 = 8;
const uint8_t dataPin2 = 2;
const uint8_t clockPin2 = 3;
const uint8_t green2 = A5;
const uint8_t blue2 = A4;
const uint8_t red2 = A3;
const uint8_t green1 = A1;
const uint8_t blue1 = A2;
const uint8_t red1 = A0;
const uint8_t busyPin = 6;
const uint8_t rxPin = 5;
const uint8_t txPin = 4;
int wt1, wt2, wt_diff;
int prev_wt1 = 0, prev_wt2 = 0;
bool measureComplete;
// Define the state enumeration
enum State {
IDLE,
MEASURE,
COMPARE,
ACTION,
UPDATE
};
State currentState = IDLE;
// 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; //Green and Blue at full brightness
const uint32_t magenta = 0xFF00FF; //Red and Blue at full brightness
const uint32_t lightBlue = 0x4CCCFF; //Red at around 30 percent of brightness, green up high at 80 percent, and blue at full brightness
const uint32_t purple = 0xFF99FF; //Red and blue are all the way up, with green at 60 percent
const uint32_t white = 0xFFFFFF; //All colors at full brightness
const uint32_t off = 0x000000;
// Calibration constants for the load cells
const float calibrationCell1 = 1932.124511;
const float calibrationCell2 = 1997.294799;
const long offsetCell1 = 169060;
const long offsetCell2 = 72511;
HX711 scale1, scale2;
SoftwareSerial mySoftwareSerial(rxPin, txPin);
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(red1, redValue);
analogWrite(red2, redValue);
analogWrite(green1, greenValue);
analogWrite(green2, greenValue);
analogWrite(blue1, blueValue);
analogWrite(blue2, blueValue);
}
// Play DFR audio
void playAudio(uint8_t fileNumber) {
myDFPlayer.playMp3Folder(fileNumber);
// delay(delayInSeconds * 1000);
while (digitalRead(busyPin) != BUSY) {
; // // This loop waits for the DFR player to get BUSY.
}
while (digitalRead(busyPin) == BUSY) {
; // This loop executes while the DFR BUSY pin is LOW indicating playback in progress.
}
}
void setup() {
//Initialize serial and wait for port to open
Serial.begin(SERIAL_BAUD);
mySoftwareSerial.begin(SOFTWARE_SERIAL_BAUD);
delay(100);
Serial.println("Starting up...");
pinMode(red1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(blue1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(blue2, OUTPUT);
pinMode(busyPin, INPUT);
scale1.begin(dataPin1, clockPin1);
scale2.begin(dataPin2, clockPin2);
scale1.set_offset(offsetCell1);
scale1.set_scale(calibrationCell1);
scale2.set_offset(offsetCell2);
scale2.set_scale(calibrationCell2);
// // reset the scale to zero = 0
scale1.tare(20);
scale2.tare(20);
// Initialize the DFPlayer Mini
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println("DFPlayer Mini not detected. Please check connections and if SD card is inserted.");
while (true) {
;
}
}
myDFPlayer.volume(VOLUME);
delay(250);
setColor(blue);
playAudio(1);
Serial.println("Start up complete!");
}
void loop() {
switch (currentState) {
case IDLE:
if (scale1.is_ready() && scale2.is_ready()) {
currentState = MEASURE;
}
break;
case MEASURE:
wt1 = int(scale1.get_units(10));
wt1 = abs(wt1);
wt2 = int(scale2.get_units(10));
wt2 = abs(wt2);
if ((wt1 > MAX_WT) || (wt1 < MIN_WT)) {
wt1 = 0;
}
if ((wt2 > MAX_WT) || (wt2 < MIN_WT)) {
wt2 = 0;
}
Serial.println("");
Serial.print("Wt1 = ");
Serial.println(wt1);
Serial.print("Wt2 = ");
Serial.println(wt2);
currentState = COMPARE;
break;
case COMPARE:
int wt1_curr_prev_diff = wt1 - prev_wt1;
int wt2_curr_prev_diff = wt2 - prev_wt2;
if ((wt1 > 0) && (wt2 > 0) && (wt1_curr_prev_diff == 0) && (wt2_curr_prev_diff == 0)) {
measureComplete = true;
wt_diff = wt2 - wt1;
wt_diff = abs(wt_diff);
} else {
measureComplete = false;
}
Serial.print("measureComplete = ");
Serial.println(measureComplete);
currentState = ACTION;
break;
case ACTION:
if (measureComplete) {
if (wt1 > 0 && wt2 > 0) {
if (wt_diff > 3 && wt_diff < 9) {
Serial.println("Case 1");
setColor(green);
playAudio(2);
} else if (wt_diff > 9 && wt_diff < 14) {
Serial.println("Case 2");
setColor(blue);
playAudio(3);
} else if (wt_diff > 14 && wt_diff < 19) {
Serial.println("Case 3");
setColor(purple);
playAudio(4);
} else if (wt_diff > 19 && wt_diff < 26) {
Serial.println("Case 4");
setColor(magenta);
playAudio(5);
} else if (wt_diff > 26 && wt_diff < 32) {
Serial.println("Case 5");
setColor(white);
playAudio(6);
} else {
setColor(lightBlue);
}
}
}
currentState = UPDATE;
break;
case UPDATE:
prev_wt1 = wt1;
prev_wt2 = wt2;
currentState = IDLE; // Go back to the initial state
break;
default:
currentState = IDLE;
break;
}
}
// -- END OF FILE --
On running this code, I see the following and this does not repeat as it should.
Starting up...
Start up complete!
wt1 = 0
wt2 = 0
measureComplete = 0