Created
February 12, 2019 10:28
-
-
Save AungWinnHtut/82041201fd3fa54d46ec9701c64d3890 to your computer and use it in GitHub Desktop.
HX711.ino scale for ko yar zar min
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); | |
#include "HX711.h" | |
#define calibration_factor -669.10 | |
#define DOUT 3 | |
#define CLK 2 | |
HX711 scale(DOUT, CLK); | |
char * floatToString(char * outstr, float value, int places); | |
void setup() { | |
lcd.begin(16, 2); | |
Serial.begin(9600); | |
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch | |
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0 | |
Serial.println("Readings:"); | |
lcd.write("Readings:"); | |
} | |
void loop() { | |
float data; | |
char buffer[25]; | |
Serial.print("Reading: "); | |
lcd.setCursor(0, 0); | |
lcd.write("Readings:"); | |
Serial.print(scale.get_units(), 2); //scale.get_units() returns a float | |
lcd.setCursor(0, 1); | |
data = scale.get_units(); | |
lcd.write(floatToString(buffer, data, 2)); | |
Serial.print(" g"); //You can change this to kg but you'll need to refactor the calibration_factor | |
lcd.write(" g"); | |
Serial.println(); | |
} | |
char * floatToString(char * outstr, float value, int places) { | |
// this is used to write a float value to string, outstr. oustr is also the return value. | |
int minwidth = 0; | |
bool rightjustify = false; | |
int digit; | |
float tens = 0.1; | |
int tenscount = 0; | |
int i; | |
float tempfloat = value; | |
int c = 0; | |
int charcount = 1; | |
int extra = 0; | |
// make sure we round properly. this could use pow from <math.h>, but doesn't seem worth the import | |
// if this rounding step isn't here, the value 54.321 prints as 54.3209 | |
// calculate rounding term d: 0.5/pow(10,places) | |
float d = 0.5; | |
if (value < 0) | |
d *= -1.0; | |
// divide by ten for each decimal place | |
for (i = 0; i < places; i++) | |
d /= 10.0; | |
// this small addition, combined with truncation will round our values properly | |
tempfloat += d; | |
// first get value tens to be the large power of ten less than value | |
if (value < 0) | |
tempfloat *= -1.0; | |
while ((tens * 10.0) <= tempfloat) { | |
tens *= 10.0; | |
tenscount += 1; | |
} | |
if (tenscount > 0) | |
charcount += tenscount; | |
else | |
charcount += 1; | |
if (value < 0) | |
charcount += 1; | |
charcount += 1 + places; | |
minwidth += 1; // both count the null final character | |
if (minwidth > charcount) { | |
extra = minwidth - charcount; | |
charcount = minwidth; | |
} | |
if (extra > 0 and rightjustify) { | |
for (int i = 0; i < extra; i++) { | |
outstr[c++] = ' '; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment