ผมจะเครื่องฉีดน้ำ ตั้งแต่ 50 ml - 400 ml ต่อครั้ง
ปัญหาคือ เมื่อตั้งค่าที่ 60 ml แล้วปล่อยน้ำ พบว่า LCD แสดงค่าเกินกว่าที่กำหนด และน้ำที่ก็ออกมาเกิน หลังจาก
Relay ตัดไฟ พอจะแก้ได้หรือปล่าวครับ
ใช้ Arduino Uno R3
water flow sensor : YF-S20,1-30 L/min,Pressure<=1.75 Mpa
Relay : บอร์ด Relay 2 ช่อง 5 โวลต์ 10A 250V สำหรับ Arduino และ Microcontroller
โคดที่ใช้
#include <Wire.h>
#include <LiquidCrystal.h>
/*******************************************************
This program will test the LCD panel and the buttons
Mark Bramwell, July 2010
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
byte prev_screen;
byte prev_lcd_key;
byte sensorInterrupt = 0; // 0 = pin 2; 1 = pin 3
byte sensorPin = 2;
byte relayPin = 3;
float calibrationFactor = 4.5;
float flowRate = 0;
volatile byte pulseCount = 0;
unsigned long flowMilliLitres = 0;
unsigned long totalMilliLitresA = 0;
byte screen = 0;
int set_point = 0;
byte edit = 0;
unsigned long oldTime = 0;
byte key_input;
byte statusLed = 13;
int total_litres = 0;
byte display_run = 0;
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.clear();
lcd.setCursor(0, 0);
Serial.begin (9600);
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
pinMode(statusLed, OUTPUT);
pinMode(relayPin, OUTPUT);
lcd_key = read_LCD_buttons(); // read the buttons
if (prev_lcd_key != lcd_key) {
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
key_input = 2;
break;
}
case btnLEFT:
{
key_input = 1;
break;
}
case btnUP:
{
if (edit == 1)
{
set_point++;
}
break;
}
case btnDOWN:
{
if (edit == 1)
{
set_point--;
}
break;
}
case btnSELECT:
{
if (screen == 1) {
screen = 0;
edit = 0;
}
else
{ screen = 1;
edit = 1;
}
break;
}
case btnNONE:
{
key_input=0;
break;
}
}
}
prev_lcd_key = lcd_key;
if (set_point < 60){set_point = 60;
}
if (set_point >400){set_point =400;
}
if (key_input == 1) {
totalMilliLitresA = 0;
total_litres = 0;
display_run = 1;
digitalWrite(statusLed, HIGH);
digitalWrite(relayPin, LOW);
}
if (key_input == 2) {
display_run = 0;
digitalWrite(statusLed, LOW);
digitalWrite(relayPin, HIGH);
}
if (total_litres >= set_point) {
display_run = 0;
digitalWrite(statusLed, LOW);
digitalWrite(relayPin, HIGH);
}
if ((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitresA += flowMilliLitres;
if (display_run == 1) {
total_litres = totalMilliLitresA;
}
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
Serial.print ("display: ");
Serial.print (display_run);
Serial.print (" ml: ");
Serial.print (totalMilliLitresA);
Serial.print (" mL ");
Serial.print (total_litres);
Serial.print (" setpoint ");
Serial.println (set_point);
if (prev_screen != screen) {
lcd.clear();
}
lcd.setCursor(0, 0);
switch (screen) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Flow:");
if (flowRate < 10){
lcd.print(" ");}
lcd.print(flowRate, 2);
lcd.setCursor(11, 0);
lcd.print(total_litres);
lcd.print("mL");
lcd.setCursor(6, 1);
lcd.print(set_point);
lcd.print("mL");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("select to set");
lcd.setCursor(0, 1);
lcd.print(set_point );
lcd.print("mL");
break;
}
prev_screen = screen;
}
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
ตรวงส่วนนี้ต้องลองดีบั๊กกับอุปกรณ์ที่ใช้ ปรับจูนเพิ่มอีกทีนะครับ