{"id":2242,"date":"2018-08-06T11:01:49","date_gmt":"2018-08-06T08:01:49","guid":{"rendered":"http:\/\/old.razwww.ro\/?p=2242"},"modified":"2019-02-23T14:32:08","modified_gmt":"2019-02-23T12:32:08","slug":"cat-counter","status":"publish","type":"post","link":"https:\/\/old.razwww.ro\/index.php\/2018\/08\/06\/cat-counter\/","title":{"rendered":"Cat counter"},"content":{"rendered":"<p>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 \ud83d\ude42 ) approaches within 12 cm from the sensor. Also the back light is turned on for 5 seconds after the initial approach.<\/p>\n<p>After the event all approaches are ignored for the next 2 minutes.<\/p>\n<p>If the button is pressed, the LCD backlight is turned on for 5 seconds.<\/p>\n\n\t\t<style>\n\t\t\t#gallery-1 {\n\t\t\t\tmargin: auto;\n\t\t\t}\n\t\t\t#gallery-1 .gallery-item {\n\t\t\t\tfloat: left;\n\t\t\t\tmargin-top: 10px;\n\t\t\t\ttext-align: center;\n\t\t\t\twidth: 33%;\n\t\t\t}\n\t\t\t#gallery-1 img {\n\t\t\t\tborder: 2px solid #cfcfcf;\n\t\t\t}\n\t\t\t#gallery-1 .gallery-caption {\n\t\t\t\tmargin-left: 0;\n\t\t\t}\n\t\t\t\/* see gallery_shortcode() in wp-includes\/media.php *\/\n\t\t<\/style>\n\t\t<div id='gallery-1' class='gallery galleryid-2242 gallery-columns-3 gallery-size-thumbnail'><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon landscape'>\n\t\t\t\t<a href='https:\/\/old.razwww.ro\/numarator_de_pisici_schem_editat\/'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"101\" src=\"https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/numarator_de_pisici_schem_editat-400x270.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"Schematic\" srcset=\"https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/numarator_de_pisici_schem_editat-400x270.png 400w, https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/numarator_de_pisici_schem_editat-768x518.png 768w, https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/numarator_de_pisici_schem_editat.png 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>\n\t\t\t<\/dt><\/dl><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon landscape'>\n\t\t\t\t<a href='https:\/\/old.razwww.ro\/img_20180731_161908_text\/'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"138\" src=\"https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180731_161908_text-400x367.jpg\" class=\"attachment-thumbnail size-thumbnail\" alt=\"Real circuit\" srcset=\"https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180731_161908_text-400x367.jpg 400w, https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180731_161908_text-768x705.jpg 768w, https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180731_161908_text.jpg 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>\n\t\t\t<\/dt><\/dl><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon landscape'>\n\t\t\t\t<a href='https:\/\/old.razwww.ro\/img_20180605_185926_small\/'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"113\" src=\"https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180605_185926_small-400x300.jpg\" class=\"attachment-thumbnail size-thumbnail\" alt=\"Used\" srcset=\"https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180605_185926_small-400x300.jpg 400w, https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180605_185926_small-768x576.jpg 768w, https:\/\/old.razwww.ro\/wp-content\/uploads\/2018\/08\/IMG_20180605_185926_small.jpg 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>\n\t\t\t<\/dt><\/dl><br style=\"clear: both\" \/>\n\t\t<\/div>\n\n<p>BTW, my cat eats between 10 and 22 times per day and drinks water between 8 and 15 times per day \ud83d\ude42<\/p>\n<p>The code:<\/p>\n<pre>[code lang=\"cpp\"]\/*\n  Copyright \u00a9 2018 Razvan Murariu &amp;amp;amp;amp;amp;lt;razwww@gmail.com&amp;amp;amp;amp;amp;gt;\n  This program is free software. It comes without any warranty, to\n  the extent permitted by applicable law. You can redistribute it\n  and\/or modify it under the terms of the Do What The Fuck You Want\n  To Public License, Version 2, as published by Sam Hocevar. See\n  the COPYING file for more details.\n\n  The circuit:\n  Arduino Nano\n  trigPin: A1\n  echoPin: A0\n  display SCL: A4\n  display SDA: A5\n  button (NC): between 5V and D2\n  D2 pulled-down to ground with a 10 K\u03a9 resistor\n*\/\n\n#include &amp;amp;amp;amp;lt;LiquidCrystal_I2C.h&amp;amp;amp;amp;gt;\n\nLiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);\n\nconst long intervalEat = 120000;\nconst long intervalLight = 5000;\nint distance = 12;\nlong duration;\nint distanceCm;\nint counter = 0;\nint buttonState = 0;\nunsigned long previousMillisEat = 0;\nunsigned long previousMillisButton = 0;\nconst int trigPin = A1;\nconst int echoPin = A0;\n\nvoid setup() {\n  lcd.begin(16,2);\n\n  Serial.begin(9600);\n\n  pinMode(trigPin, OUTPUT);\n  pinMode(echoPin, INPUT);\n\n  pinMode(13, OUTPUT);\n  pinMode(2, INPUT); \/\/ button for backlight\n\n  lcd.setCursor(0,0);\n  lcd.print(&quot;0   pisici&quot;);\n  lcd.backlight();\n  delay(intervalLight);\n  lcd.noBacklight();\n}\n\nvoid loop() {\n  unsigned long currentMillis = millis();\n\n  delay(10);\n  digitalWrite(trigPin, LOW);\n  delayMicroseconds(2);\n  digitalWrite(trigPin, HIGH);\n  delayMicroseconds(10);\n  digitalWrite(trigPin, LOW);\n  duration = pulseIn(echoPin, HIGH);\n  distanceCm= duration*0.034\/2;\n\n  \/\/ distance smaller than 12 cm &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; (just powered on || intervalEat passed)\n  if (distanceCm &amp;amp;amp;amp;lt; distance &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; ((currentMillis &amp;amp;amp;amp;lt; intervalEat &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; counter == 0) || (currentMillis - previousMillisEat &amp;amp;amp;amp;gt;= intervalEat))) {\n    previousMillisEat = currentMillis;\n    counter++;\n    Serial.print(distanceCm);\n    Serial.println(&quot; cm&quot;);\n    Serial.print(counter);\n    Serial.println(&quot; pisici&quot;);\n    Serial.println(currentMillis);\n    lcd.backlight();\n    lcd.setCursor(0,0);\n    lcd.print(counter);\n    delay(intervalLight);\n    lcd.noBacklight();\n  }\n\n  buttonState = digitalRead(2);\n  if (buttonState == LOW) {\n    Serial.println(&quot;Buton&quot;);\n    lcd.backlight();\n    previousMillisButton = currentMillis;\n    }\n\n  if ( currentMillis - previousMillisButton &amp;amp;amp;amp;gt;= intervalLight ) {\n    lcd.noBacklight();\n    }\n\n}\n[\/code]<\/pre>\n<p>The most recent version of the code is found on <a href=\"https:\/\/github.com\/razwww\/cat-counter\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \ud83d\ude42 ) 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2242","post","type-post","status-publish","format-standard","hentry","category-arduino"],"_links":{"self":[{"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/posts\/2242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/comments?post=2242"}],"version-history":[{"count":2,"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/posts\/2242\/revisions"}],"predecessor-version":[{"id":2454,"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/posts\/2242\/revisions\/2454"}],"wp:attachment":[{"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/media?parent=2242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/categories?post=2242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/old.razwww.ro\/index.php\/wp-json\/wp\/v2\/tags?post=2242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}