Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:eb499e2a1b9b
- Parent:
- 0:765cf978c3e5
- Child:
- 2:61a0169765bf
diff -r 765cf978c3e5 -r eb499e2a1b9b main.cpp --- a/main.cpp Sat Feb 16 02:32:43 2019 +0000 +++ b/main.cpp Sat Feb 16 07:02:56 2019 +0000 @@ -1,7 +1,79 @@ #include "mbed.h" +#include <HX711.h> CAN can1(PD_0, PD_1); CAN can2(PB_5, PB_6); +DigitalOut led1(LED1); +DigitalOut led2(LED2); +//FlashIAP flashIAP; + +/* scale */ +HX711 hx711(PB_11, PB_10); + +struct ScaleCalibrationData { + unsigned int calibrationWeight; // the weight (g) used for calibration for example 1000g or 10g. The maximum value is 3000. + long offsetValue; // the value for scale offset + float scaleValue; // the ADC increment for 1g + unsigned char checksum; +}; +unsigned int calibration_ADC_value; +//#define CALIBRATION_VALUE 10 // 10g as the calibration weight +#define WEIGHT_DIFFERENCE 200 // 10g ADC value minimum difference +#define CALIBRATION_WEIGHT 2000 // calibration weight + +ScaleCalibrationData customVar; + +long zero_value; +long calibration_value; +unsigned int calibration_times; // must calibration 3 times +unsigned int calibration_done = 0; +float scale_value; + +/* scale */ +void scaleCalibration() +{ + printf("Start Calibration.\r\n"); + calibration_done = 0; + if (!calibration_done) + { + led1 = 1; + led2 = 0; + customVar.calibrationWeight = CALIBRATION_WEIGHT; + zero_value = hx711.averageValue(10); // skip first 10 readings + zero_value = hx711.averageValue(20); + printf("zero_value=%d \r\n", zero_value); + calibration_value = 0; + scale_value = 0; + calibration_times = 0; + + while (( calibration_times < 5)) + { + + calibration_value = hx711.averageValue(20); + if (calibration_value > (zero_value + WEIGHT_DIFFERENCE)) + { + calibration_times++; + } + else + calibration_times = 0; + } + printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times); + if (calibration_times >=5) + { + // calibration is OK + led1 = 0; + led2 = 1; + calibration_times = 0; + scale_value = (calibration_value - zero_value) / customVar.calibrationWeight; + customVar.offsetValue = zero_value; + customVar.scaleValue = scale_value; +// EEPROM.put(0x00, customVar); + hx711.setOffset(zero_value); + hx711.setScale(scale_value); // this value is obtained by calibrating the scale with known weights; see the README for details + calibration_done = 1; + } + } +} int a = 0; int b = 0; @@ -14,8 +86,7 @@ Thread thread; -DigitalOut led1(LED1); -DigitalOut led2(LED2); + CANMessage msg; @@ -67,19 +138,23 @@ int main() { printf("\n\n*** RTOS basic example ***\n"); + scaleCalibration(); + thread.start(print_thread); - thread.start(print_thread); +// flashIAP.init(); +// printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size()); // can1.reset(); // can2.reset(); // can1.frequency(100000); // can2.frequency(100000); //button1.mode(PullUp); // Activate pull-up - button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event + button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event int idx = 0; // Just for printf below while(1) { if (button1_pressed) { // Set when button is pressed + printf("scale value %f. \r\n", hx711.getGram()); button1_pressed = false; printf("Button pressed %d\n", idx++); can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));