其他分享
首页 > 其他分享> > 基于arduino的矿石分拣机

基于arduino的矿石分拣机

作者:互联网

基于arduino uno的矿石分拣机,基于电子秤原理,矿石通过传送带送到电子秤,然后测得重量,通过舵机分拨到不同的筐里

// Hx711.DOUT - pin #A1
// Hx711.SCK - pin #A0
//serve_9 接9
//serve_10接10
//relay继电器接7
//SDA 接 模拟口A4 
//SCL 接 模拟口A5
/*                
 舵机接线:棕红橙:G V S 
       黑红白  G V S
 */
#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);  // 舵机连接9号管脚
    servo_10.attach(10);  // 舵机连接10号管脚
    lcd.begin(16, 2);
	  Init_Hx711();				//初始化HX711模块连接的IO设置
    pinMode(Relay,OUTPUT);//继电器
    pinMode(sound,OUTPUT);
    Serial.begin(9600);
    Get_Maopi();		//获取毛皮
	  delay(3000);
	  Get_Maopi();		//获取毛皮
}

void loop()
{
  //int flag=0;
    servo_9.write(80);
    delay(15);
    servo_10.write(95);
    delay(15);
    Weight = Get_Weight();	//计算放在传感器上的重物重量
  delay(1000);
 
    if(Weight<0)
    {
      Weight=0;
      }
  /*lcd.setCursor(0, 1);
	lcd.print(Weight);	//串口显示重量
	lcd.print(" g");	//显示单位
   lcd.print("       ");*/
     digitalWrite(Relay,HIGH);
      //      delay(1000);
    if(Weight>5)
    { 

     //   flag=1;
         digitalWrite(Relay,LOW);
         delay(1000);
         Weight = Get_Weight();  //计算放在传感器上的重物重量
         delay(1000);
  /*    if(Weight>200)
      {
        digitalWrite(sound,HIGH);
        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);   //延时 
      // mylcd.clear();
     servo_10.write(170);
    delay(2000);
    servo_10.write(95);
    delay(2000);
 /*    Weight = Get_Weight();  //计算放在传感器上的重物重量
         delay(1000);
    if(-5<=Weight&&Weight<=5)
     {
      flag=0;
      }   
        while(flag);*/
    digitalWrite(Relay,HIGH);
    // delay(1000);
      }   
   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);
  /*    Weight = Get_Weight();  //计算放在传感器上的重物重量
         delay(1000);
     if(-5<=Weight&&Weight<=5)
     {
      flag=0;
      }      
      while(flag);*/
     digitalWrite(Relay,HIGH);
    // delay(1000);
          }   
         
      }
 
}
//蓝牙
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    if(inChar == 'A' || inChar == 'a' )
    {
      digitalWrite(Relay,HIGH);
      delay(1000);
    }
    else  if(inChar == 'B' || inChar == 'b')
    {
      digitalWrite(Relay,LOW);
     delay(1000);   //延时
    }
  }
}

标签:10,mylcd,Weight,arduino,分拣机,矿石,delay,print,Serial
来源: https://blog.csdn.net/qq_27885507/article/details/104860905