Hello everyone. I am an Arduino beginner and am doing an assignment about using the HX711 load cell to control a WS2182 led to light up under different weights. Weird thing is, it shows that there is an error and I don't know how to fix this.
One more thing is, I manage to light the led up but idk how to turn it off while in other weight condition.
If anyone would like to help me, I would be grateful.
/*
-------------------------------------------------------------------------------------
HX711_ADC
Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
Olav Kallhovd sept2017
-------------------------------------------------------------------------------------
*/
/*
Settling time (number of samples) and data filtering can be adjusted in the config.h file
For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"
The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine,
see example file "Read_1x_load_cell_interrupt_driven.ino".
This is an example sketch on how to use this library
*/
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
#include <FastLED.h> //led library
#define LED_PIN 7
#define NUM_LEDS 20
//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
unsigned long t = 0;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(57600); delay(10);
Serial.println();
Serial.println("Starting...");
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
float calibrationValue; // calibration value (see example file "Calibration.ino")
calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
//EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
unsigned long stabilizingtime = 2000; // preciscion right after power-up 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
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
Serial.println("Startup is complete");
}
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); //led setup
}
void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) { newDataReady = true; }
// get smoothed value from the dataset:
if (newDataReady) {}
if (millis() > t + serialPrintInterval) {}
float i = LoadCell.getData();
Serial.print("A");
Serial.println(i);
newDataReady = 0;
t = millis();
//control led with scale value:
if (i < 100 && i >98) { //This is the weight condition i need led lights up
for (int e = 0; e <= 19; e++) {
leds[e] = CRGB ( 150, 0, 255);
FastLED.show();
delay(500);
FastLED.clear();
delay(40);
}
}
// receive command from serial terminal, send 't' to initiate tare operation:
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay(); {
}
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}
}
Welcome to the forum
If you have posted your complete sketch then your use of curly brace pairs is seriously messed up. Every opening brace should have a corresponding closing brace
Have you posted your complete sketch ?
Please post the full error message copied using the convenient button in the IDE
I suggest that you use tools /autoformat in the IDE to properly indent your code; this will not solve your problem but will show you that you are missing 4 } and might make it easier to find where you made the mistake. Each { needs to have a matching }.
Bob, i was trying to correct them follow your guidance but the error still exist. I update the code, could you please check it again for me? Thank you!
/*
-------------------------------------------------------------------------------------
HX711_ADC
Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
Olav Kallhovd sept2017
-------------------------------------------------------------------------------------
*/
/*
Settling time (number of samples) and data filtering can be adjusted in the config.h file
For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"
The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine,
see example file "Read_1x_load_cell_interrupt_driven.ino".
This is an example sketch on how to use this library
*/
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
#include <FastLED.h> //led library
#define LED_PIN 7
#define NUM_LEDS 20
//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
unsigned long t = 0;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(57600); delay(10);
Serial.println();
Serial.println("Starting...");
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
float calibrationValue; // calibration value (see example file "Calibration.ino")
calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
unsigned long stabilizingtime = 2000; // preciscion right after power-up 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
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
Serial.println("Startup is complete");
}
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); //led setup
}
void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) {
newDataReady = true;
}
// get smoothed value from the dataset:
if (newDataReady) {}
if (millis() > t + serialPrintInterval) {}
float i = LoadCell.getData();
Serial.print("A");
Serial.println(i);
newDataReady = 0;
t = millis();
//control led with scale value:
if (i = 100) { //This is the weight condition i need led lights up
for (int e = 0; e <= 19; e++) {
leds[e] = CRGB ( 150, 0, 255);
FastLED.show();
delay(40);
FastLED.clear();
}
// receive command from serial terminal, send 't' to initiate tare operation:
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay(); {
}
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}
}
}
Bob, i found the error and corrected it. Another question is, could you please tell me how can i constrain the 'i' with two conditions? Cuz i need the led lights up(last about 3s or 5s) when the load cell read 100 and then off.
Thank you sooooo much rilviana! Now i got anothe question is, could you please tell me how can i constrain the 'i' with two conditions? Cuz i need the led lights up(last about 3s or 5s) when the load cell read 100 and then off.