项目进度(七) - 完善秤

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_27587443/article/details/72794541

本周的项目进度主要是由我负责,所以项目进度和个人学习部分差不多。


之前秤的各个模块的确是连接起来了,但是还没法实现出一个秤应有的正常的功能。

所以在Arduino IDE进行代码的编写,如下:



/* sample for digital weight scale of hx711, display with a HD44780 liquid crtstal monitor
 *
 * hardware design: syyyd
 * available at http://syyyd.taobao.com
 *
 * library design: Weihong Guan (@aguegu)
 * http://aguegu.net
 *
 * library host on
 * https://github.com/aguegu/Arduino
 */
 
//LCD YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1

// Hx711.DOUT - pin #A1
// Hx711.SCK - pin #A0

#include <Hx711.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

Hx711 scale(10, 9);
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  Serial.begin(9600);
  Serial.setTimeout(1000);
  lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("This is an");
  lcd.setCursor(2,1);
  lcd.print("auto scale!");
}

void loop() {
  double weight = scale.getGram();
  double prize = 10;
  double offset = 19.1;
  weight = weight;
  char kind[20] = "Apple!";
  int data = 0;


  while (Serial.available()>0)
  {
    data = Serial.read();
    Serial.println(data);
    prize = data - '0';
    delay(2);
  }
  
  lcd.setCursor(0,0);
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");
  
  Serial.print(weight, 1);
  Serial.println(" g");
   
  lcd.setCursor(0,0);
  lcd.print("Kind: ");
  lcd.setCursor(6,0);
  lcd.print(kind);
  
  lcd.setCursor(0,1);
  lcd.print("W:");
  lcd.setCursor(2,1);
  lcd.print(weight, 1);
  
  lcd.setCursor(9,1);
  lcd.print("P:");
  lcd.setCursor(11,1);
  lcd.print(prize, 1);

  delay(200);
}



这段代码的功能是 每当有东西上秤时,都会显示它的重量,当串口有数据时,它都会进行接收,把接收到的数值通过P变量展示出来。

这样我们以后的工作就是在服务端进行图像的获取与处理,分析出果蔬种类,然后通过串口传输,传输种类,Arduino再根据这个种类,显示价格。


最后的效果图如下:

猜你喜欢

转载自blog.csdn.net/github_27587443/article/details/72794541