{"id":664,"date":"2011-03-18T16:40:30","date_gmt":"2011-03-18T21:40:30","guid":{"rendered":"http:\/\/nootropicdesign.com\/projectlab\/?p=664"},"modified":"2018-11-02T09:11:30","modified_gmt":"2018-11-02T14:11:30","slug":"reaction-timer","status":"publish","type":"post","link":"https:\/\/nootropicdesign.com\/projectlab\/2011\/03\/18\/reaction-timer\/","title":{"rendered":"Reaction Timer"},"content":{"rendered":"<p><strong><em>Difficulty Level = 1<\/em><\/strong>  <a href=\"\/projectlab\/difficulty-levels\/\">[What&#8217;s this?]<\/a><\/p>\n<p>How fast can you press a button once a timer starts counting?  Find out by attaching a button to your Arduino and the <a href=\"\/digitshield\">Digit Shield<\/a>.  A simple tactile button switch is connected to digital pin 8 and ground.  <\/p>\n<p><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2011\/03\/reactionTimer.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2011\/03\/reactionTimer.jpg\" alt=\"\" title=\"reactionTimer\" width=\"640\" height=\"480\" class=\"alignleft size-full wp-image-670\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2011\/03\/reactionTimer.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2011\/03\/reactionTimer-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><br \/>\n<br clear=\"all\"\/><br \/>\nPress the button once to start the sequence.  First the Arduino will wait a random amount of time between 2 and 5 seconds, then it will start counting milliseconds on the display.  As soon as you see the numbers start, press the button again to stop the count.  Then repeat: press the button again to clear the display and start the random wait before the numbers start counting again.  <\/p>\n<p>Here is the complete code:<\/p>\n<pre class=\"codeblock\">\r\n#include &lt;DigitShield.h&gt;\r\n#define BUTTON 8\r\n\r\nunsigned long start, stop;\r\n\r\nvoid setup() {\r\n  randomSeed(analogRead(0));\r\n\r\n  pinMode(8, INPUT);\r\n  digitalWrite(8, HIGH);\r\n\r\n  DigitShield.begin();\r\n  DigitShield.setBlank(true);\r\n}\r\n\r\nvoid loop() {\r\n  \/\/ wait until button press\r\n  while (digitalRead(BUTTON) == HIGH);\r\n\r\n  \/\/ turn off the display\r\n  DigitShield.setBlank(true);\r\n\r\n  \/\/ delay from 2 to 5 seconds\r\n  delay(random(2000, 5000));\r\n  start = millis();\r\n  DigitShield.setBlank(false);\r\n\r\n  while (true) {\r\n    DigitShield.setValue((int)(millis() - start));\r\n    if (digitalRead(BUTTON) == LOW) {\r\n      stop = millis();\r\n      break;\r\n    }\r\n  }\r\n\r\n  DigitShield.setValue((int)(stop-start));\r\n  delay(1000);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Difficulty Level = 1 [What&#8217;s this?] How fast can you press a button once a timer starts counting? Find out by attaching a button to your Arduino and the Digit Shield. A simple tactile button switch is connected to digital pin 8 and ground. Press the button once to start the sequence. First the Arduino [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-arduino"],"_links":{"self":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/comments?post=664"}],"version-history":[{"count":12,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":1959,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/664\/revisions\/1959"}],"wp:attachment":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}