Detta är min första version av kod till vågen.
Den använder ett exempel från HX711 biblioteket och har sedan lagt till display biblioteket LedControll och funktionerna.
Det är denna kod som visas i filmen.
Den är skapad i PlatformIO, men bör funka i Arduino IDE också.
/**
*
* Dependency Graph
* |-- HX711 @ 0.7.5
* |-- LedControl @ 1.0.6
*
**/
#include <Arduino.h>
#include "HX711.h"
#include "LedControl.h"
void DisplayNumber(unsigned long);
void DisplayFloat(float);
void DisplayString(String);
char getChar(String, int);
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
unsigned long c = 0;
LedControl lc=LedControl(12,11,10,1); //Ettan betyder att vi har en Display i kaskaden.
HX711 scale;
void setup() {
Serial.begin(9600);
//Initsiera LED-Displayen
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
DisplayString((String) "Pch Pch.");
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
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(468.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() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
DisplayFloat(scale.get_units(10));
scale.power_down(); // put the ADC in sleep mode
delay(1000);
scale.power_up();
}
//Skriver ut ett tal.
void DisplayNumber(unsigned long number){
if(number > 99999999){
DisplayString("E9"); //Betyder Error 9, talet är för högt
return;
}
String s = String(number);
DisplayString(s);
}
void DisplayFloat(float number){
if(number > 99999){
DisplayString("E9"); //Betyder Error 9, talet är för högt
return;
}
String s = (String)number;
int l = s.length();
for (int i = 0; i <= 7; i++) {
lc.setChar(0, i, getChar(s, l-1-i), false);
}
}
//Skriver ut en sträng.
void DisplayString(String s){
int l = s.length();
if(l > 8){
s = "E8"; //Betyder Error 8, strängen är för lång
l = s.length();
}
lc.setChar(0, 7, getChar(s, l-8), false);
lc.setChar(0, 6, getChar(s, l-7), false);
lc.setChar(0, 5, getChar(s, l-6), false);
lc.setChar(0, 4, getChar(s, l-5), false);
lc.setChar(0, 3, getChar(s, l-4), false);
lc.setChar(0, 2, getChar(s, l-3), false);
lc.setChar(0, 1, getChar(s, l-2), false);
lc.setChar(0, 0, getChar(s, l-1), false);
}
//Denna returnerar vilket tecken som är på platsen i strängen.
//Är platsen utanför strängen returneras ett blanksteg.
char getChar(String s, int i){
char rc = ' ';
if(s.length() > (unsigned int) i ){
rc = s.charAt(i);
}
return rc;
}
Inköpslista:
2906 Delar till vågat experiment. Digitalvåg
2855 Viktsensor Load cell 5 Kg
2122 HX711, 24 bitars ADC, passar till viktsensorer
2889 Experiment kort Nano 3.0 med genomomgående hål
2881 USB-C.ARDUINO NANO KOMPATIBEL
Eller liknande.
Tillbaka till https://pchbutik.se/