{"id":31363,"date":"2024-02-02T22:58:56","date_gmt":"2024-02-02T22:58:56","guid":{"rendered":"https:\/\/www.pinballnews.com\/site\/?p=31363"},"modified":"2024-02-02T23:24:01","modified_gmt":"2024-02-02T23:24:01","slug":"pinball-ball-detector-part-two","status":"publish","type":"post","link":"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-two\/","title":{"rendered":"PINBALL BALL DETECTOR &#8211; PART TWO"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\" id=\"introduction\"><strong><span style=\"text-decoration: underline;\">Introduction<\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the <a href=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-one\/\" data-type=\"link\" data-id=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-one\/\" target=\"_blank\" rel=\"noreferrer noopener\">first part of this article<\/a>, Todd Andersen showed us how to create a programmable pinball ball detector which can power an add-on or topper so that it reacts when a ball is sensed in a particular location.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This operates using an infrared (IR)&nbsp;module to sense the ball, an Arduino Uno microcontroller, and a relay module to switch the power to the add-on or topper.  All these components are cheap, small and readily-available, while the use of a microcontroller provides lots of possibilities for additional inputs and outputs for far more complex interaction with the gameplay.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before that though, we need to programme the basic functionality of detecting the ball and then switching the power to the add-on or topper.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Back to Todd.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">I received feedback concerning a previous Pinball News two-part article (<a href=\"https:\/\/www.pinballnews.com\/site\/2022\/12\/24\/pinball-add-on-controller-part-one\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pinball Add-On Controller: Part One<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/www.pinballnews.com\/site\/2022\/12\/24\/pinball-add-on-controller-part-two\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pinball Add-On Controller: Part Two<\/a>) being too technical. I hope to rectify that with this pair of follow-up articles.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article is the second part and it concentrates on the software for the hardware featured in <a href=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-one\/\" data-type=\"link\" data-id=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-one\/\" target=\"_blank\" rel=\"noreferrer noopener\">part one<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I will again be using one of the free software choices preferred by many hobbyists, Arduino IDE. For those not versed in that software, don\u2019t worry. Though Pinball News has covered its use in a previous set of articles (<a href=\"https:\/\/www.pinballnews.com\/site\/2017\/01\/11\/intro-to-arduino\/\">Intro to Arduino<\/a> and <a href=\"https:\/\/www.pinballnews.com\/site\/2017\/01\/12\/pin-uino\/\">Pin-\u2018Uino<\/a>), the information will be covered in a slightly different way in this article.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"modules\"><strong><span style=\"text-decoration: underline;\">Modules<\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All of the devices we are using are powered by a five volt supply. We specifically chose them for that reason, plus the way their power indicators light up when the device is correctly powered. See the <a href=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-one\/\" data-type=\"link\" data-id=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-part-one\/\" target=\"_blank\" rel=\"noreferrer noopener\">hardware part of these articles<\/a> for the power connection details. The devices have also been chosen and configured so their input or output indicators light up when they activated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We are going to examine each of the three steps &#8211; Input, Controller, and Output &#8211; in order, by looking at the associated module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As has been the practise with these articles, each module will be examined by looking at the known information about the inputs and outputs, and then at their practical uses.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Input Device \u2013 IR Module<\/strong><br>    <em>What we know from the seller<\/em><br>        Input: &#8220;Detecting the reflected distance (1mm-25mm applicable)&#8221;<br>        Output: &#8220;Signal output indicator (while the beam is reflected, outputs low level, indicator lights up)&#8221;<br>    <em>Practical Use<\/em><br>        Input: Pinball is in front of the IR sensor pair<br>        Output: A LOW at the output pin<\/td><\/tr><tr><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Controller Device &#8211; Arduino Uno<\/strong><br>    <em>What we know from the seller<\/em><br>        Input: 5 volt-tolerant general purpose Analog and Digital pins.<br>        Output: 5 volt (HIGH) or 0 volt (LOW)<br>    <em>Practical Use<\/em><br>        Input: Analog pin (A0)<br>        Output: 5 volts (HIGH) at selected digital output pin #13<\/td><\/tr><tr><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Output Device &#8211; Relay Module<\/strong><br>    <em>What we know from the seller<\/em><br>        Input: &#8220;High or Low Level Trigger&#8221;<br>        Output: &#8220;1-Way&#8221; (with the indicator lighting up)<br>    <em>Practical Use<\/em><br>        Input: 5 volts (HIGH) output from digital pin #13 of the Arduino<br>        Output: A Normally Open (NO) connection<\/td><\/tr><tr><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"input-output-chain\"><strong><span style=\"text-decoration: underline;\">Input &amp; Output Chain<\/span><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes has-medium-font-size\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong><span style=\"text-decoration: underline;\">IR Module<\/span><br><\/strong>DO (output)<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><img loading=\"lazy\" decoding=\"async\" width=\"30\" height=\"43\" class=\"wp-image-31367\" style=\"width: 30px;\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1.png\" alt=\"\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1.png 787w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1-210x300.png 210w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1-716x1024.png 716w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1-768x1099.png 768w\" sizes=\"auto, (max-width: 30px) 100vw, 30px\" \/><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong><span style=\"text-decoration: underline;\">Arduino Uno<\/span><br><\/strong>A0 (input)<br>Digital 13 (output)<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong><img loading=\"lazy\" decoding=\"async\" width=\"30\" height=\"43\" class=\"wp-image-31367\" style=\"width: 30px;\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1.png\" alt=\"\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1.png 787w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1-210x300.png 210w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1-716x1024.png 716w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/website-artwork\/down_arrow_1-768x1099.png 768w\" sizes=\"auto, (max-width: 30px) 100vw, 30px\" \/><\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong><span style=\"text-decoration: underline;\">Relay Module<\/span><\/strong><br>IN (input)<br>COM &amp; NO (output)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"software\"><strong><span style=\"text-decoration: underline;\">Software<\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To communicate with the Arduino Uno you will need to install the Arduino Integrated Development Environment software, also known as the Arduino IDE.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can download that software and get the documentation from the <a href=\"https:\/\/www.arduino.cc\/en\/software\" data-type=\"link\" data-id=\"https:\/\/www.arduino.cc\/en\/software\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino website<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have installed that, connect the Arduino Uno&#8217;s USB port to your computer&#8217;s USB port.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, start the Arduino IDE software and make sure you have the correct type of microcontroller board selected. The method for doing this has changed with the latest versions of the Arduino IDE software. In older versions you select the board under the Tools menu.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector-1024x576.jpg\" alt=\"Selecting the Arduino Uno board in the Arduino IDE software\" class=\"wp-image-31369\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector-1024x576.jpg 1024w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector-300x169.jpg 300w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector-768x432.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector-1536x864.jpg 1536w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector-850x478.jpg 850w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/101-pinball-ball-detector.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Selecting the Arduino Uno board in the Arduino IDE software<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">In newer versions of the Arduino IDE the board can also be selected on the top toolbar.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"575\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector-1024x575.jpg\" alt=\"The newer method of selecting your controller in the Arduino IDE software\" class=\"wp-image-31685\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector-1024x575.jpg 1024w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector-300x168.jpg 300w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector-768x431.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector-1536x862.jpg 1536w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector-850x477.jpg 850w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/255-pinball-ball-detector.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The newer method of selecting your controller in the Arduino IDE software<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">When writing the code for our ball detector, it&#8217;s best to build and test in stages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because the IR module can be a little tricky to use to test the code, I used a capacitive switch module instead as the input device to test my starter code.  It has the same pins and output level as the IR module we will be using, but only requires a finger&#8217;s touch on the sensor to activate rather than a reflective ball.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the picture below, notice how the onboard yellow LED is lit when I touch the sensor.  This LED is connected to the Digital 13 pin which will eventually cause the relay module to activate.  The yellow LED indicates the software on the Arduino &#8211; called a &#8216;sketch&#8217; &#8211; is working correctly.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"829\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257-1024x829.jpg\" alt=\"The yellow LED connected to the Digital 13 pin is lit when the sensor is touched, showing the Arduino sketch is working correctly\" class=\"wp-image-31696\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257-1024x829.jpg 1024w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257-300x243.jpg 300w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257-768x622.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257-1536x1243.jpg 1536w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257-850x688.jpg 850w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/257.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The yellow LED connected to the Digital 13 pin is lit when the sensor is touched, showing the Arduino sketch is working correctly<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The final sketch is just a slightly built-up version of this test sketch, with additional comments added to help explain how it works.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"1009\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/104-pinball-ball-detector.png\" alt=\"The Arduino test sketch\" class=\"wp-image-31378\" style=\"width:450px\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/104-pinball-ball-detector.png 900w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/104-pinball-ball-detector-268x300.png 268w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/104-pinball-ball-detector-768x861.png 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/104-pinball-ball-detector-850x953.png 850w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><figcaption class=\"wp-element-caption\">The Arduino test sketch<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a commented version of the same test sketch.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"604\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-1024x604.jpg\" alt=\"The commented version of the Arduino sketch\" class=\"wp-image-31377\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-1024x604.jpg 1024w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-300x177.jpg 300w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-768x453.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-1536x906.jpg 1536w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-850x502.jpg 850w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector-413x244.jpg 413w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/103-pinball-ball-detector.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The commented version of the test Arduino sketch<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can view or download the test Arduino sketch with <a href=\"https:\/\/pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/IrSensModTestSketch.ino\" data-type=\"link\" data-id=\"https:\/\/pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/IrSensModTestSketch.ino\" target=\"_blank\" rel=\"noreferrer noopener\">this link<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although the final sketch is heavily commented for the user, here is an explanation of the sparse code.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><em>Line 1<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">void <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#6b8458\" class=\"has-inline-color\">setup <\/mark>()<\/strong><\/code><\/td><\/tr><tr><td>This is the specific way Arduino needs to initialize itself.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 3&nbsp;<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code><\/code><strong><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff9473\" class=\"has-inline-color\">pinMode <\/mark>(13, <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">OUTPUT<\/mark><\/strong>);<\/code><\/strong><\/td><\/tr><tr><td>This sets digital pin #13 as an output rather than an input. Pin 13 was specifically chosen for our project because it is also attached to the onboard LED. This LED is used as an easy indicator of the Arduino doing something.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 4&nbsp;&nbsp;<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff9473\" class=\"has-inline-color\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff9473\" class=\"has-inline-color\">pinMode<\/mark><\/strong> <\/mark><\/strong>(A0, <code><code><strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">INPUT<\/mark><\/strong><\/code><\/strong><\/code><\/code>);<\/strong><\/code><\/code><\/td><\/tr><tr><td>This sets Analog pin #0 as an input; rather than an in output. Pin #0 was chosen purely for convenience.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 7&nbsp;<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">void <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#6b8458\" class=\"has-inline-color\">loop <\/mark><\/strong><\/code>()<\/strong><\/code><\/code><\/td><\/tr><tr><td>This is the specific way Arduino needs to start running the code that follows.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 9<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#6b8458\" class=\"has-inline-color\">if <\/mark><\/strong><\/code>(<strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff9473\" class=\"has-inline-color\">digitalRead<\/mark><\/strong> (A0) == <code><code><strong><code><code><strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">LOW<\/mark><\/strong><\/code><\/strong><\/code><\/code><\/strong><\/code><\/code>)<\/strong><\/code><\/code><\/td><\/tr><tr><td>This asks the microprocessor to watch for a digital low signal on Analog pin #0. This is opposed to an analogRead which would give us a numerical value instead.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 11&nbsp;&nbsp;&nbsp;<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><code><code><strong><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff9473\" class=\"has-inline-color\">digitalWrite<\/mark><\/strong> <\/strong><\/code><\/code>(13, <code><code><strong><code><code><strong><code><code><strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">HIGH<\/mark><\/strong><\/code><\/strong><\/code><\/code><\/strong><\/code><\/code><\/strong><\/code><\/code>);<\/strong><\/code><\/code><\/td><\/tr><tr><td>This makes Digital pin #13 go high. A voltage appears on that pin and, in turn, is used to light the onboard LED and signal the relay module to turn on.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 13<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#6b8458\" class=\"has-inline-color\">else<\/mark><\/strong><\/code><\/strong><\/code><\/code><\/td><\/tr><tr><td>This line is looking for anything other than a digital low signal on Analog pin #0.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code><code><em>Line 15<\/em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><code><code><strong><code><code><strong><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff9473\" class=\"has-inline-color\">digitalWrite<\/mark><\/strong> <\/strong><\/code><\/code><\/strong><\/code><\/code>(13, <code><code><strong><code><code><strong><code><code><strong><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#28b4ca\" class=\"has-inline-color\">LOW<\/mark><\/strong><\/code><\/strong><\/code><\/code><\/strong><\/code><\/code><\/strong><\/code><\/code>);<\/strong><\/code><\/code><\/td><\/tr><tr><td>If anything else than a digital low signal is detected on Analog pin #0, a low state is set on Digital pin #13. This takes voltage away from the onboard LED and extinguishes it, while also turning off the module relay.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The rest of the symbols are required as part of the syntax for the software to run properly. This includes the semicolons (;) at the end of lines: 3, 4, 11, and 15. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Spaces are ignored, while the use of double forward slashes (\/\/) makes the software ignore whatever comes after them on the same line. So, we can use the space afterward to make notes about the code. A good source to learn the syntax of Arduino sketches is the <a href=\"https:\/\/www.arduino.cc\/\" data-type=\"link\" data-id=\"https:\/\/www.arduino.cc\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino website<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From here, we can get just a little more complex.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"607\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-1024x607.jpg\" alt=\"The final version of the Arduino sketch\" class=\"wp-image-31517\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-1024x607.jpg 1024w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-300x178.jpg 300w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-768x455.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-1536x910.jpg 1536w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-850x504.jpg 850w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector-413x244.jpg 413w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/108-pinball-ball-detector.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The final version of the Arduino sketch<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">This final version, thought not technically the best way to implement this software, it is OK for beginners and hobbyists. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can view or download the final Arduino sketch with <a href=\"https:\/\/pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/IrSensModFinalSketch.ino\" data-type=\"link\" data-id=\"https:\/\/pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/IrSensModTestSketch.ino\" target=\"_blank\" rel=\"noreferrer noopener\">this link<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this version we have added a short delay (measured in milliseconds) to the digitalWrite (line #19). This delay makes the output more stable by ensuring Digital pin #13 goes HIGH for a minimum amount of time, no matter how briefly the ball is detected by the IR module. This works well to make an LED momentarily flash in a topper or for the contacts of a relay to remain closed long enough to be registered by, say, the switch matrix of a pinball machine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The value of the &#8216;dwell&#8217; delay set in line #5 can be adjusted for your own use. For example, the value of 300 might be too long to \u2018catch\u2019 two pinballs coming down the same ramp closely together, but the same value may be too short for a specific LED lighting effect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can see that the majority of the work was understanding the operation of the three main components and how to link them together. Even for coding the sketch, the majority of the work was in the set up. Just one short section of the code &#8211; lines #16 through #19 &#8211; did the magic part.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have described how to make this pinball ball detector using an Arduino Uno, but you can use <a href=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-alternative-microcontrollers\/\" data-type=\"link\" data-id=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-alternative-microcontrollers\/\" target=\"_blank\" rel=\"noreferrer noopener\">other microcontroller boards instead<\/a>. Below you can see all three components working together on a cheap Arduino Nano clone mounted on a screw terminal breakout board, with all three power and status indicator LEDs lit as expected.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"575\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector-1024x575.jpg\" alt=\"Tested and ready to install\" class=\"wp-image-31518\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector-1024x575.jpg 1024w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector-300x168.jpg 300w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector-768x431.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector-1536x862.jpg 1536w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector-850x477.jpg 850w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/109-pinball-ball-detector.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Tested and ready to install<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong><span style=\"text-decoration: underline;\">Summary<\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We set up an input on the Arduino. That input was provided by the IR input module. The Arduino saw that input and acted on it. The action was to provide a signal to the output relay module. What you use the relay to switch on and off is left to your creativity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><span style=\"text-decoration: underline;\">Hints-n-Tips<\/span><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 One thing to remember is that third-party Arduino clone boards usually don\u2019t have the latest firmware (bootloader) installed. So if you can\u2019t \u2018talk\u2019 to your board with the Arduino IDE software, try selecting an older version of the board under Tools\/Processor or on the top toolbar.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 Infra Red (IR) input modules with potentiometers for sensitivity adjustment are the most versatile.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 When used on the top the playfield, the better IR module to use will be the vertically-oriented variety, mounted above the point of detection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 Know whether your input IR module produces a <strong>HIGH <\/strong>or <strong>LOW <\/strong>signal and adjust your code accordingly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 Verify or set the jumper on your output relay module to the &#8216;H&#8217; (<strong>HIGH<\/strong>) or&nbsp;\u2018<strong>L<\/strong>\u2018 (<strong>LOW<\/strong>)&nbsp;position.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 Double-check all of your connections before first powering your creation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 Ensure your creation is not electrically shorting against, or physically binding, anything in your pinball machine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 Changing ambient lighting, in-game lighting or playfield flashers can sometimes give false signals which trigger the IR module. False triggering can usually be fixed by carefully adjusting the sensitivity potentiometer on the IR module. The vertically-oriented IR module has a built in baffle\/beam separator making it physically more immune to the effects of ambient light.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 If your IR input module seems to start acting strangely during gameplay, check how its output\/signal indicator LED is behaving. This may be an indication that ambient light is interfering with the sensor, in which case you may need to place a little heat shrink tubing around the bodies of the IR sensor pair. Be sure the lenses\/tips aren&#8217;t obstructed though.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"1121\" src=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector.jpg\" alt=\"Heat shrink tubing around the IR transmitter and receiver, with un-shrunk tubing protecting the module too\" class=\"wp-image-31691\" style=\"object-fit:cover;width:550px;height:550px\" srcset=\"https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector.jpg 900w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector-241x300.jpg 241w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector-822x1024.jpg 822w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector-768x957.jpg 768w, https:\/\/www.pinballnews.com\/site\/wp-content\/uploads\/learn\/pinball-ball-detector\/256-pinball-ball-detector-850x1059.jpg 850w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption class=\"wp-element-caption\">Heat shrink tubing around the IR transmitter and receiver, with un-shrunk tubing protecting the module too<\/figcaption><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">While these two parts show you how to use an Arduino Uno to build and programme your ball detector and relay, as Todd say, you can use many other microcontroller in place of the Uno.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Todd has produced an additional article listing many alternatives to the Uno, highlighting four of them to detail their unique properties and providing example sketches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-alternative-microcontrollers\/\" data-type=\"link\" data-id=\"https:\/\/www.pinballnews.com\/site\/2024\/02\/02\/pinball-ball-detector-alternative-microcontrollers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to view his list of alternative microcontroller boards.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article concentrates on the software for the ball-detection hardware featured in part one.<br \/>\nI will again be using one of the free software choices preferred by many hobbyists, Arduino IDE. <\/p>\n","protected":false},"author":2,"featured_media":31525,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[1943,148,1909,149,2044,2045],"class_list":["post-31363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn","tag-add-on","tag-arduino","tag-ir","tag-led","tag-module","tag-topper"],"_links":{"self":[{"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/posts\/31363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/comments?post=31363"}],"version-history":[{"count":9,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/posts\/31363\/revisions"}],"predecessor-version":[{"id":31714,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/posts\/31363\/revisions\/31714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/media\/31525"}],"wp:attachment":[{"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/media?parent=31363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/categories?post=31363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pinballnews.com\/site\/wp-json\/wp\/v2\/tags?post=31363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}