bogde / HX711

An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for Weight Scales.
MIT License
876 stars 535 forks source link

Problem code with Visual studio code (HX711 use ESP32) with function map #219

Open truXik opened 2 years ago

truXik commented 2 years ago

Please help, I am a complete beginner in programming and I have this problem see. code. Error highlighted. I do it in visual studio code. I use MODEMCU ESP-32, HX711 and 1kg senzor.

include

include

const byte hx711_data_pin = 23; const byte hx711_clock_pin = 22;

float y1 = 20.0; // calibrated mass to be added long x1 = 0L; long x0 = 0L; float avg_size = 10.0; // amount of averages for each mass measurement

HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() { Serial.begin(9600); // prepare serial port delay(1000); // allow load cell and hx711 to settle // tare procedure for (int ii=0;ii<int(avg_size);ii++){ delay(10); x0+=hx711.read(); } x0/=long(avg_size); Serial.println("Add Calibrated Mass"); // calibration procedure (mass should be added equal to y1) int ii = 1; while(true){ if (hx711.read()<x0+10000){ } else { ii++; delay(2000); for (int jj=0;jj<int(avg_size);jj++){ x1+=hx711.read(); } x1/=long(avg_size); break; } } Serial.println("Calibration Complete"); }

void loop() { // averaging reading long reading = 0; for (int jj=0;jj<int(avg_size);jj++){ reading+=hx711.read(); } reading/=long(avg_size); // calculating mass based on calibration and linear fit float ratio_1 = (float) (reading-x0); float ratio_2 = (float) (x1-x0); float ratio = ratio_1/ratio_2; float mass = y1*ratio; Serial.print("Raw: "); Serial.print(reading); Serial.print(", "); Serial.println(mass); }

Compiling .pio\build\nodemcu-32s\FrameworkArduino\MD5Builder.cpp.o src\main.cpp:7:7: error: 'float y1' redeclared as different kind of symbol float y1 = 20.0; // calibrated mass to be added ^ In file included from C:\Users\eriko.platformio\packages\framework-arduinoespressif32\cores\esp32/esp32-hal.h:34:0, from C:\Users\eriko.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:35, from src\main.cpp:1: C:\Users\eriko.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib/math.h:475:15: note: previous declaration 'double y1(double)' extern double y1 _PARAMS((double)); ^ src\main.cpp:12:44: error: no matching function for call to 'HX711::HX711(const byte&, const byte&)' HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711 ^ In file included from src\main.cpp:2:0: .pio\libdeps\nodemcu-32s\HX711\src/HX711.h:30:3: note: candidate: HX711::HX711() HX711(); ^ .pio\libdeps\nodemcu-32s\HX711\src/HX711.h:30:3: note: candidate expects 0 arguments, 2 provided .pio\libdeps\nodemcu-32s\HX711\src/HX711.h:19:7: note: candidate: constexpr HX711::HX711(const HX711&) class HX711 ^ .pio\libdeps\nodemcu-32s\HX711\src/HX711.h:19:7: note: candidate expects 1 argument, 2 provided src\main.cpp: In function 'void loop()': src\main.cpp:52:19: error: invalid operands of types 'double(double)' and 'float' to binary 'operator' float mass = y1ratio; ^ *** [.pio\build\nodemcu-32s\src\main.cpp.o] Error 1 ========================================= [FAILED] Took 4.02 seconds =

truXik commented 2 years ago

include

include

const byte hx711_data_pin = 23; const byte hx711_clock_pin = 22;

float y = 69.0; // calibrated mass to be added long x1 = 0L; long x0 = 0L; float avg_size = 10.0; // amount of averages for each mass measurement

Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() { Serial.begin(115200); // prepare serial port delay(1000); // allow load cell and hx711 to settle // tare procedure for (int ii=0;ii<int(avg_size);ii++){ delay(10); x0+=hx711.read(); } x0/=long(avg_size); Serial.println("Add Calibrated Mass"); // calibration procedure (mass should be added equal to y) int ii = 1; while(true){ if (hx711.read()<x0+10000){ } else { ii++; delay(2000); for (int jj=0;jj<int(avg_size);jj++){ x1+=hx711.read(); } x1/=long(avg_size); break; } } Serial.println("Calibration Complete"); }

void loop() { // averaging reading long reading = 0; for (int jj=0;jj<int(avg_size);jj++){ reading+=hx711.read(); } reading/=long(avg_size); // calculating mass based on calibration and linear fit float ratio_1 = (float) (reading-x0); float ratio_2 = (float) (x1-x0); float ratio = ratio_1/ratio_2; float mass = y*ratio; Serial.print("Raw: "); Serial.print(reading); Serial.print(", "); Serial.println(mass); map = map (mass, 0, 1000, 0, 100) ; Serial.println(map); }

src\main.cpp: In function 'void loop()': src\main.cpp:57:40: error: 'map' cannot be used as a function long map = map (mass, 0, 1000, 0, 100) ; ^ *** [.pio\build\nodemcu-32s\src\main.cpp.o] Error 1

Help please, how do i write a map function to convert my weight to length