Cat counter

This counts and displays on an LCD the number of times someone/something (of course it can be used to count anything, not only cats 🙂 ) approaches within 12 cm from the sensor. Also the back light is turned on for 5 seconds after the initial approach.

After the event all approaches are ignored for the next 2 minutes.

If the button is pressed, the LCD backlight is turned on for 5 seconds.

Schematic

 

Real circuit

 

Used

BTW, my cat eats between 10 and 22 times per day and drinks water between 8 and 15 times per day 🙂

The code:

/*
  Copyright © 2018 Razvan Murariu <razwww@gmail.com>
  This program is free software. It comes without any warranty, to
  the extent permitted by applicable law. You can redistribute it
  and/or modify it under the terms of the Do What The Fuck You Want
  To Public License, Version 2, as published by Sam Hocevar. See
  the COPYING file for more details.

  The circuit:
  Arduino Nano
  trigPin: A1
  echoPin: A0
  display SCL: A4
  display SDA: A5
  button (NC): between 5V and D2
  D2 pulled-down to ground with a 10 KΩ resistor
*/

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const long intervalEat = 120000;
const long intervalLight = 5000;
int distance = 12;
long duration;
int distanceCm;
int counter = 0;
int buttonState = 0;
unsigned long previousMillisEat = 0;
unsigned long previousMillisButton = 0;
const int trigPin = A1;
const int echoPin = A0;

void setup() {
  lcd.begin(16,2);
  
  Serial.begin(9600);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  pinMode(13, OUTPUT);
  pinMode(2, INPUT); // button for backlight
  
  lcd.setCursor(0,0);
  lcd.print("0   pisici");
  lcd.backlight();
  delay(intervalLight);
  lcd.noBacklight();
}

void loop() {
  unsigned long currentMillis = millis();

  delay(10);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceCm= duration*0.034/2;
  
  // distance smaller than 12 cm && (just powered on || intervalEat passed)
  if (distanceCm < distance && ((currentMillis < intervalEat &amp;&amp; counter == 0) || (currentMillis - previousMillisEat >= intervalEat))) {
    previousMillisEat = currentMillis;
    counter++;
    Serial.print(distanceCm);
    Serial.println(" cm");
    Serial.print(counter);
    Serial.println(" pisici");
    Serial.println(currentMillis);
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print(counter);
    delay(intervalLight);
    lcd.noBacklight();
  }
  
  buttonState = digitalRead(2);
  if (buttonState == LOW) {
    Serial.println("Buton");    
    lcd.backlight();
    previousMillisButton = currentMillis;
    }
    
  if ( currentMillis - previousMillisButton >= intervalLight ) {
    lcd.noBacklight();
    }

}

The most recent version of the code is found on GitHub


0 Comentarii la “Cat counter”

Abonare la comentarii pentru acest articol
  1. Nu sunt comentarii

Adauga un comentariu