Browse Source

add tm1637 4digit 7segment display

master
interfisch 9 months ago
parent
commit
264db45641
  1. 1
      platformio.ini
  2. 156
      src/main.cpp

1
platformio.ini

@ -19,3 +19,4 @@ monitor_speed= 115200
lib_deps =
bogde/HX711 @ 0.7.4
Homie@3.0.0
smougenot/TM1637

156
src/main.cpp

@ -8,6 +8,27 @@
//Upload config: platformio run --target uploadfs
#include <TM1637Display.h>
#define TM1637_CLK D5
#define TM1637_DIO D6
TM1637Display display(TM1637_CLK, TM1637_DIO);
const uint8_t SEG_DONE[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
const uint8_t SEG_POINT[] = { SEG_DP};
uint8_t display_data[] = { 0xff, 0xff, 0xff, 0xff };
uint8_t display_blank[] = { 0x00, 0x00, 0x00, 0x00 };
unsigned long last_displayupdate=0;
#define DISPLAYUPDATEINTERVAL 100
#include "HX711.h"
@ -66,6 +87,10 @@ void setup() {
Serial.begin(115200);
display.setBrightness(7, true); //brightness 0 to 7
Homie.disableResetTrigger(); //disable config reset if pin 1 (D3) is low on startup
@ -97,42 +122,6 @@ void setup() {
Serial.println("tared. measuring...");
//after this taring put known weight on scale and get value from scale.get_units(10). then devide this value by the weight and use this number for set_scale(NUMBER)
/*
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
*/
}
void loop() {
@ -147,7 +136,7 @@ void loopHandler() {
if (loopmillis>last_measure+MEASURE_INTERVAL) {
last_measure=loopmillis;
Serial.print("reading=");
//Serial.print("reading=");
weight_current=0;
if (scale.wait_ready_timeout(1000)) { //for non blocking mode
weight_read_pos++;
@ -162,8 +151,8 @@ void loopHandler() {
float weight_filtered=getFilteredWeight();
float spread=getWeightSpread();
Serial.println(weight_current);
Serial.print("spread="); Serial.println(spread,3);
//Serial.println(weight_current);
//Serial.print("spread="); Serial.println(spread,3);
@ -216,6 +205,91 @@ void loopHandler() {
powerOff();
}
if (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL) {
last_displayupdate=loopmillis;
uint8_t displayresolution=3; //how many digits after dot
float numberdisplay=weight_current;
bool _negative=false;
if (numberdisplay<0) {
numberdisplay*=-1;
_negative=true;
Serial.print("negative, ");
}
Serial.print("numberdisplay="); Serial.print(numberdisplay);
if (numberdisplay>999.9 | displayresolution==0) {
Serial.print(", 1k");
display.showNumberDec((int)(numberdisplay+0.5), false); //just diplay number
}else{
Serial.print(", low-");
uint8_t d1=0;
uint8_t d2=0;
uint8_t d3=0;
uint8_t d4=0;
if(numberdisplay<10 && displayresolution>=3) { // 5.241, 0.005 etc.
Serial.print("<10");
int _number=(int)(numberdisplay*1000+0.5); //in 1000th kg rounded
Serial.print(" = "); Serial.print(_number);
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[1] = display.encodeDigit(d3);
display_data[0] = display.encodeDigit(d4); //leftmost digit
display_data[0] |= SEG_DP; //add decimal point after left most digit
}else if(numberdisplay<100 && displayresolution>=2) { //10.24, 99.20
Serial.print("<100");
int _number=(int)(numberdisplay*100+0.5); //in 100th kg rounded
Serial.print(" = "); Serial.print(_number);
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[1] = display.encodeDigit(d3);
display_data[1] |= SEG_DP; //add decimal point after second digit from the left
display_data[0] = display.encodeDigit(d4); //leftmost digit
if (d4==0) { //number smaller than 1000
display_data[0]={0}; //turn off left segment
}
}else if (numberdisplay<1000 && displayresolution>=1) //100.0, 999.9
{
Serial.print("<1000");
int _number=(int)(numberdisplay*10+0.5); //in 10th kg rounded
Serial.print(" = "); Serial.print(_number);
d1=_number%10;
d2=(_number/10)%10;
d3=(_number/100)%10;
d4=(_number/1000)%10;
display_data[3] = display.encodeDigit(d1); //rightmost digit
display_data[2] = display.encodeDigit(d2);
display_data[2] |= SEG_DP; //add decimal point after second digit from the right
display_data[1] = display.encodeDigit(d3);
display_data[0] = display.encodeDigit(d4); //leftmost digit
if (d4==0) { //number smaller than 1000
display_data[0]={0}; //turn off left segment
if (d3==0) { //number smaller than 100
display_data[1]={0}; //turn off 2nd from left segment
}
}
}
if (_negative) { //show negative number by using rightmost dot
display_data[3] |= SEG_DP;
}
/*
*/
Serial.println();
display.setSegments(display_data);
}
}
/*
scale.power_down(); // put the ADC in sleep mode
@ -323,8 +397,8 @@ bool cmdHandler(const HomieRange& range, const String& value) {
}
void powerOff() {
Serial.println("Turning Off");
Serial.flush();
//Serial.println("Turning Off");
//Serial.flush();
delay(100);
digitalWrite(PIN_SELFENABLE, LOW);
}
Loading…
Cancel
Save