Advertisement
Aodai

Untitled

Jul 4th, 2022
723
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <HX711_ADC.h>
  2. #include <Wire.h>
  3. #include "Arduino.h"
  4. #include <LiquidCrystal_I2C.h>
  5. #include <SoftwareSerial.h>
  6.  
  7. const byte rxPin = 9;
  8. const byte txPin = 8;
  9. int taree = 7;
  10. SoftwareSerial MyBlue(rxPin, txPin); //RX TX
  11. HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
  12. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
  13.  
  14.  
  15. void setup() {
  16.   LoadCell.begin(); // start connection to HX711
  17.   LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
  18.   LoadCell.setCalFactor(1715); // calibration factor for load cell => strongly dependent on your individual setup//1715
  19.   lcd.begin(16, 2); // begins connection to the LCD module
  20.   lcd.backlight(); // turns on the backlight
  21.   lcd.setCursor(1, 0); // set cursor to first row
  22.   lcd.print("Kitchen Scale "); // print out to LCD
  23.   lcd.setCursor(0, 1); // set cursor to first row
  24.   lcd.print(" 1KG MAX LOAD "); // print out to LCD
  25.   pinMode(rxPin, INPUT);
  26.   pinMode(txPin, OUTPUT);
  27.   MyBlue.begin(9600);
  28.   Serial.begin(9600);
  29.   delay(2000);
  30.   lcd.clear();
  31. }
  32.  
  33. //int greutate = 0;
  34. String messageBuffer ="";
  35. String message= "";
  36. int sum = 999;
  37. int cnt = 1;
  38. int buzzer = 6;
  39. int maxWeight;
  40. //init local variables that are used in the code
  41.  
  42. void loop()//ifnite loop sine the messages are transmited constantly
  43.   {
  44.     LoadCell.update(); // retrieves data from the load cell
  45.    float i = LoadCell.getData();// get output value
  46.   while(MyBlue.available()> 0) //there the message sent via bluetooth is transformed in decimal in order to be compared with the actual value measured on the scale
  47.   {
  48.     char data = MyBlue.read();
  49. if(sum=999){
  50.   sum=0;
  51. }
  52.  
  53.     if(data!= ';') // the messaged sent on bluethoot has to end with an ; in order to see where it is finished
  54.     {
  55.         messageBuffer += data;
  56.         cnt = data;
  57.         sum = sum*10 + (cnt - 48);//-'0'
  58.         Serial.print("sum=");
  59.     Serial.print(sum);// convert char into int decrementing them with ascii value for 0
  60.     }
  61.     else
  62.     {
  63.         message = messageBuffer;
  64.         messageBuffer ="";                        //i implemented this part of code, that has to set the max value of the scale via bluetooth. and when the value on the scale is >=
  65.                                                   //than the value set from bluetooth temrinal, on the lcd shall apear a message and a sound from the buzzer
  66.                                                   // my implementation works only when i hard code the value of sum( the value from bluetooth ). and the message from the bluetooth has to end with ";"
  67.                                                   //
  68.          cnt = 0;
  69.          maxWeight = atoi(message);
  70.        
  71.   }
  72. }
  73. if( (sum < (int)i+5)) //the scale shall display the message error in case the measured value is higher than the one set via bluethoot mesage
  74. {
  75.     Serial.print("sum=");
  76.     Serial.print(sum);
  77.     Serial.print("\n");
  78.     Serial.print("i=");
  79.     Serial.print((int)i);
  80.     Serial.print("\n");
  81.     Serial.print("12321312");
  82.     Serial.print("\n");
  83.     lcd.setCursor(0, 1);
  84.     lcd.print("MAX");
  85.     tone(buzzer, 450);
  86.     delay(300);
  87.     sum=999;
  88.      }
  89.      
  90.   lcd.setCursor(1, 0); // set cursor to first row
  91.   lcd.print("Digital Scale "); // print out to LCD
  92.   LoadCell.update(); // retrieves data from the load cell
  93.  if (i<0.5)//set margin of error
  94.  {
  95.   if(i>-0.5){
  96.     i=0;
  97.      // i = i * (-1); //set margin of error
  98.     lcd.setCursor(0, 1);
  99.     lcd.print("");
  100.     lcd.setCursor(8, 1);
  101.     lcd.print("");
  102.    }
  103.   else
  104.   {  
  105.     i = i * (-1);   //display the actual value added to the tare one
  106.     lcd.setCursor(0, 1);
  107.     lcd.print("-");
  108.     lcd.setCursor(8, 1);
  109.     lcd.print("-");
  110.   }
  111.  }
  112.  else
  113.  {
  114.    lcd.setCursor(0, 1); //display the values in gr and oz
  115.    lcd.print(" ");
  116.    lcd.setCursor(8, 1);
  117.    lcd.print(" ");
  118.  }
  119.  
  120.   lcd.setCursor(1, 1); // set cursor to secon row
  121.   lcd.print(i, 1); // print out the retrieved value to the second row
  122.   lcd.print("g ");
  123.   float z = i/28.3495; // convert gr to oz
  124.   lcd.setCursor(9, 1);
  125.   lcd.print(z, 2);
  126.   lcd.print("oz ");
  127.  
  128.   if (i>=5000)
  129.   {
  130.     i=0;
  131.   lcd.setCursor(0, 0); // set cursor to secon row
  132.   lcd.print("  Over Loaded   ");
  133.   delay(200);
  134.   }
  135.   if (digitalRead (taree) == LOW) //set tare
  136.   {
  137.     lcd.setCursor(0, 1); // set cursor to secon row
  138.     lcd.print("   Taring...    ");
  139.     LoadCell.start(1000);
  140.     lcd.setCursor(0, 1);
  141.     lcd.print("                ");
  142.   }
  143.   }
Advertisement
RAW Paste Data Copied
Advertisement