{"id":792,"date":"2017-02-04T16:42:45","date_gmt":"2017-02-04T14:42:45","guid":{"rendered":"https:\/\/eugene.dullaard.nl\/?p=792"},"modified":"2018-01-01T23:12:29","modified_gmt":"2018-01-01T21:12:29","slug":"attiny85-one-button-timer","status":"publish","type":"post","link":"https:\/\/eugene.dullaard.nl\/?p=792","title":{"rendered":"ATTiny85 One Button Timer"},"content":{"rendered":"<p>For if you have a need. Single switch button with three LEDs to indicate the amount of time you want to set. In the configuration you can set the time per unit (20 minutes in the code below). After turning it on, the three LEDs should start to burn so to check they work, then when pressing the button it will start the time. You can at anytime adjust the time which will then be reset to the amount of time you want. If you want to decrease the time you have to cycle until the end after which it will go back to the lowest setting. <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-791\" src=\"https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/ButtonTimer.jpg\" alt=\"ButtonTimer\" width=\"591\" height=\"534\" srcset=\"https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/ButtonTimer.jpg 591w, https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/ButtonTimer-150x136.jpg 150w, https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/ButtonTimer-300x271.jpg 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/>Total times works in the same way as binary, given the value in the code is 20, the first LED is 20, the second 40(2x) and the last 80(4x). Now by pressing the button you can choose between\u00a020 to 140 minutes of delay before it switches off the timer.\u00a0 There is also a random time function which is activated when all LEDs are turned off, this will pick a random time between 40(2*) and 140(7*). As the amount of i\/o is limited I&#8217;ve used an ATTiny85 rather than a complete Arduino, by modifying which pins it is using the code can be used on an Arduino as well.<\/p>\n<pre class=\"brush: cpp; collapse: false; title: ; notranslate\" title=\"\">\r\n\/\/ Button Timer 1.0 for ATTiny85\r\n\/\/ Update 1.1 : Added 0 (no LEDS) as a random timer with a pre-defined minimum and maximum of 7\r\n\r\n\/\/ Control a device through relay or optocoupler for a set period of time.\r\n\/\/ Time can be adjusted during operation, cycle through all option before\r\n\/\/ returning to lower values again, following a binary like display.\r\n\r\n\/\/ Global Initialization\r\n\r\nint Timer = 20;       \/\/ Default timer value (lowest and incremental factor) = X\r\nint TimerInc = Timer; \/\/ Incremental Timer value to calculate Timer changes\r\nint TimeLED&#x5B;3];       \/\/ LEDs indicating time (X, X*2, X*4)\r\nint TimeLEDSP = 0;    \/\/ First of the three Pins for the TimeLEDs,\r\nint TimeInSecs;       \/\/ Timer expiration counter\r\nint TimerButton = 3;  \/\/ Button to change time\r\nint TimerRelay = 4;   \/\/ Relay that is on for duration of Timer\r\nint RandomOn = 0;\t  \/\/ 0 = Random off; 1 = Random on\r\nint RandomMin = 40;   \/\/ Minimum random time in minutes\r\n\r\nvoid setup() {\r\n  int SetupTMP;       \/\/ Temp Var within Setup\r\n  int SetupLoop = 1;  \/\/ Loop Var within Setup\r\n\r\n  \/\/ Mapping PINs and setting them to either input or output\r\n  for (SetupTMP = 0; SetupTMP &lt; 3; SetupTMP++) {\r\n    TimeLED&#x5B;SetupTMP] = SetupTMP + TimeLEDSP;\r\n    pinMode(TimeLED&#x5B;SetupTMP], OUTPUT);\r\n  }\r\n  pinMode(TimerButton, INPUT);\r\n  pinMode(TimerRelay, OUTPUT);\r\n  \r\n  \/\/seed random number generator with noise from an unconnected pin\r\n  randomSeed(analogRead(A0));\r\n\r\n}\r\n\r\nvoid loop() {\r\n  int MainTMP;        \/\/ Temp Var within Main\r\n  TimeInSecs = 60 * Timer;\r\n  \r\n  \/\/ Test Procedure, run all lights wait for TimerButton push, after push TimerRelay turns on\r\n  for (MainTMP = 0; MainTMP &lt; 3; MainTMP++) {\r\n    digitalWrite(TimeLED&#x5B;MainTMP], HIGH);\r\n  }\r\n  MainTMP = 1;\r\n  \r\n  while (MainTMP == 1) {\r\n    delay(1000);\r\n    if (digitalRead(TimerButton) == HIGH) {MainTMP = 0;}\r\n  }\r\n\r\n  for (MainTMP = 0; MainTMP &lt; 3; MainTMP++) {\r\n    digitalWrite(TimeLED&#x5B;MainTMP], LOW);\r\n  }\r\n  digitalWrite(TimerRelay, HIGH);\r\n\r\n  \/\/ Main Loop, wait until time runs out or button is pressed to increase time (one sec interval)\r\n  while (TimeInSecs &gt; 0) {\r\n\r\n    delay(1000);        \/\/ 1 Second delay\r\n    TimeInSecs--;\r\n    \r\n    \/\/ Button is pressed, add and recalculate time, if max is reached go to 0\/random value\r\n    if (digitalRead(TimerButton) == HIGH) {\r\n\t  if (RandomOn == 1) {\r\n\t    Timer = 0;\r\n\t\tRandomOn = 0;\r\n\t  }\r\n      Timer += TimerInc;\r\n      if (Timer &gt; (TimerInc * 7)) {Timer = 0;}\r\n      if (Timer &gt; 0) {TimeInSecs = 60 * Timer;}\r\n\t  else { \r\n\t    TimeInSecs = (random(RandomMin, (7 * Timer)) * 60);\r\n\t\tRandomOn = 1;\r\n\t  }\r\n    }\r\n\r\n    \/\/ Show Time through LEDs\r\n    MainTMP = Timer;\r\n    if (MainTMP &gt;= (TimerInc * 4)) {\r\n      digitalWrite(TimeLED&#x5B;2], HIGH);\r\n      MainTMP -= (TimerInc * 4);\r\n    }\r\n    else {\r\n      digitalWrite(TimeLED&#x5B;2], LOW);\r\n    }\r\n    if (MainTMP &gt;= (TimerInc * 2)) {\r\n      digitalWrite(TimeLED&#x5B;1], HIGH);\r\n      MainTMP -= (TimerInc * 2);\r\n    }\r\n    else {\r\n      digitalWrite(TimeLED&#x5B;1], LOW);\r\n    }\r\n    if (MainTMP &gt;= TimerInc) {\r\n      digitalWrite(TimeLED&#x5B;0], HIGH);\r\n      MainTMP -= TimerInc;\r\n    }\r\n    else {\r\n      digitalWrite(TimeLED&#x5B;0], LOW);\r\n    }\r\n    \r\n  }\r\n\r\n  \/\/ Turn relay off after timer has expired\r\n  digitalWrite(TimerRelay, LOW);\r\n\r\n}\r\n\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-824\" src=\"https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/btbuild.jpg\" alt=\"One Button Timer Build\" width=\"750\" height=\"568\" srcset=\"https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/btbuild.jpg 750w, https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/btbuild-150x114.jpg 150w, https:\/\/eugene.dullaard.nl\/wp-content\/uploads\/btbuild-300x227.jpg 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>My own build on a little bit of perfboard and put together with a relay in the smallest box I had lying around.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For if you have a need. Single switch button with three LEDs to indicate the amount of time you want to set. In the configuration you can set the time per unit (20 minutes in the code below). After turning it on, the three LEDs should start to burn so to check they work, then &hellip; <a href=\"https:\/\/eugene.dullaard.nl\/?p=792\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ATTiny85 One Button Timer<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[95,93],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-arduino","tag-sketch"],"_links":{"self":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=792"}],"version-history":[{"count":5,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":909,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions\/909"}],"wp:attachment":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}