Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "HX711.h"
- #include <EEPROM.h>
- //#include "nextion.h"
- // HX711 circuit wiring
- const int LOADCELL_DOUT_PIN = A1;
- const int LOADCELL_SCK_PIN = A0;
- const char commandStart= '@';
- const char commandEnd= ';';
- unsigned char commandPtr= 0;
- char command[20];
- unsigned long serTimeout;
- HX711 scale;
- long adTable[ 30];
- unsigned char firstRead= 1;
- char setPoint[20];
- long avgAd;
- long zeroAd;
- float spanAd;
- unsigned char avgCount= 15;
- unsigned char decPoint= 1;
- unsigned char nyugFelt= 10;
- unsigned char lepes= 1;
- void setup() {
- Serial.begin( 9600);
- scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
- EEPROM.get( 0, zeroAd);
- Serial.print( "zeroAd: ");
- Serial.println( zeroAd);
- EEPROM.get( 4, spanAd);
- Serial.print( "spanAd: ");
- Serial.println( spanAd);
- EEPROM.get( 12, avgCount);
- if ((avgCount>99) || (avgCount<3)){
- avgCount= 15;
- EEPROM.put( 12, avgCount);
- }
- Serial.print( "avgCount: ");
- Serial.println( avgCount);
- EEPROM.get( 13, decPoint);
- if (decPoint>5){
- decPoint= 1;
- EEPROM.put( 13, decPoint);
- }
- Serial.print( "decPoint: ");
- Serial.println( decPoint);
- EEPROM.get( 14, nyugFelt);
- if (nyugFelt>99){
- nyugFelt= 10;
- EEPROM.put( 14, nyugFelt);
- }
- Serial.print( "nyugFelt: ");
- Serial.println( nyugFelt);
- EEPROM.get( 15, lepes);
- if (lepes!=1 && lepes!=2 && lepes!=5 && lepes!=10 && lepes!=20 && lepes!=50){
- Serial.print( "lepes: ");
- Serial.println( lepes);
- lepes= 1;
- EEPROM.put( 15, lepes);
- }
- Serial.print( "lepes: ");
- Serial.println( lepes);
- }
- float weight( long avgAd)
- {
- /* Serial.print( "zeroAd: ");
- Serial.print( zeroAd);
- Serial.print( " endAd: ");
- Serial.print( endAd);
- Serial.print( " endValue: ");
- Serial.print( endValue);
- */
- char s[20];
- float x= (float )avgAd;
- float zA= (float )zeroAd;
- // Serial.print( " avgAd: ");
- // dtostrf( x, 10, 10, s);
- // Serial.print( s);
- x-= zA;
- /* Serial.print( " avg-zero: ");
- dtostrf( x, 10, 10, s);
- Serial.print( s);*/
- float sA= (float )spanAd;
- x*= sA;
- /* Serial.print( " *eV:");
- dtostrf( x, 10, 10, s);
- Serial.print( s);
- Serial.print( " weight: ");*/
- float l= (float)lepes;
- unsigned char i;
- for (i=0; i<decPoint; i++) x*=10;
- x= round( x/l)*l;
- for (i=0; i<decPoint; i++) x/=10;
- return x;
- }
- unsigned long last_millis= millis()+ 99;
- void loop() {
- // -- HX711 beolvasás kb 100 ms-onként.
- if (millis()>= last_millis){
- if (scale.wait_ready_retry(10)) {
- long reading = scale.read();
- unsigned char i;
- if (firstRead) {
- firstRead= 0;
- for (i= 0; i<30; i++) adTable[i]= reading;
- }
- for (i=avgCount-1; i>0; i--) {
- adTable[i]= adTable[i-1];
- }
- adTable[0]= reading;
- long avg= 0;
- for (i=0; i< avgCount; i++) {
- avg+= adTable[i];
- // Serial.print( weight( adTable[i]));
- //Serial.print( " ");
- }
- avg/= avgCount;
- avgAd= avg;
- // Serial.println( avgAd);
- // Serial.print("AVG:");
- //Serial.print( avgAd);
- unsigned char nyugalom= 1;
- float nyugF= nyugFelt;
- for (i=0; i< decPoint; i++)
- nyugF/=10;
- float w= weight( avgAd);
- for (i=0; i< avgCount; i++){
- if ( abs( weight(adTable[i])- w)> nyugF){
- nyugalom= 0;
- }
- }
- if (nyugalom) Serial.print( "W");
- else Serial.print( "w");
- char s[20];
- dtostrf( weight( avgAd), 8, decPoint, s);
- Serial.println( s);
- //Serial.print("\x03");
- } else {
- Serial.println("HX711 not found.");
- }
- last_millis= millis()+ 100;
- }
- //-- soros port kezelése
- while (Serial.available() > 0) {
- char serRead= Serial.read();
- switch (commandPtr){
- case 0: if (serRead== commandStart) {
- commandPtr++;
- serTimeout= millis()+ 100;
- Serial.println( "START");
- }
- break;
- case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11:
- // parancsok
- // nullázás ZERO
- // végérték megadás ENDP004000
- // átalagolás száma AVGC30 01..99
- // tizedes pontok DECP2 0..5
- // lepes nagysag LEPS01 01 02 05 10 20 50
- // nyug feltétel NYUG01 01..99 tizedosztás
- if (serRead== commandEnd ) {
- commandPtr= 0;
- serTimeout= 0;
- if (strncmp( command, "ZERO", 4)== 0){
- if (strlen( command)== 4){
- zeroAd= avgAd;
- Serial.print( "ZERO: ");
- Serial.println( avgAd);
- EEPROM.put( 0, zeroAd);
- }else{
- Serial.print( "LENGTH ERROR ");
- Serial.println( command);
- }
- }else
- if (strncmp( command, "ENDP", 4)== 0){
- if (strlen( command)== 10){
- strncpy( setPoint, command+ 4, 6);
- setPoint[ 6]= 0;
- long endValue= strtol( setPoint, NULL, 10);
- long endAd= avgAd- zeroAd;
- spanAd= (float)endValue/ (float) endAd;
- Serial.print( "endAd: ");
- Serial.print( endAd);
- Serial.print( " endValue: ");
- Serial.print( endValue);
- Serial.print( " spanAd: ");
- Serial.println( spanAd);
- EEPROM.put( 4, spanAd);
- }else{
- Serial.print( "LENGTH ERROR ");
- Serial.println( command);
- }
- }else
- if (strncmp( command, "AVGC", 4)== 0){
- if (strlen( command)== 6){
- strncpy( setPoint, command+ 4, 2);
- setPoint[ 2]= 0;
- avgCount= strtol( setPoint, NULL, 10);
- Serial.print( "avgCount: ");
- Serial.println( avgCount);
- EEPROM.put( 12, avgCount);
- }else{
- Serial.print( "LENGTH ERROR ");
- Serial.println( command);
- }
- }else
- if (strncmp( command, "DECP", 4)== 0){
- if (strlen( command)== 5){
- strncpy( setPoint, command+ 4, 1);
- setPoint[ 1]= 0;
- decPoint= strtol( setPoint, NULL, 10);
- Serial.print( "decP: ");
- Serial.println( decPoint);
- EEPROM.put( 13, decPoint);
- }else{
- Serial.print( "LENGTH ERROR ");
- Serial.println( command);
- }
- }else
- if (strncmp( command, "NYUG", 4)== 0){
- if (strlen( command)== 6){
- strncpy( setPoint, command+ 4, 2);
- setPoint[ 2]= 0;
- nyugFelt= strtol( setPoint, NULL, 10);
- Serial.print( "nyugFelt: ");
- Serial.println( nyugFelt);
- EEPROM.put( 14, nyugFelt);
- }else{
- Serial.print( "LENGTH ERROR ");
- Serial.println( command);
- }
- }else
- if (strncmp( command, "LEPS", 4)== 0){
- if (strlen( command)== 6){
- strncpy( setPoint, command+ 4, 2);
- setPoint[ 2]= 0;
- lepes= strtol( setPoint, NULL, 10);
- Serial.print( "lepes: ");
- Serial.println( lepes);
- EEPROM.put( 15, lepes);
- }else{
- Serial.print( "LENGTH ERROR ");
- Serial.println( command);
- }
- }else
- {
- Serial.print( "ERROR: ");
- Serial.println( command);
- }
- }else{
- command[ commandPtr-1]= serRead;
- command[ commandPtr]= 0;
- commandPtr++;
- if (commandPtr> 11) {
- commandPtr= 0;
- serTimeout= 0;
- Serial.println( "ERROR 11");
- }
- }
- break;
- default:
- break;
- }
- }
- if (serTimeout>0 && millis()> serTimeout){
- serTimeout= 0;
- commandPtr= 0;
- Serial.println( "TIMEOUT");
- }
- delay(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement