/*
Credits:
OLED library for Oled
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
HX711 library for load cell amplifier
Bogde HX711 lib (https://github.com/bogde/HX711)
Button library
Mathertel OneButtonlib (https://github.com/mathertel/OneButton)
*/
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <OneButton.h>
#include "HX711.h"
HX711 scale1;
HX711 scale2;
#define DOUT1 A0 //Pin 2 and 3 to connect HX711 load cell amplifier
#define CLK1 A1 //
#define DOUT2 A2 //Pin 2 and 3 to connect HX711 load cell amplifier
#define CLK2 A3 //
OneButton buttonTare(4, true); //Digital pin 3 for Tare button
OneButton buttonWeight(5, true); //Digital pin 4 for Weight button
OneButton buttonSpine(6, true); //Digital pin 5 for Spine button
OneButton buttonFoc(7, true); //Digital pin 5 for FOC button
float calibration_factor_1 = 420; //Value calculated from calibration of digital scale. Standard values in grams
float calibration_factor_2 = 420; //Value calculated from calibration of digital scale. Standard values in grams
float baseWeight = 0.0; //To store shaft weight
float spineWeight = 0.0; //To store weight for spine measurement
float foc = 0.0; //To store FOC
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); //Initialize the proper oled. See library sample for the correct one.
void setup(void) {
u8g2.begin(); //Initialize display
scale1.begin(DOUT1, CLK1); //Initialize scale1
scale2.begin(DOUT2, CLK2); //Initialize scale1
scale1.set_scale(calibration_factor_1); //Adjust to the calibration factor (here 456)
scale2.set_scale(calibration_factor_2); //Adjust to the calibration factor (here 456)
buttonTare.attachClick(click1); //-----------------------------------------------
buttonWeight.attachClick(click2); //..buttons actions
buttonSpine.attachClick(click3); //-----------------------------------------------
buttonFoc.attachClick(click4);
u8g2.clearBuffer(); //Clear the internal buffer
u8g2.setFont(u8g2_font_t0_16_tr); //Choose a font before position cursor and print
u8g2.setCursor(15, 16); //Set cursor position
u8g2.print("Arcieri di"); //Print But3 to indicate to click the third button
u8g2.setCursor(33, 32); //Change cursor position
u8g2.print("Ascoli");
u8g2.sendBuffer(); //Transfer internal memory to the display
}
void loop(void) {
buttonTare.tick(); //-----------------------------------------------
buttonWeight.tick(); //Read button state
buttonSpine.tick();
buttonFoc.tick(); //-----------------------------------------------
}
void click1() { //Event simple click for TARE before spine (button 3)
scale1.tare(); //Tare scale 1
scale2.tare(); //Tare scale 2
u8g2.clearBuffer(); //Clear the internal buffer
u8g2.setFont(u8g2_font_logisoso28_tr); //Choose a font before position cursor and print
u8g2.setCursor(15, 32); //Set cursor position
u8g2.print("READY"); //Print 0.00 with two decimals
u8g2.setFont(u8g2_font_t0_16_tr); //Change font
u8g2.sendBuffer(); //Transfer internal memory to the display
}
void click2() { //Event click for Weight
baseWeight=(scale1.get_units(10) + scale2.get_units(10))*15432,4; //Average of ten readings
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_logisoso28_tr);
u8g2.setCursor(5, 32);
u8g2.print(baseWeight, 0); //Print shaft weight with two decimals
u8g2.setFont(u8g2_font_t0_16_tr);
u8g2.setCursor(85, 24);
u8g2.print("Grani"); //Print resume of the shaft weight
u8g2.sendBuffer();
}
void click3() { //Event click for Spine
float localWeight=0.0; //Variable to store the weight for spine calc
localWeight=scale1.get_units(10) + scale2.get_units(10);
spineWeight=((localWeight-0.521980143)*60/1.565940428)+20; //Formula to calculate the spine. It depends from maximum bending
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_logisoso28_tr);
u8g2.setCursor(5, 32);
u8g2.print(spineWeight, 0); //Print shaft spine with two decimals
u8g2.print(" #");
u8g2.setFont(u8g2_font_t0_16_tr);
u8g2.setCursor(85, 16);
u8g2.print(baseWeight, 0); //Print resume of the shaft weight
u8g2.setCursor(85, 32);
u8g2.print("Grani"); //Print resume of the shaft spine
u8g2.sendBuffer();
}
void click4() { //Event click for FOC
foc = (scale2.get_units(10) - scale1.get_units(10))/2*100;
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_logisoso28_tr);
u8g2.setCursor(5, 32);
u8g2.print(foc, 0.0); //Print shaft spine with two decimals
u8g2.print("%");
u8g2.setFont(u8g2_font_t0_16_tr);
u8g2.setCursor(85, 16);
u8g2.print(baseWeight, 0); //Print resume of the shaft weight
u8g2.print("gr");
u8g2.setFont(u8g2_font_t0_16_tr);
u8g2.setCursor(85, 32);
u8g2.print(spineWeight, 0);
u8g2.print(" #");
u8g2.sendBuffer(); //Print resume of the shaft spine
}