Hi,
I need to make for a school project a scale to weigh ingredients for people with disabilities. Instead of a display with the corresponding number to the mass of an object, I work with 3 leds and a adjustable weight setting with a rotary knob and a display. The worker who helps the people that are disabled can set the desired weight, and then the disabled person can add or substract the amount according to 3 leds. This is needed because they can’t read or understand value…
I got all the hardware right (load cell, hx711 amp, i2c lcd, arduino nano,…), except there is a problem in the code with the 3 conditions for the leds to light up… I’m working with the if … else statement,
if the value measured by the scale is lower than the one on the display the green led should light up,
if the amount is equal the blue one should light up,
if the amount is greater the red one should light up.
I’ll share my code wich is an adaption of the example in the hx711 library:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711_ADC.h>
HX711_ADC LoadCell(3, 2); // parameters: dt pin, sck pin
int pMin = 0; //the lowest value that comes out of the potentiometer
int pMax = 1023; //the highest value that comes out of the potentiometer.
int x = 0; //we will use this value to store the readings of the potentiometer
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
Serial.begin(9600);//Serial monitor can be used to check the values
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
LoadCell.begin(); // start connection to HX711
LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
LoadCell.setCalFactor(481.0); // calibration factor for load cell => strongly dependent on your individual setup
lcd.begin(16, 2);
lcd.backlight(); // turns on the backlight
}
void loop() {
LoadCell.update(); // retrieves data from the load cell
int i = LoadCell.getData(); // get output value
x = analogRead(A0); //connect the potentiometer to the A0 pin of the Arduino
Serial.print(x); //prints the original reading
Serial.print("\t");
Serial.println(i);
x = map(x, pMin, pMax, 0, 1000); //take the value of x, compared it to the scale of the potentiometer pMin to pMax, and translate that value to the scale of 0 to 100
x = constrain(x, 0, 1000);
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Set weight[g]:"); // print out to LCD
lcd.setCursor(0, 1); // set cursor to secon row
lcd.print(x); // print out the retrieved value to the second row
if (i < x)
{
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}
else if (i == x)
{
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
else
{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);