顆粒狀或粉末狀物質任意比例調配機器Arduino程序代碼
程序代碼如下:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
extern unsigned long HX711_Read(void);
extern long Get_Weight();
float WEIGHT = 0;
int HX711_SCK =2;
int HX711_DT= 4;
long HX711_Buffer = 0;
long Weight_Maopi = 0, Weight_Shiwu = 0;
#define GapValue 405
LiquidCrystal_I2C mylcd(0x27,16,2);
String item;
volatile int SF;
volatile int CS;
String W;
volatile float Weight;
volatile float SFW;
volatile float CSW;
volatile int DMODE;
Servo servo_A2;
Servo servo_A3;
const byte KEYPAD_4_4_ROWS = 4;
const byte KEYPAD_4_4_COLS = 4;
char KEYPAD_4_4_hexaKeys[KEYPAD_4_4_ROWS][KEYPAD_4_4_COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte KEYPAD_4_4_rowPins[KEYPAD_4_4_ROWS] = {13, 12, 11, 10};
byte KEYPAD_4_4_colPins[KEYPAD_4_4_COLS] = {9, 8, 7, 6};
Keypad KEYPAD_4_4 = Keypad(makeKeymap(KEYPAD_4_4_hexaKeys), KEYPAD_4_4_rowPins, KEYPAD_4_4_colPins, KEYPAD_4_4_ROWS, KEYPAD_4_4_COLS);
void Delete() {
W = String(W).substring(0,(String(W).length() - 1));
if (W == "") {
W = "0";
Weight = String(W).toInt();
}
Weight = String(W).toInt();
}
void Pour() {
if (!digitalRead(1)) {
if(Weight == 0 || CS + SF < 100 || Weight >= 700)
{
return;
}
mylcd.clear();
WEIGHT = Get_Weight();
mylcd.setCursor(10-1, 2-1);
mylcd.print("/ "+String(round(Weight)));
while (float(WEIGHT) <= SFW+1) {
WEIGHT = Get_Weight();
mylcd.setCursor(3-1, 1-1);
mylcd.print("In Progress");
mylcd.setCursor(4-1, 2-1);
mylcd.print(String(round(WEIGHT)));
servo_A2.write(55);
servo_A3.write(118);
}
servo_A2.write(80);
mylcd.clear();
WEIGHT = Get_Weight();
mylcd.setCursor(10-1, 2-1);
mylcd.print("/ "+String(round(Weight)));
delay(50);
servo_A3.write(93);
while (float(WEIGHT) > SFW+1 && float(WEIGHT) < Weight+2) {
WEIGHT = Get_Weight();
mylcd.setCursor(3-1, 1-1);
mylcd.print("In Progress");
mylcd.setCursor(4-1, 2-1);
mylcd.print(String(round(WEIGHT)));
servo_A2.write(80);
servo_A3.write(93);
}
if (float(WEIGHT) >= Weight+2) {
servo_A2.write(80);
servo_A3.write(118);
}
SF = 0;
CS = 0;
W = "0";
Weight = 0;
mylcd.clear();
mylcd.setCursor(4-1, 1-1);
mylcd.print("Finished!");
delay(2000);
}
}
void Reset() {
W = "0";
Weight = String(W).toInt();
}
void Dmode() {
if (!digitalRead(5)) {
if (SF + CS < 100) {
SF = SF + 2;
} else if (SF < 100 && CS > 0) {
SF = SF + 2;
CS = CS - 2;
}
}
if (!digitalRead(3)) {
if (SF + CS < 100) {
CS = CS + 2;
} else if (CS < 100 && SF > 0) {
CS = CS + 2;
SF = SF - 2;
}
}
}
long Get_Weight()
{
HX711_Buffer = HX711_Read();
Weight_Shiwu = HX711_Buffer;
Weight_Shiwu = Weight_Shiwu - Weight_Maopi;
Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue);
return Weight_Shiwu;
}
unsigned long HX711_Read(void)
{
unsigned long count;
unsigned char i;
digitalWrite(HX711_DT, HIGH);
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
count=0;
while(digitalRead(HX711_DT));
for(i=0;i<24;i++)
{
digitalWrite(HX711_SCK, HIGH);
delayMicroseconds(1);
count=count<<1;
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
if(digitalRead(HX711_DT))
count++;
}
digitalWrite(HX711_SCK, HIGH);
count ^= 0x800000;
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
return(count);
}
void setup(){
servo_A2.write(80);
delay(5);
servo_A3.write(118);
delay(5);
servo_A2.attach(A2);
servo_A3.attach(A3);
pinMode(HX711_SCK, OUTPUT);
pinMode(HX711_DT, INPUT);
Serial.begin(9600);
mylcd.init();
mylcd.backlight();
mylcd.setCursor(0, 0);
mylcd.print("Welcome to use !");
delay(2000);
mylcd.clear();
Weight_Maopi = HX711_Read();
item = "";
SF = 0;
CS = 0;
W = "";
Weight = 0;
SFW = 0;
CSW = 0;
DMODE = false;
pinMode(1, INPUT);
pinMode(3, INPUT);
pinMode(5, INPUT);
}
void loop(){
WEIGHT = Get_Weight();
Serial.print(round(WEIGHT));
Serial.print(" g\n");
Serial.print("\n");
Serial.println(SFW);
item = String(KEYPAD_4_4.getKey());
if (DMODE == true) {
Dmode();
}
if (W == "") {
W = "0";
Weight = String(W).toInt();
}
if (item == "A") {
DMODE = false;
SF = 10;
CS = 90;
} else if (item == "B") {
DMODE = false;
SF = 50;
CS = 50;
} else if (item == "C") {
DMODE = false;
SF = 90;
CS = 10;
} else if (item == "D") {
DMODE = true;
} else if (item == "#") {
Delete();
} else if (item == "*") {
Reset();
} else {
if (Weight <= 100) {
W = String(W) + String(item);
Weight = String(W).toInt();
}
}
mylcd.setCursor(0, 0);
mylcd.print(String("WEIGHT: ") + String(String(int(Weight)) + String(" g")));
mylcd.setCursor(0, 1);
mylcd.print(String(String("LF: ") + String(String(SF) + String("%"))) + String(String(String(" RT: ") + String(CS)) + String("%")));
SFW = float((Weight * SF) / 100);
CSW = Weight - SF;
Pour();
delay(100);
mylcd.clear();