ขอความช่วยเหลือ โค๊ดนับจำนวน

พอดีว่าผมนำโค๊ดมาใช้แล้วตอนทดลองแล้วมันคลาดเคลื่อนไม่แม่นยำแล้วก็เวลาวัตถุผ่านช้าๆมันจะนับเกิน
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F
#define BACKLIGHT_PIN 3
int sensor4 = 4; // ประกาศตัวแปลเป็น Sensor
int sensor3 = 3; // ประกาศตัวแปลเป็น Sensor
int sensor2 = 2; // ประกาศตัวแปลเป็น Sensor


int a = 0 ; //ประกาศค่าตัวแปรเซ็นเซอร์เป็น 0
int b = 0 ; //ประกาศค่าตัวแปรเซ็นเซอร์เป็น 0
int c = 0 ; //ประกาศค่าตัวแปรเซ็นเซอร์เป็น 0

int i = 0 ; //ประกาศค่าตัวแปรจำนวนที่จะนับเป็น 0
int j = 0 ; //ประกาศค่าตัวแปรจำนวนที่จะนับเป็น 0
int k = 0 ; //ประกาศค่าตัวแปรจำนวนที่จะนับเป็น 0

boolean takeLowTime;
boolean lockLow = true;
long unsigned int lowIn;   
long unsigned int pause = 1000;



LiquidCrystal_I2C lcd(I2C_ADDR,2,1,0,4,5,6,7);

void setup()
{
  lcd.begin (20,4); // ขนาดหน้าจอ LCD
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH); // แสงของหน้าจอ LCD ติด

  pinMode(sensor4,INPUT); //ประกาศสัญญาณเซ็นเซอร์เป็นinput
  pinMode(sensor3,INPUT); //ประกาศสัญญาณเซ็นเซอร์เป็นinput
  pinMode(sensor2,INPUT); //ประกาศสัญญาณเซ็นเซอร์เป็นinput
  Serial.begin(9600);

    lcd.setCursor(4,0); // ไปที่ตัวอักษรที่ 4 แถวที่ 0
    lcd.print("Lime Counter"); // แสดงผลคำว่า Lime Counter
    lcd.setCursor(16,1); // ไปที่ตัวอักษรที่ 16 แถวที่ 1
    lcd.print("Unit"); // แสดงผลคำว่า Unit
    lcd.setCursor(16,2); // ไปที่ตัวอักษรที่ 16 แถวที่ 2
    lcd.print("Unit"); // แสดงผลคำว่า Unit
    lcd.setCursor(16,3); // ไปที่ตัวอักษรที่ 16 แถวที่ 3
    lcd.print("Unit"); // แสดงผลคำว่า Unit
}

void loop()
{
  {a = digitalRead(sensor4); // อ่านค่าจากขา Digital(4)
  b = digitalRead(sensor3); // อ่านค่าจากขา Digital(3)
  c = digitalRead(sensor2); // อ่านค่าจากขา Digital(2)
  lcd.setCursor(0,1); // ไปที่ตัวอักษรที่ 0 แถวที่ 1
  lcd.print("Size S = "); // แสดงผลคำว่า Size S
  lcd.setCursor(0,2); // ไปที่ตัวอักษรที่ 0 แถวที่ 2
  lcd.print("Size M = "); // แสดงผลคำว่า Size M
  lcd.setCursor(0,3); // ไปที่ตัวอักษรที่ 0 แถวที่ 3
  lcd.print("Size L = "); // แสดงผลคำว่า Size L

  
  lcd.setCursor(11,1); // ไปที่ตัวอักษรที่ 11 แถวที่ 1
  lcd.print(i); // แสดงค่าของขา Digital ออกหน้าจอ
  Serial.println(i);
  delay(0.0001);

  lcd.setCursor(11,2); // ไปที่ตัวอักษรที่ 11 แถวที่ 2
  lcd.print(j); // แสดงค่าของขา Digital ออกหน้าจอ
  Serial.println(j);
  delay(0.0001);

    lcd.setCursor(11,3); // ไปที่ตัวอักษรที่ 11 แถวที่ 3
  lcd.print(k); // แสดงค่าของขา Digital ออกหน้าจอ
  Serial.println(k);
  delay(0.0001);
  
       /*if(a == LOW){
      
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
      
         
        i++;
       }
        if(a == HIGH){      
      
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
      
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;  
                                
       }*/

  
     if(a==LOW) //ถ้าเซ็นเซอร์ตรวจจับได้ให้บวกค่าเพิ่มทีละ 1
      i++; delay(50);
      if(b==LOW)
      j++;delay(50);
      if(c==LOW)
      k++;delay(50);

      a=HIGH;
      b=HIGH;
      c=HIGH;
      delay(0.001);
      
      
   }
   
   
    }
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่