Advertisement
Guest User

oka2

a guest
Nov 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "HX711.h"
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. LiquidCrystal_I2C lcd(0x27,16,2);
  5.  
  6. #define DOUT  A3
  7. #define CLK   A2
  8.  
  9. #define DEC_POINT  2
  10. #define STABLE  1
  11.  
  12. float offset=0;
  13. float calibration_factor = 1;
  14. float real_weight = 1.560;//kg
  15.  
  16. HX711 scale;
  17. double data=0;
  18.  
  19. unsigned char state=0;
  20. long  FindZeroFactor();
  21. float get_units_kg();
  22. void  ReadWeight();
  23. void  FindCalibrationFactor();
  24.  
  25. void setup()
  26. {
  27.   scale.begin(DOUT, CLK);
  28.   Serial.begin(115200);
  29.   Serial.println();
  30.   Serial.println("Auto Calibrate Program");
  31.   Serial.println("Send 'a' to Find Zero Factor (Please Remove all weight from scale)");
  32.   Serial.println("Send 'b' to Find Calibration Factor (Please insert know the weight on the scales)");
  33.   Serial.println("Send 'c' Show weight on the scales");
  34.   lcd.init(); // initialize the lcd
  35.   lcd.init();
  36. }
  37. void loop()
  38. {
  39.   if(Serial.available())
  40.   {
  41.     char temp = Serial.read();
  42.     if(temp=='a')
  43.       state=1;
  44.     if(temp=='b')
  45.       state=2;  
  46.     if(temp=='c')
  47.       state=3;
  48.   }
  49.  
  50.   switch(state)
  51.   {
  52.     case 0:
  53.     break;
  54.     case 1:
  55.       FindZeroFactor();
  56.    //   ReadWeight();
  57.       state=0;
  58.     break;
  59.     case 2:
  60.      FindCalibrationFactor();
  61.      state=0;
  62.     break;
  63.     case 3:
  64.       ReadWeight();
  65.       delay(200);
  66.     break;
  67.     case 4:
  68.      
  69.     break;
  70.    
  71.   }
  72.  
  73. }
  74.  
  75. long FindZeroFactor()
  76. {
  77.      Serial.println("Find Zero Factor");
  78.      Serial.println("Please wait .....");
  79.      scale.set_scale();
  80.      scale.tare();
  81.      long zero_factor = scale.read_average(20);
  82.      Serial.print("Zero factor: ");
  83.      Serial.println(zero_factor);
  84.      return(zero_factor);
  85. }
  86.  
  87. void FindCalibrationFactor()
  88. {
  89.   unsigned char flag_stable=0;
  90.   unsigned int decpoint=1;
  91.   for(unsigned char i=0;i<DEC_POINT+1;i++ )
  92.     decpoint = decpoint*10;
  93.   while(1)
  94.   {
  95.       scale.set_scale(calibration_factor); //Adjust to this calibration factor
  96.       Serial.print("Reading: ");
  97.       float read_weight = get_units_kg();
  98.       String data = String(read_weight, DEC_POINT);
  99.       Serial.print(data);
  100.       Serial.print(" kg");
  101.       Serial.print(" calibration_factor: ");
  102.       Serial.print(calibration_factor);
  103.       Serial.println();
  104.       long r_weight      = (real_weight*decpoint);
  105.       long int_read_weight = read_weight*decpoint;
  106.       Serial.print(r_weight);
  107.       Serial.print(" , ");
  108.       Serial.println(int_read_weight);
  109.       long x;
  110.       if(r_weight == int_read_weight)
  111.       {
  112.         flag_stable++;
  113.         if(flag_stable>=STABLE)
  114.         {
  115.           Serial.print("Calibration Factor is = ");
  116.           Serial.println(calibration_factor);
  117.           break;
  118.          }
  119.         }
  120.        if(r_weight > int_read_weight)
  121.           {
  122.             x = r_weight - int_read_weight;
  123.             if(x > 100)
  124.               calibration_factor += 1000;
  125.             else if(x > 10)
  126.               calibration_factor += 10;
  127.             else
  128.               calibration_factor += 1;
  129.             flag_stable=0;
  130.           }
  131.           if(r_weight < int_read_weight)
  132.           {
  133.             x =  int_read_weight-r_weight;
  134.             if(x > 100)
  135.               calibration_factor -= 1000;
  136.             else if(x > 10)
  137.               calibration_factor -= 10;
  138.             else
  139.               calibration_factor -= 1;
  140.             flag_stable=0;
  141.            }  
  142.   }
  143. }
  144.  
  145. float get_units_kg()
  146. {
  147.   return(scale.get_units()*0.453592);
  148. }
  149. void ReadWeight()
  150. {
  151.   scale.set_scale(calibration_factor); //Adjust to this calibration factor
  152.   Serial.print("Reading: ");
  153.   String data = String(get_units_kg()+offset, DEC_POINT);
  154.   Serial.print(data);
  155.   Serial.println(" kg");
  156.   lcd.backlight();
  157. lcd.setCursor(3,0);
  158. lcd.print("Weight(kg)");
  159. lcd.setCursor(2,1);
  160. lcd.print(data);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement