Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HX711_ADC.h>
- #include <Wire.h>
- #include "Arduino.h"
- #include <LiquidCrystal_I2C.h>
- #include <SoftwareSerial.h>
- const byte rxPin = 9;
- const byte txPin = 8;
- int taree = 7;
- SoftwareSerial MyBlue(rxPin, txPin); //RX TX
- 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>
- 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)
- void setup() {
- LoadCell.begin(); // start connection to HX711
- LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
- LoadCell.setCalFactor(1715); // calibration factor for load cell => strongly dependent on your individual setup//1715
- lcd.begin(16, 2); // begins connection to the LCD module
- lcd.backlight(); // turns on the backlight
- lcd.setCursor(1, 0); // set cursor to first row
- lcd.print("Kitchen Scale "); // print out to LCD
- lcd.setCursor(0, 1); // set cursor to first row
- lcd.print(" 1KG MAX LOAD "); // print out to LCD
- pinMode(rxPin, INPUT);
- pinMode(txPin, OUTPUT);
- MyBlue.begin(9600);
- Serial.begin(9600);
- delay(2000);
- lcd.clear();
- }
- //int greutate = 0;
- String messageBuffer ="";
- String message= "";
- int sum = 999;
- int cnt = 1;
- int buzzer = 6;
- int maxWeight;
- //init local variables that are used in the code
- void loop()//ifnite loop sine the messages are transmited constantly
- {
- LoadCell.update(); // retrieves data from the load cell
- float i = LoadCell.getData();// get output value
- 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
- {
- char data = MyBlue.read();
- if(sum=999){
- sum=0;
- }
- if(data!= ';') // the messaged sent on bluethoot has to end with an ; in order to see where it is finished
- {
- messageBuffer += data;
- cnt = data;
- sum = sum*10 + (cnt - 48);//-'0'
- Serial.print("sum=");
- Serial.print(sum);// convert char into int decrementing them with ascii value for 0
- }
- else
- {
- message = messageBuffer;
- 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 >=
- //than the value set from bluetooth temrinal, on the lcd shall apear a message and a sound from the buzzer
- // 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 ";"
- //
- cnt = 0;
- maxWeight = atoi(message);
- }
- }
- 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
- {
- Serial.print("sum=");
- Serial.print(sum);
- Serial.print("\n");
- Serial.print("i=");
- Serial.print((int)i);
- Serial.print("\n");
- Serial.print("12321312");
- Serial.print("\n");
- lcd.setCursor(0, 1);
- lcd.print("MAX");
- tone(buzzer, 450);
- delay(300);
- sum=999;
- }
- lcd.setCursor(1, 0); // set cursor to first row
- lcd.print("Digital Scale "); // print out to LCD
- LoadCell.update(); // retrieves data from the load cell
- if (i<0.5)//set margin of error
- {
- if(i>-0.5){
- i=0;
- // i = i * (-1); //set margin of error
- lcd.setCursor(0, 1);
- lcd.print("");
- lcd.setCursor(8, 1);
- lcd.print("");
- }
- else
- {
- i = i * (-1); //display the actual value added to the tare one
- lcd.setCursor(0, 1);
- lcd.print("-");
- lcd.setCursor(8, 1);
- lcd.print("-");
- }
- }
- else
- {
- lcd.setCursor(0, 1); //display the values in gr and oz
- lcd.print(" ");
- lcd.setCursor(8, 1);
- lcd.print(" ");
- }
- lcd.setCursor(1, 1); // set cursor to secon row
- lcd.print(i, 1); // print out the retrieved value to the second row
- lcd.print("g ");
- float z = i/28.3495; // convert gr to oz
- lcd.setCursor(9, 1);
- lcd.print(z, 2);
- lcd.print("oz ");
- if (i>=5000)
- {
- i=0;
- lcd.setCursor(0, 0); // set cursor to secon row
- lcd.print(" Over Loaded ");
- delay(200);
- }
- if (digitalRead (taree) == LOW) //set tare
- {
- lcd.setCursor(0, 1); // set cursor to secon row
- lcd.print(" Taring... ");
- LoadCell.start(1000);
- lcd.setCursor(0, 1);
- lcd.print(" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment