说明
- 接通电源,机器开始运作。
- 将货物放在传送带上,货物被传送带带到电子秤上进行称量。
- 满足称量标准的货物被放置于一边,不满足称量标准的货物被放置于另一边。
- 显示器上对合格与不合格的货物个数进行统计,并可实时显示货物重量和合格率,还可通过蓝牙设备将数据进行发送,对数据进行采集。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8608b3dad96d53ef8b99daa1a22c8109.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8b6a2359084194b0fa4d817a45321ba5.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f3df9020f916bdbea58c12714deb7847.png)
#include <Servo.h>
#include <LiquidCrystal.h>
#include "HX711.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
Servo servo_9;
Servo servo_10;
int sound=2;
int Relay = 7;
String inputString = "";
String comdata = "";
int Weight = 0,m = 0,n = 0;
float s=0.0;
void setup()
{
mylcd.init();
mylcd.backlight();
servo_9.attach(9);
servo_10.attach(10);
lcd.begin(16, 2);
Init_Hx711();
pinMode(Relay,OUTPUT);
pinMode(sound,OUTPUT);
Serial.begin(9600);
Get_Maopi();
delay(3000);
Get_Maopi();
}
void loop()
{
servo_9.write(80);
delay(15);
servo_10.write(95);
delay(15);
Weight = Get_Weight();
delay(1000);
if(Weight<0)
{
Weight=0;
}
digitalWrite(Relay,HIGH);
if(Weight>5)
{
digitalWrite(Relay,LOW);
delay(1000);
Weight = Get_Weight();
delay(1000);
if(Weight<=50)
{
m+=1;
s=(float)(n)/(float)(m+n);
Serial.print(Weight);
Serial.print(" g");
Serial.print("不合格的为:");
Serial.print(m);
Serial.print("合格的为:");
Serial.print(n);
Serial.print(" 合格率:");
Serial.print(s*100);
Serial.println("%");
Serial.println();
mylcd.setCursor(0, 0);
mylcd.print("m ");
mylcd.print(m);
mylcd.print(" n ");
mylcd.print(n);
mylcd.setCursor(0, 1);
mylcd.print(Weight);
mylcd.print(" g");
mylcd.print(" s ");
mylcd.print(s*100);
mylcd.println("% ");
delay(2000);
servo_10.write(170);
delay(2000);
servo_10.write(95);
delay(2000);
digitalWrite(Relay,HIGH);
}
if(Weight>50)
{
n+=1;
s=(float)(n)/(float)(m+n);
Serial.print(Weight);
Serial.print(" g");
Serial.print(" 不合格的为:");
Serial.print(m);
Serial.print(" 合格的为:");
Serial.print(n);
Serial.print(" 合格率:");
Serial.print(s*100);
Serial.println("%");
Serial.println();
mylcd.setCursor(0, 0);
mylcd.print("m ");
mylcd.print(m);
mylcd.print(" n ");
mylcd.print(n);
mylcd.setCursor(0, 1);
mylcd.print(Weight);
mylcd.print(" g");
mylcd.print(" s ");
mylcd.print(s*100);
mylcd.println("% ");
delay(3000);
servo_9.write(10);
delay(2000);
servo_9.write(80);
delay(2000);
digitalWrite(Relay,HIGH);
}
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if(inChar == 'A' || inChar == 'a' )
{
digitalWrite(Relay,HIGH);
delay(1000);
}
else if(inChar == 'B' || inChar == 'b')
{
digitalWrite(Relay,LOW);
delay(1000);
}
}
}