Title: Double Transducer: Color to Linear Position

Overhead shot of the double transducer. The main components are labelled with names and numbers 1-3 which correspond to the order by which things are transduced.

Servo motor arm close-up. The black pin moves linearly along the track. The pieces were laser cut and attached using pins and glue.

The DIY color sensor without the color insert. RGB LEDs flash in sequence inside the 3D printed cover. The blue LED is on in this instance.

The working sequence. The arm extends when the color sensor detects red; once the red coin is removed, the arm retracts.

Description: A power source powers a color sensor, which sends a signal to generate a magnetic field when it detects a change in color. The presence of a magnetic field will in turn cause the motor to turn, moving an arm linearly along a track.

Progress:

Here we are testing the range of the servo motor arm and the distance this translates to along the track.

The failed first attempt at 3D printing a black-out cover for the color sensor. The tube was too small to cover the 3 LEDs and the photocell. We also chose red and yellow inserts for the final.

Wrapping copper wire to create the coils. This took some experimentation because we didn’t wrap around enough times in the first few attempts.

Discussion:

The makeshift RGB sensor worked surprisingly well after being wired up correctly, although the compact design increased the difficulty of properly wiring 3 LEDs and a photoresistor into the space of a little more than a square centimeter of space. The length of the legs of the LEDs were rather close, so at least one of the LEDs was inserted in the wrong direction initially. Despite minor problems, the RGB signal distinctly changed by about 100 for the red value after the red colored disk was inserted, which was a great success. However, it should be noted that the red signal was comparatively weaker, even with nothing else within the black casing, suggesting the possibility that the light from the previous LED isn’t given enough time to fade away. Admittedly, these were issues for the color sensor that weren’t predicted when designing the code and schematic from a purely theoretical perspective, but it did work as desired; this simple sensor exemplifies how valuable planning ahead was, yet it also reinforces how certain challenges might only be discovered in practice.

The solenoid system was neither simple or ended up working all too well. Numerous challenges arose, some from construction and others from human error. Although advice (thank you Zach for all the help with getting the solenoid circuits set up)  and research produced circuits that would ideally work well with more powerful solenoids,  the initial solenoid wire coils were initially only around a half-centimeter thick. So while the ‘drive coil’ (the coil that’s actively charged/discharged to produce a changing magnetic flux) could produce a tangible ‘pulse’ in a ceramic magnet disc held nearby, no signal could be detected in the receiving coil. After expanding the wire coils to a thickness of well over a centimeter, there still appeared to be no readable signal. This time it was due in part how the code, written beforehand, had used ‘digitalRead()’ instead of ‘analogRead()’, which made it impossible to detect the weak signal. Irregularities with how the Arduino was powered also interfered with consistent results. Overall, this experimental feature was a unique experience to test an unconventional signal transduction method and presented its fair share of challenges. Exercising good caution in design prevented any accidents from happening, though it might’ve been better to remove an existing coil from some motor or other component that was already wrapped and saved that time.

The linear motion servo made for little difficulty on the coding end since a library was available to easily interface with the device, requiring just a little calibration. Coming up with a design to convert its rotation to linear motion and constructing the largely mechanical instead of electrical component was another matter.

Schematic:

Code:

  1. <span class="com">/* Project 1: Double Transducer
  2. Description: This code detects a color signal with a photoresistor and RGB LEDs, transmits that signal across two inductors,
  3. and passes out a 'positive' signal by driving a servo arm to extend a linear motion device.
  4. Resources Referenced:
  5. https://www.arduino.cc/reference/en/
  6. http://www.physics-and-radio-electronics.com/electronic-devices-and-circuits/rectifier/bridgerectifier.html
  7. https://create.arduino.cc/projecthub/mjrobot/arduino-color-detection-57e4ce?ref=tag&ref_id=color-detector&offset=0
  8. https://www.instructables.com/id/How-to-Control-an-Electromagnet-with-an-Arduino/
  9. Outputs
  10. Arduino Pin | Output
  11. 2 Red LED
  12. 3 Green LED
  13. 4 Blue LED
  14. 6 Output inductor Pin
  15. 7 Servo Pin
  16. Input
  17. Arduino Pin | Input
  18. A0 Photoresistor
  19. A5 Input inductor Pin
  20. */</span><span class="pln">
  21. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><Servo.h></span><span class="pln">
  22. </span><span class="com">//Linear Motion</span><span class="pln">
  23. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVO_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. Servo control pin.</span><span class="pln">
  24. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> rPeriod </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1000</span><span class="pun">;</span><span class="pln"> </span><span class="com">//Parameter: refractory period for servo. Time for which a change to servo state cannot be changed again.</span><span class="pln">
  25. </span><span class="typ">Servo</span><span class="pln"> arm</span><span class="pun">;</span><span class="pln"> </span><span class="com">//Servo 'object'</span><span class="pln">
  26. </span><span class="kwd">bool</span><span class="pln"> moveConfirmed </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY: when true, arm extends at the end of the next refractory period, otherwise it'll retract</span><span class="pln">
  27. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> lastmove </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY: counter to determine time until refractory ends</span><span class="pln">
  28. </span><span class="com">//magnetic domain</span><span class="pln">
  29. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> O_MAG_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. Controls transistor powering the inductor.</span><span class="pln">
  30. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> I_MAG_PIN </span><span class="pun">=</span><span class="pln"> A5</span><span class="pun">;</span><span class="pln"> </span><span class="com">//INPUT, analog. Reads the read inductor</span><span class="pln">
  31. </span><span class="com">//magnetic domain: output</span><span class="pln">
  32. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> togThresh </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span><span class="pun">;</span><span class="pln"> </span><span class="com">//PARAMETER, toggle output frequency in milliseconds. This determines how frequently the output inductor produces flux when it is permitted to do so.</span><span class="pln">
  33. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> lasttoggle </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, toggle: Time since magnet signal was last toggled on/off. Only when the magnet is toggled</span><span class="pln">
  34. </span><span class="kwd">bool</span><span class="pln"> toggle </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, toggle: used to determine which toggle step, on or off.</span><span class="pln">
  35. </span><span class="com">//magetic domain: input</span><span class="pln">
  36. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> thresh </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, minimum signal from the input pin from the inductor classified as a read</span><span class="pln">
  37. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> readTime </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, milliseconds taken by the entire read step</span><span class="pln">
  38. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> readFrq </span><span class="pun">=</span><span class="pln"> </span><span class="lit">20</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, number of times reads are taken in the</span><span class="pln">
  39. </span><span class="com">//Color Sensor</span><span class="pln">
  40. </span><span class="com">//(light detector)</span><span class="pln">
  41. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> PHOTO_PIN </span><span class="pun">=</span><span class="pln"> A0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//INPUT, analog. Photoresistor pin. Signal corresponds to light level.</span><span class="pln">
  42. </span><span class="com">//(LED colors)</span><span class="pln">
  43. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> R_LED_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. RED LED pin, flashes first in series.</span><span class="pln">
  44. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> G_LED_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">3</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. GREEN LED pin, flashes second in series.</span><span class="pln">
  45. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> B_LED_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">4</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. BLUE LED pin, flashes last in series.</span><span class="pln">
  46. </span><span class="kwd">int</span><span class="pln"> target</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="lit">300</span><span class="pun">,</span><span class="pln"> </span><span class="lit">460</span><span class="pun">,</span><span class="pln"> </span><span class="lit">230</span><span class="pun">};</span><span class="pln"> </span><span class="com">//PARAMETER for the RED disk, must be calibrated based on lighting levels</span><span class="pln">
  47. </span><span class="com">//NOTE: Pervious testing values: casing has a color of about (195, 380, 295). RED side has about (270, 400, 250)</span><span class="pln">
  48. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> tolerance </span><span class="pun">=</span><span class="pln"> </span><span class="lit">15</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, +/- range that color matches target</span><span class="pln">
  49. </span><span class="kwd">int</span><span class="pln"> color</span><span class="pun">[</span><span class="lit">3</span><span class="pun">];</span><span class="pln"> </span><span class="com">//UTILITY, virtual RGB container variable representing the reflected color data</span><span class="pln">
  50. </span><span class="kwd">void</span><span class="pln"> setup</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  51. </span><span class="com">// put your setup code here, to run once:</span><span class="pln">
  52. </span><span class="com">//For debugging and viewing the inductor receiving signal</span><span class="pln">
  53. </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">(</span><span class="lit">19200</span><span class="pun">);</span><span class="pln">
  54. </span><span class="com">//Linear Motion</span><span class="pln">
  55. </span><span class="com">//OUTPUT</span><span class="pln">
  56. arm</span><span class="pun">.</span><span class="pln">attach</span><span class="pun">(</span><span class="pln">SERVO_PIN</span><span class="pun">);</span><span class="pln">
  57. </span><span class="com">//Magnetic domain</span><span class="pln">
  58. </span><span class="com">//INPUT</span><span class="pln">
  59. pinMode</span><span class="pun">(</span><span class="pln">I_MAG_PIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  60. </span><span class="com">//OUTPUT</span><span class="pln">
  61. pinMode</span><span class="pun">(</span><span class="pln">O_MAG_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln"> </span><span class="com">//To transistor</span><span class="pln">
  62. </span><span class="com">//Color Sensor</span><span class="pln">
  63. </span><span class="com">//INPUT</span><span class="pln">
  64. pinMode</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  65. </span><span class="com">//OUTPUT</span><span class="pln">
  66. pinMode</span><span class="pun">(</span><span class="pln">R_LED_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  67. pinMode</span><span class="pun">(</span><span class="pln">G_LED_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  68. pinMode</span><span class="pun">(</span><span class="pln">B_LED_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  69. delay</span><span class="pun">(</span><span class="lit">1000</span><span class="pun">);</span><span class="pln"> </span><span class="com">//wait for the servo arm, as there may be cases where the signal is immediately present as soon as loop goes through</span><span class="pln">
  70. </span><span class="pun">}</span><span class="pln">
  71. </span><span class="kwd">void</span><span class="pln"> loop</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  72. </span><span class="com">// put your main code here, to run repeatedly:</span><span class="pln">
  73. </span><span class="com">//Perform detection: check if RGB has changed enough to match some signal color</span><span class="pln">
  74. colorRead</span><span class="pun">(</span><span class="lit">100</span><span class="pun">);</span><span class="pln"> </span><span class="com">//Color detection; values stored in global array with a delay of 100 between each color.</span><span class="pln">
  75. </span><span class="com">//Debugging line: Color input</span><span class="pln">
  76. </span><span class="typ">Serial</span><span class="pun">.</span><span class="pln">println</span><span class="pun">((</span><span class="typ">String</span><span class="pun">)(</span><span class="pln">color</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">" "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="typ">String</span><span class="pun">)(</span><span class="pln">color</span><span class="pun">[</span><span class="lit">1</span><span class="pun">])</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">" "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="typ">String</span><span class="pun">)(</span><span class="pln">color</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]));</span><span class="pln">
  77. </span><span class="com">//if condition is met, power the inductor</span><span class="pln">
  78. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">inThreshold</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  79. driveInductor</span><span class="pun">();</span><span class="pln">
  80. </span><span class="pun">}</span><span class="pln">
  81. </span><span class="com">//read the receivor inductor and confirm it has moved</span><span class="pln">
  82. moveConfirmed </span><span class="pun">=</span><span class="pln"> moveConfirmed </span><span class="pun">||</span><span class="pln"> readInductor</span><span class="pun">();</span><span class="pln">
  83. </span><span class="com">//Attempts to move servo arm. Only proceeds if the refractory period has passed</span><span class="pln">
  84. moveServoArm</span><span class="pun">();</span><span class="pln">
  85. </span><span class="pun">}</span><span class="pln">
  86. </span><span class="com">//move servo arm out and in</span><span class="pln">
  87. </span><span class="kwd">void</span><span class="pln"> moveServoArm</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  88. </span><span class="com">//whenever the refractory period has passed, the arm is allowed to move again</span><span class="pln">
  89. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">millis</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> lastmove </span><span class="pun">></span><span class="pln"> rPeriod</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  90. </span><span class="com">//moves arm when enough time has passed</span><span class="pln">
  91. arm</span><span class="pun">.</span><span class="pln">write</span><span class="pun">((</span><span class="pln">moveConfirmed</span><span class="pun">)</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> </span><span class="lit">60</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">5</span><span class="pun">);</span><span class="pln"> </span><span class="com">//If a move signal is measured at this step, move the arm out, otherwise pull it back in.</span><span class="pln">
  92. lastmove </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
  93. moveConfirmed </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  94. </span><span class="pun">}</span><span class="pln">
  95. </span><span class="pun">}</span><span class="pln">
  96. </span><span class="com">//charge and discharge inductor to produce a varying flux</span><span class="pln">
  97. </span><span class="kwd">void</span><span class="pln"> driveInductor</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  98. </span><span class="com">//if time since last toggle exceeds the threshold, toggle the inductor drive pin</span><span class="pln">
  99. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">abs</span><span class="pun">(</span><span class="pln">millis</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> lasttoggle</span><span class="pun">)</span><span class="pln"> </span><span class="pun">></span><span class="pln"> togThresh</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  100. digitalWrite</span><span class="pun">(</span><span class="pln">O_MAG_PIN</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span class="pln">toggle</span><span class="pun">)</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> HIGH </span><span class="pun">:</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
  101. lasttoggle </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
  102. toggle </span><span class="pun">=</span><span class="pln"> </span><span class="pun">!</span><span class="pln">toggle</span><span class="pun">;</span><span class="pln">
  103. </span><span class="pun">}</span><span class="pln">
  104. </span><span class="pun">}</span><span class="pln">
  105. </span><span class="com">//indicates whether there is substantial voltage induced from changing flux in the read inductor</span><span class="pln">
  106. </span><span class="kwd">bool</span><span class="pln"> readInductor</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  107. </span><span class="com">//records if the signal ever exceeds a threshold</span><span class="pln">
  108. </span><span class="kwd">bool</span><span class="pln"> successfulRead </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  109. </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> readFrq</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  110. </span><span class="kwd">int</span><span class="pln"> fluxread </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">I_MAG_PIN</span><span class="pun">);</span><span class="pln">
  111. </span><span class="com">//Debugging: Flux measurement results</span><span class="pln">
  112. </span><span class="com">//Serial.println(fluxread);</span><span class="pln">
  113. </span><span class="com">//checks if signal exceeds a threshold.</span><span class="pln">
  114. successfulRead </span><span class="pun">=</span><span class="pln"> successfulRead </span><span class="pun">||</span><span class="pln"> fluxread </span><span class="pun">></span><span class="pln"> thresh</span><span class="pun">;</span><span class="pln">
  115. delay</span><span class="pun">(</span><span class="pln">readTime </span><span class="pun">/</span><span class="pln"> readFrq</span><span class="pun">);</span><span class="pln"> </span><span class="com">//divides the delay time into readFrq number of segments for a total of readTime worth of time</span><span class="pln">
  116. </span><span class="pun">}</span><span class="pln">
  117. </span><span class="kwd">return</span><span class="pln"> successfulRead</span><span class="pun">;</span><span class="pln">
  118. </span><span class="pun">}</span><span class="pln">
  119. </span><span class="com">//Checks if light is within the threshold of being a target</span><span class="pln">
  120. </span><span class="kwd">bool</span><span class="pln"> inThreshold</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  121. </span><span class="com">//loops for 1,2,3 or RGB to check all values are within tolerance threshold</span><span class="pln">
  122. </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> </span><span class="lit">3</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  123. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">abs</span><span class="pun">(</span><span class="pln">target</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> color</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])</span><span class="pln"> </span><span class="pun">></span><span class="pln"> tolerance</span><span class="pun">)</span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  124. </span><span class="pun">}</span><span class="pln">
  125. </span><span class="com">//if for no color it returns false, then all colors are in tolerance</span><span class="pln">
  126. </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
  127. </span><span class="pun">}</span><span class="pln">
  128. </span><span class="com">//Checks color by flashing each RGB LED color for reflection from chamber and sets value in color array</span><span class="pln">
  129. </span><span class="com">//frq variable used to control delay between reads, or the speed of the flashes.</span><span class="pln">
  130. </span><span class="com">//In theory, a longer delay would result in higher accuracy but sacrifice responsiveness and speed</span><span class="pln">
  131. </span><span class="kwd">void</span><span class="pln"> colorRead</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> frq</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  132. setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln"> </span><span class="com">//reset to no LEDs</span><span class="pln">
  133. setLED</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln"> </span><span class="com">//RED LED ON</span><span class="pln">
  134. delay</span><span class="pun">(</span><span class="pln">frq</span><span class="pun">);</span><span class="pln">
  135. color</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">//read RED</span><span class="pln">
  136. setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln">
  137. setLED</span><span class="pun">(</span><span class="lit">2</span><span class="pun">);</span><span class="pln"> </span><span class="com">//GREEN LED ON</span><span class="pln">
  138. delay</span><span class="pun">(</span><span class="pln">frq</span><span class="pun">);</span><span class="pln">
  139. color</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">//read GREEN</span><span class="pln">
  140. setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln">
  141. setLED</span><span class="pun">(</span><span class="lit">3</span><span class="pun">);</span><span class="pln"> </span><span class="com">//BLUE LED ON</span><span class="pln">
  142. delay</span><span class="pun">(</span><span class="pln">frq</span><span class="pun">);</span><span class="pln">
  143. color</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">//read BLUE</span><span class="pln">
  144. setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln">
  145. </span><span class="pun">}</span><span class="pln">
  146. </span><span class="com">//Sets the LEDs on/off based on a key value. A switch case might work even better, but this function is still perfectly functional.</span><span class="pln">
  147. </span><span class="kwd">void</span><span class="pln"> setLED</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> key</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  148. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  149. digitalWrite</span><span class="pun">(</span><span class="pln">R_LED_PIN</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
  150. digitalWrite</span><span class="pun">(</span><span class="pln">G_LED_PIN</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
  151. digitalWrite</span><span class="pun">(</span><span class="pln">B_LED_PIN</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
  152. </span><span class="pun">}</span><span class="pln">
  153. </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="pln">R_LED_PIN</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span><span class="pln">
  154. </span><span class="com">//else digitalWrite(R_LED_PIN,LOW);</span><span class="pln">
  155. </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="pln">G_LED_PIN</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span><span class="pln">
  156. </span><span class="com">//else digitalWrite(G_LED_PIN,LOW);</span><span class="pln">
  157. </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="pln">B_LED_PIN</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span><span class="pln">
  158. </span><span class="com">//else digitalWrite(B_LED_PIN,LOW);</span><span class="pln">
  159. </span><span class="pun">}</span>
<span class="com">/* Project 1: Double Transducer

   Description: This code detects a color signal with a photoresistor and RGB LEDs, transmits that signal across two inductors,
   and passes out a 'positive' signal by driving a servo arm to extend a linear motion device.

   Resources Referenced:
   https://www.arduino.cc/reference/en/
   http://www.physics-and-radio-electronics.com/electronic-devices-and-circuits/rectifier/bridgerectifier.html
   https://create.arduino.cc/projecthub/mjrobot/arduino-color-detection-57e4ce?ref=tag&ref_id=color-detector&offset=0
   https://www.instructables.com/id/How-to-Control-an-Electromagnet-with-an-Arduino/

   Outputs
   Arduino Pin | Output
   2              Red LED
   3              Green LED
   4              Blue LED
   6              Output inductor Pin
   7              Servo Pin

   Input
   Arduino Pin | Input
   A0            Photoresistor
   A5            Input inductor Pin
*/</span><span class="pln">

</span><span class="com">#include</span><span class="pln"> </span><span class="str"><Servo.h></span><span class="pln">

</span><span class="com">//Linear Motion</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SERVO_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. Servo control pin.</span><span class="pln">

</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> rPeriod </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1000</span><span class="pun">;</span><span class="pln"> </span><span class="com">//Parameter: refractory period for servo. Time for which a change to servo state cannot be changed again.</span><span class="pln">

</span><span class="typ">Servo</span><span class="pln"> arm</span><span class="pun">;</span><span class="pln"> </span><span class="com">//Servo 'object'</span><span class="pln">

</span><span class="kwd">bool</span><span class="pln"> moveConfirmed </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY: when true, arm extends at the end of the next refractory period, otherwise it'll retract</span><span class="pln">
</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> lastmove </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY: counter to determine time until refractory ends</span><span class="pln">

</span><span class="com">//magnetic domain</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> O_MAG_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. Controls transistor powering the inductor.</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> I_MAG_PIN </span><span class="pun">=</span><span class="pln"> A5</span><span class="pun">;</span><span class="pln"> </span><span class="com">//INPUT, analog. Reads the read inductor</span><span class="pln">

</span><span class="com">//magnetic domain: output</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> togThresh </span><span class="pun">=</span><span class="pln"> </span><span class="lit">300</span><span class="pun">;</span><span class="pln"> </span><span class="com">//PARAMETER, toggle output frequency in milliseconds. This determines how frequently the output inductor produces flux when it is permitted to do so.</span><span class="pln">

</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> lasttoggle </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, toggle: Time since magnet signal was last toggled on/off. Only when the magnet is toggled</span><span class="pln">
</span><span class="kwd">bool</span><span class="pln"> toggle </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, toggle: used to determine which toggle step, on or off.</span><span class="pln">

</span><span class="com">//magetic domain: input</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> thresh </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, minimum signal from the input pin from the inductor classified as a read</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> readTime </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, milliseconds taken by the entire read step</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> readFrq </span><span class="pun">=</span><span class="pln"> </span><span class="lit">20</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, number of times reads are taken in the</span><span class="pln">

</span><span class="com">//Color Sensor</span><span class="pln">
</span><span class="com">//(light detector)</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> PHOTO_PIN </span><span class="pun">=</span><span class="pln"> A0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//INPUT, analog. Photoresistor pin. Signal corresponds to light level.</span><span class="pln">
</span><span class="com">//(LED colors)</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> R_LED_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. RED LED pin, flashes first in series.</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> G_LED_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">3</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. GREEN LED pin, flashes second in series.</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> B_LED_PIN </span><span class="pun">=</span><span class="pln"> </span><span class="lit">4</span><span class="pun">;</span><span class="pln"> </span><span class="com">//OUTPUT, digital. BLUE LED pin, flashes last in series.</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> target</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="lit">300</span><span class="pun">,</span><span class="pln"> </span><span class="lit">460</span><span class="pun">,</span><span class="pln"> </span><span class="lit">230</span><span class="pun">};</span><span class="pln"> </span><span class="com">//PARAMETER for the RED  disk, must be calibrated based on lighting levels</span><span class="pln">
</span><span class="com">//NOTE: Pervious testing values: casing has a color of about (195, 380, 295). RED side has about (270, 400, 250)</span><span class="pln">

</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> tolerance </span><span class="pun">=</span><span class="pln"> </span><span class="lit">15</span><span class="pun">;</span><span class="pln"> </span><span class="com">//UTILITY, +/- range that color matches target</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> color</span><span class="pun">[</span><span class="lit">3</span><span class="pun">];</span><span class="pln"> </span><span class="com">//UTILITY, virtual RGB container variable representing the reflected color data</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> setup</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">// put your setup code here, to run once:</span><span class="pln">
  </span><span class="com">//For debugging and viewing the inductor receiving signal</span><span class="pln">
  </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">(</span><span class="lit">19200</span><span class="pun">);</span><span class="pln">

  </span><span class="com">//Linear Motion</span><span class="pln">
  </span><span class="com">//OUTPUT</span><span class="pln">
  arm</span><span class="pun">.</span><span class="pln">attach</span><span class="pun">(</span><span class="pln">SERVO_PIN</span><span class="pun">);</span><span class="pln">

  </span><span class="com">//Magnetic domain</span><span class="pln">
  </span><span class="com">//INPUT</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">I_MAG_PIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  </span><span class="com">//OUTPUT</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">O_MAG_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln"> </span><span class="com">//To transistor</span><span class="pln">


  </span><span class="com">//Color Sensor</span><span class="pln">
  </span><span class="com">//INPUT</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  </span><span class="com">//OUTPUT</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">R_LED_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">G_LED_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">B_LED_PIN</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">

  delay</span><span class="pun">(</span><span class="lit">1000</span><span class="pun">);</span><span class="pln"> </span><span class="com">//wait for the servo arm, as there may be cases where the signal is immediately present as soon as loop goes through</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">void</span><span class="pln"> loop</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">// put your main code here, to run repeatedly:</span><span class="pln">

  </span><span class="com">//Perform detection: check if RGB has changed enough to match some signal color</span><span class="pln">
  colorRead</span><span class="pun">(</span><span class="lit">100</span><span class="pun">);</span><span class="pln"> </span><span class="com">//Color detection; values stored in global array with a delay of 100 between each color.</span><span class="pln">

  </span><span class="com">//Debugging line: Color input</span><span class="pln">
  </span><span class="typ">Serial</span><span class="pun">.</span><span class="pln">println</span><span class="pun">((</span><span class="typ">String</span><span class="pun">)(</span><span class="pln">color</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">" "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="typ">String</span><span class="pun">)(</span><span class="pln">color</span><span class="pun">[</span><span class="lit">1</span><span class="pun">])</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">" "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="typ">String</span><span class="pun">)(</span><span class="pln">color</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]));</span><span class="pln">

  </span><span class="com">//if condition is met, power the inductor</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">inThreshold</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    driveInductor</span><span class="pun">();</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">//read the receivor inductor and confirm it has moved</span><span class="pln">
  moveConfirmed </span><span class="pun">=</span><span class="pln"> moveConfirmed </span><span class="pun">||</span><span class="pln"> readInductor</span><span class="pun">();</span><span class="pln">

  </span><span class="com">//Attempts to move servo arm. Only proceeds if the refractory period has passed</span><span class="pln">
  moveServoArm</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">//move servo arm out and in</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> moveServoArm</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">//whenever the refractory period has passed, the arm is allowed to move again</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">millis</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> lastmove </span><span class="pun">></span><span class="pln"> rPeriod</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">//moves arm when enough time has passed</span><span class="pln">
    arm</span><span class="pun">.</span><span class="pln">write</span><span class="pun">((</span><span class="pln">moveConfirmed</span><span class="pun">)</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> </span><span class="lit">60</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">5</span><span class="pun">);</span><span class="pln"> </span><span class="com">//If a move signal is measured at this step, move the arm out, otherwise pull it back in.</span><span class="pln">
    lastmove </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
    moveConfirmed </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">//charge and discharge inductor to produce a varying flux</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> driveInductor</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">//if time since last toggle exceeds the threshold, toggle the inductor drive pin</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">abs</span><span class="pun">(</span><span class="pln">millis</span><span class="pun">()</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> lasttoggle</span><span class="pun">)</span><span class="pln"> </span><span class="pun">></span><span class="pln"> togThresh</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    digitalWrite</span><span class="pun">(</span><span class="pln">O_MAG_PIN</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span class="pln">toggle</span><span class="pun">)</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> HIGH </span><span class="pun">:</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
    lasttoggle </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
    toggle </span><span class="pun">=</span><span class="pln"> </span><span class="pun">!</span><span class="pln">toggle</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="com">//indicates whether there is substantial voltage induced from changing flux in the read inductor</span><span class="pln">
</span><span class="kwd">bool</span><span class="pln"> readInductor</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">//records if the signal ever exceeds a threshold</span><span class="pln">
  </span><span class="kwd">bool</span><span class="pln"> successfulRead </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> readFrq</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">int</span><span class="pln"> fluxread </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">I_MAG_PIN</span><span class="pun">);</span><span class="pln">
    </span><span class="com">//Debugging: Flux measurement results</span><span class="pln">
    </span><span class="com">//Serial.println(fluxread);</span><span class="pln">

    </span><span class="com">//checks if signal exceeds a threshold.</span><span class="pln">
    successfulRead </span><span class="pun">=</span><span class="pln"> successfulRead </span><span class="pun">||</span><span class="pln"> fluxread </span><span class="pun">></span><span class="pln"> thresh</span><span class="pun">;</span><span class="pln">

    delay</span><span class="pun">(</span><span class="pln">readTime </span><span class="pun">/</span><span class="pln"> readFrq</span><span class="pun">);</span><span class="pln"> </span><span class="com">//divides the delay time into readFrq number of segments for a total of readTime worth of time</span><span class="pln">

  </span><span class="pun">}</span><span class="pln">
  </span><span class="kwd">return</span><span class="pln"> successfulRead</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">//Checks if light is within the threshold of being a target</span><span class="pln">
</span><span class="kwd">bool</span><span class="pln"> inThreshold</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="com">//loops for 1,2,3 or RGB to check all values are within tolerance threshold</span><span class="pln">
  </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> </span><span class="lit">3</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">abs</span><span class="pun">(</span><span class="pln">target</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> color</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])</span><span class="pln"> </span><span class="pun">></span><span class="pln"> tolerance</span><span class="pun">)</span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  </span><span class="com">//if for no color it returns false, then all colors are in tolerance</span><span class="pln">
  </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">//Checks color by flashing each RGB LED color for reflection from chamber and sets value in color array</span><span class="pln">
</span><span class="com">//frq variable used to control delay between reads, or the speed of the flashes.</span><span class="pln">
</span><span class="com">//In theory, a longer delay would result in higher accuracy but sacrifice responsiveness and speed</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> colorRead</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> frq</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln"> </span><span class="com">//reset to no LEDs</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span><span class="pln"> </span><span class="com">//RED LED ON</span><span class="pln">
  delay</span><span class="pun">(</span><span class="pln">frq</span><span class="pun">);</span><span class="pln">
  color</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">//read RED</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">2</span><span class="pun">);</span><span class="pln"> </span><span class="com">//GREEN LED ON</span><span class="pln">
  delay</span><span class="pun">(</span><span class="pln">frq</span><span class="pun">);</span><span class="pln">
  color</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">//read GREEN</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">3</span><span class="pun">);</span><span class="pln"> </span><span class="com">//BLUE LED ON</span><span class="pln">
  delay</span><span class="pun">(</span><span class="pln">frq</span><span class="pun">);</span><span class="pln">
  color</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTO_PIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">//read BLUE</span><span class="pln">
  setLED</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">//Sets the LEDs on/off based on a key value. A switch case might work even better, but this function is still perfectly functional.</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> setLED</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> key</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    digitalWrite</span><span class="pun">(</span><span class="pln">R_LED_PIN</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
    digitalWrite</span><span class="pun">(</span><span class="pln">G_LED_PIN</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
    digitalWrite</span><span class="pun">(</span><span class="pln">B_LED_PIN</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="pln">R_LED_PIN</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span><span class="pln">
  </span><span class="com">//else digitalWrite(R_LED_PIN,LOW);</span><span class="pln">
  </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="pln">G_LED_PIN</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span><span class="pln">
  </span><span class="com">//else digitalWrite(G_LED_PIN,LOW);</span><span class="pln">
  </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">key </span><span class="pun">==</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln">digitalWrite</span><span class="pun">(</span><span class="pln">B_LED_PIN</span><span class="pun">,</span><span class="pln"> HIGH</span><span class="pun">);</span><span class="pln">
  </span><span class="com">//else digitalWrite(B_LED_PIN,LOW);</span><span class="pln">
</span><span class="pun">}</span>