Hello, I'm doing a project in school and the exhibit for it is the next day. I'm writing to get help with my problem in the system. First of all, when you put the input in the system, the load cell should immediately get the weight and sort it if it's lighter or heavier, but as I am experiencing, it does not. In the coin counting portion, the system is also struggling to calculate even with the limited number of data values. What should I do?
Here's my code for reference:
#include <LiquidCrystal_I2C.h>
#include <HX711_ADC.h>
#include <Wire.h>
#include <Stepper.h>
#include <Servo.h>
#define BUTTON_PIN 12
HX711_ADC LoadCell(2, 3);
int capasensor = A0;
int value;
Servo myservo1;
Servo myservo2;
LiquidCrystal_I2C lcd(0x27, 20, 4);
int IRSensor = A2;
unsigned int buttonStatus = LOW;
const int stepsPerRevolution = 300;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
enum { Start,
FullBin,
NotFullBin,
Metal,
Paper,
Plastic,
CoinCount,
CoinRelease };
unsigned char RVMState;
void setup() {
Serial.begin(9600);
myservo1.attach(5); //load cell servo
myservo1.write(180);
myservo2.attach(13); //coin servo
pinMode(BUTTON_PIN, INPUT);
pinMode(capasensor, INPUT);
myStepper.setSpeed(30);
LoadCell.begin();
LoadCell.start(2000);
LoadCell.setCalFactor(600);
lcd.begin();
lcd.backlight();
delay(1000);
}
void loop() {
int binStatus = digitalRead(IRSensor);
int cycle = 1;
int data1 = 0;
int data2 = 0;
int data3 = 0;
int data4 = 0;
int data5 = 0;
int data6 = 0;
int data7 = 0;
int data8 = 0;
int data9 = 0;
int data10 = 0;
int coinCount = 0;
int coin = 0;
int pos = 0;
int count = 0;
int howmanycoin = 0;
switch (RVMState) {
case Start:
Serial.println("START");
lcd.setCursor(0, 0);
lcd.print("Please wait!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Checking bins...");
delay(1000);
if (digitalRead(IRSensor) == 0) { //if Irsensor sensed something
RVMState = FullBin;
break;
} else { //Irsensor did not detect something
RVMState = NotFullBin;
break;
}
case FullBin: //Stay until machine turned off
Serial.println("FullBin");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">>Bin is Full<<");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Please empty the");
delay(1000);
lcd.setCursor(0, 2);
lcd.print("bins or contact");
delay(1000);
lcd.setCursor(0, 3);
lcd.print("Customer Service...");
delay(2000);
break;
case NotFullBin:
lcd.clear();
Serial.println("NotFullBin");
lcd.setCursor(0, 0);
lcd.print(">>Bin is Ready<<");
delay(500);
lcd.setCursor(0, 1);
lcd.print("Please insert trash!");
delay(500);
lcd.setCursor(0, 2);
lcd.print("Press the button");
delay(500);
lcd.setCursor(0, 3);
lcd.print("if d-o-n-e!");
delay(5000);
LoadCell.update();
if (digitalRead(BUTTON_PIN) == HIGH) { //for counting coins
Serial.println("pushed");
RVMState = CoinCount;
break;
}
if (digitalRead(capasensor) == 1) { //for metal
RVMState = Metal;
break;
}
if (digitalRead(capasensor) == 0) { //no metal
if (abs(LoadCell.getData()) <= 4) { //no trash
break;
}
if (abs(LoadCell.getData()) > 4) { //may trash
if (abs(LoadCell.getData()) > 15) { //plastic
Serial.println(abs(LoadCell.getData()));
RVMState = Plastic;
break;
} else { //paper
Serial.println(weight);
RVMState = Paper;
break;
}
}
case Metal:
Serial.println("MetalState");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please wait");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("a moment...");
lcd.setCursor(0, 2);
lcd.print("Weight (g):");
delay(1000);
lcd.setCursor(0, 3);
lcd.print(abs(LoadCell.getData()));
Serial.println("Metal");
LoadCell.update();
if (cycle == 1) {
data1 = abs(LoadCell.getData());
}
if (cycle == 2) {
data2 = abs(LoadCell.getData());
}
if (cycle == 3) {
data3 = abs(LoadCell.getData());
}
if (cycle == 4) {
data4 = abs(LoadCell.getData());
}
if (cycle == 5) {
data5 = abs(LoadCell.getData());
}
if (cycle == 6) {
data6 = abs(LoadCell.getData());
}
if (cycle == 7) {
data7 = abs(LoadCell.getData());
}
if (cycle == 8) {
data8 = abs(LoadCell.getData());
}
if (cycle == 9) {
data9 = abs(LoadCell.getData());
}
if (cycle == 10) {
data10 = abs(LoadCell.getData());
cycle = 0;
}
cycle = cycle + 1;
myservo1.write(0);
delay(15);
myservo1.write(180);
delay(15);
LoadCell.update();
RVMState = NotFullBin;
break;
case Paper:
Serial.println("PaperState");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please wait");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("a moment...");
LoadCell.update();
lcd.setCursor(0, 2);
lcd.print("Weight (g):");
delay(1000);
lcd.setCursor(0, 3);
lcd.print(abs(LoadCell.getData()));
Serial.println("Paper");
Serial.println("clockwise");
if (cycle == 1) {
data1 = abs(LoadCell.getData());
}
if (cycle == 2) {
data2 = abs(LoadCell.getData());
}
if (cycle == 3) {
data3 = abs(LoadCell.getData());
}
if (cycle == 4) {
data4 = abs(LoadCell.getData());
}
if (cycle == 5) {
data5 = abs(LoadCell.getData());
}
if (cycle == 6) {
data6 = abs(LoadCell.getData());
}
if (cycle == 7) {
data7 = abs(LoadCell.getData());
}
if (cycle == 8) {
data8 = abs(LoadCell.getData());
}
if (cycle == 9) {
data9 = abs(LoadCell.getData());
}
if (cycle == 10) {
data10 = abs(LoadCell.getData());
cycle = 0;
}
cycle = cycle + 1;
myStepper.step(stepsPerRevolution);
delay(2000);
myservo1.write(0);
delay(15);
delay(2000);
myservo1.write(180);
delay(15);
Serial.println("counter clockwise");
myStepper.step(-stepsPerRevolution);
LoadCell.update();
RVMState = NotFullBin;
break;
case Plastic:
Serial.println("PlasticState");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please wait");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("a moment...");
lcd.setCursor(0, 2);
lcd.print("Weight (g):");
delay(1000);
lcd.setCursor(0, 3);
lcd.print(abs(LoadCell.getData()));
Serial.println("Plastic");
Serial.println("counter clockwise");
if (cycle == 1) {
data1 = abs(LoadCell.getData());
}
if (cycle == 2) {
data2 = abs(LoadCell.getData());
}
if (cycle == 3) {
data3 = abs(LoadCell.getData());
}
if (cycle == 4) {
data4 = abs(LoadCell.getData());
}
if (cycle == 5) {
data5 = abs(LoadCell.getData());
}
if (cycle == 6) {
data6 = abs(LoadCell.getData());
}
if (cycle == 7) {
data7 = abs(LoadCell.getData());
}
if (cycle == 8) {
data8 = abs(LoadCell.getData());
}
if (cycle == 9) {
data9 = abs(LoadCell.getData());
}
if (cycle == 10) {
data10 = abs(LoadCell.getData());
cycle = 0;
}
cycle = cycle + 1;
myStepper.step(-stepsPerRevolution);
delay(1000);
myservo1.write(0);
delay(15);
delay(2000);
myservo1.write(180);
delay(15);
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
LoadCell.update();
RVMState = NotFullBin;
break;
case CoinCount: //adding all weight divided by 15
Serial.println("CountingCoin");
coin = data1 + data2 + data3 + data4 + data5 + data6 + data7 + data8 + data9 + data10;
coinCount = coin / 15;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please wait");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("a moment...");
lcd.setCursor(0, 2);
lcd.print("Coins:");
lcd.setCursor(0, 3);
lcd.print(coinCount);
Serial.println("coin:");
Serial.println(coinCount);
RVMState = CoinRelease;
break;
case CoinRelease: //1 count 1 centavo
Serial.println("ReleasingCoin");
howmanycoin = coinCount;
if (coinCount > 0) {
for (pos = 0; pos <= 180; pos += 1) {
myservo2.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo2.write(pos);
delay(15);
}
coinCount = coinCount - 1;
Serial.println("Coin: ");
Serial.println(coinCount);
delay(2000);
break;
} else if (coinCount == 0) {
for (pos = 0; pos <= 0; pos += 1) {
myservo2.write(pos);
delay(1000);
count = 0;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Coins obtained");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("(in 25-centavos):");
delay(1000);
lcd.setCursor(0, 2);
lcd.print(howmanycoin);
delay(1000);
lcd.setCursor(0, 3);
lcd.print("Thank you!");
delay(2000);
RVMState = Start;
break;
}
default:
Serial.println("Process Done");
delay(2000);
RVMState = Start;
break;
}
}
}
ps.
I'm using the absolute value of the load cell data as I am getting negative values instead of positive values, not a good alternative but it works fine as no weight is getting left behind.
Disclaimer:
I'm new in coding and was forced to do it for the project, any help would be great. Thanks!