Brief Explanation

A physical reminder with customizable timing options ranging from 28 seconds to 24 hours.

Images

 

top of the reminder which displays the current timer setting

The switch on top activates a sound alarm if it is turned on. The reminder uses a 9V battery to power it.

(usage example – reminding to drink water)

Place next to object you want to be reminded about, and set the timer to the desired reminder frequency.

Once the timer runs out, the alarm will sound (quiet mode uses LEDs as shown in picture but you can have it use sound as well – plays a little song instead of lighting up). After removing and returning the water bottle from in front of the sensors, the timer resets.

If the water bottle is removed before the timer runs out and the alarm goes off, the timer is reset.

Process Images and Review

I began by attempting to use HC-06 Bluetooth modules to be able to communicate between a sensor and the actual “reminder” which would sound the alarm. That way the sensor could be a few feet from you but the alarm would go off right next to you. Tried switching to the nRF24L01 but the devices wouldn’t connect with each other

Tried to use the Arduino nano for the reminder but couldn’t get it to work reliably so I decided to use the regular Arduino. I was trying to create a more compact design, but since this is the first iteration, I thought I should focus more on the functionality

This was the separate sensor portion from when I was attempting to use the Bluetooth chips to communicate between two Arduinos

Got rid of the bluetooth chips and made it compact by using only one Arduino. Although not my original design, I thought the bluetooth chips would be too finicky and unstable, so I decided to eliminate them entirely.

I added an LCD screen that shows the number of hours/minutes/seconds that the timer is currently set to. This was the final prototype before I started soldering and creating the housing.

Everything all soldered up

Discussion

Quotes from crit:

  • “I like that there are a lot of parts that you can play with on the box and it looks nice”
  • “Good form factor and clean presentation.”
  • “I like how you have the knobs and the smoky arcyllic; I also like the quiet and sound setting.”
  • “I like the series of potentiometers that can be used to customize the interaction. Also, the box is a nice form factor besides the battery being outside.”

Reflection:

I think that people enjoyed the aesthetic of the arctic frost acrylic housing around the circuitry for this project. It created a slight techy view into the inner workings of the box, while also making the LED usage inside the box look really cool. I think that one of the components that I should have worried about was the battery. I wanted this project to not be constricted by having to be plugged in but didn’t think far enough ahead, in terms of housing, for the battery’s location.

As for my own goals that I set for myself regarding the final outcome of this project, I feel good about my final product, even though I encountered a few hiccups along the way. I wanted to use the Bluetooth chips because they would allow some proximity sensor to detect the lack of motion then send that data to another chip that was in my pocket, so I would be able to receive the alarm even if I was in the other room.

I got a bit fixated on the usage of the Bluetooth in my project when I could have been creating something that would do essentially the same things I wanted it to do without Bluetooth with more simplicity and with fewer errors. After seeing all of the other projects, I really think that my project and Nathan’s project (see next paragraph) could be brought together and used to create a really effective reminder. The adjustable timing from my project would have been great with his box. If each slot had a timer for when you’d have to have it back in the box by, the box could remind you to either take things out of the box and/or remind you to put things back in the box. This double functionality could be used to help remind you of things to remember to grab in the morning and for reminding you to replace your things at night.

Nathan’s project – a box with compartments and it the alarm would go off if there was something missing from its area. He used this to organize all of his jewelry because he has quite a bit. Most were rings and bracelets, so I can see how it would be easy to lose them.

Schematic

 

 

Code

  1. <span class="com">/*
  2. Eric Mendez
  3. emendez
  4. Project 2: "The Reminder"
  5. Description: Detects the lack of motion of an object for
  6. programmable time intervals to help remind the user to do
  7. a certain task at each of those time intervals.
  8. Inputs: switch(quiet mode), ultrasonic ranger(distance measuring),
  9. 2 potentiometers (hour and min)
  10. Outputs: speaker, LED strip, LCD screen
  11. */</span><span class="pln">
  12. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><Wire.h></span><span class="pln">
  13. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><LiquidCrystal_I2C.h></span><span class="pln">
  14. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><NewPing.h></span><span class="pln">
  15. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><TimerFreeTone.h></span><span class="pln">
  16. </span><span class="com">#include</span><span class="pln"> </span><span class="str"><PololuLedStrip.h></span><span class="pln">
  17. </span><span class="com">// set up devices and set const variables</span><span class="pln">
  18. </span><span class="typ">LiquidCrystal_I2C</span><span class="pln"> lcd</span><span class="pun">(</span><span class="lit">0x3F</span><span class="pun">,</span><span class="pln"> </span><span class="lit">16</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span><span class="pln">
  19. </span><span class="com">#define</span><span class="pln"> TRIGGER_PIN </span><span class="lit">9</span><span class="pln"> </span><span class="com">// Arduino pin tied to trigger pin on the ultrasonic sensor.</span><span class="pln">
  20. </span><span class="com">#define</span><span class="pln"> ECHO_PIN </span><span class="lit">8</span><span class="pln"> </span><span class="com">// Arduino pin tied to echo pin on the ultrasonic sensor.</span><span class="pln">
  21. </span><span class="com">#define</span><span class="pln"> MAX_DISTANCE </span><span class="lit">200</span><span class="pln"> </span><span class="com">// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.</span><span class="pln">
  22. </span><span class="typ">NewPing</span><span class="pln"> sonar</span><span class="pun">(</span><span class="pln">TRIGGER_PIN</span><span class="pun">,</span><span class="pln"> ECHO_PIN</span><span class="pun">,</span><span class="pln"> MAX_DISTANCE</span><span class="pun">);</span><span class="pln"> </span><span class="com">// NewPing setup of pins and maximum distance.</span><span class="pln">
  23. </span><span class="com">// Create an ledStrip object and specify the pin it will use.</span><span class="pln">
  24. </span><span class="typ">PololuLedStrip</span><span class="pun"><</span><span class="lit">6</span><span class="pun">></span><span class="pln"> ledStrip</span><span class="pun">;</span><span class="pln">
  25. </span><span class="com">// Create a buffer for holding the colors (3 bytes per color).</span><span class="pln">
  26. </span><span class="com">#define</span><span class="pln"> LED_COUNT </span><span class="lit">25</span><span class="pln">
  27. rgb_color colors</span><span class="pun">[</span><span class="pln">LED_COUNT</span><span class="pun">];</span><span class="pln">
  28. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> QUIET_SWITCH </span><span class="pun">=</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln">
  29. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln">
  30. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SPEAKER </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pun">;</span><span class="pln">
  31. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> POT_PIN_HOUR </span><span class="pun">=</span><span class="pln"> A1</span><span class="pun">;</span><span class="pln">
  32. </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> POT_PIN_MIN </span><span class="pun">=</span><span class="pln"> A2</span><span class="pun">;</span><span class="pln">
  33. </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"> SCALE_POT </span><span class="pun">=</span><span class="pln"> </span><span class="lit">86400000</span><span class="pun">;</span><span class="pln">
  34. </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"> HOUR_MS </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">*(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">*(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">1000</span><span class="pun">;</span><span class="pln">
  35. </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"> MIN_MS </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">*(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">1000</span><span class="pun">;</span><span class="pln">
  36. </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"> SEC_MS </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">1000</span><span class="pun">;</span><span class="pln">
  37. </span><span class="com">// set up global changable variables</span><span class="pln">
  38. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> reset_threshold </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  39. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> run_time </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  40. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> prev_time </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  41. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> led_start_time </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  42. </span><span class="kwd">bool</span><span class="pln"> led_on </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  43. </span><span class="kwd">int</span><span class="pln"> quietMode </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  44. </span><span class="kwd">int</span><span class="pln"> sonar_zeros_in_row </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  45. </span><span class="com">// note intervals for song</span><span class="pln">
  46. </span><span class="kwd">int</span><span class="pln"> a3 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">220</span><span class="pun">;</span><span class="pln">
  47. </span><span class="kwd">int</span><span class="pln"> </span><span class="typ">Asharp3</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">233</span><span class="pun">;</span><span class="pln">
  48. </span><span class="kwd">int</span><span class="pln"> b3 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">246</span><span class="pun">;</span><span class="pln">
  49. </span><span class="kwd">int</span><span class="pln"> c4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">261</span><span class="pun">;</span><span class="pln">
  50. </span><span class="kwd">int</span><span class="pln"> csharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">277</span><span class="pun">;</span><span class="pln">
  51. </span><span class="kwd">int</span><span class="pln"> d4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">293</span><span class="pun">;</span><span class="pln">
  52. </span><span class="kwd">int</span><span class="pln"> dsharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">311</span><span class="pun">;</span><span class="pln">
  53. </span><span class="kwd">int</span><span class="pln"> e4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">329</span><span class="pun">;</span><span class="pln">
  54. </span><span class="kwd">int</span><span class="pln"> f4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">349</span><span class="pun">;</span><span class="pln">
  55. </span><span class="kwd">int</span><span class="pln"> fsharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">369</span><span class="pun">;</span><span class="pln">
  56. </span><span class="kwd">int</span><span class="pln"> g4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">392</span><span class="pun">;</span><span class="pln">
  57. </span><span class="kwd">int</span><span class="pln"> gsharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">415</span><span class="pun">;</span><span class="pln">
  58. </span><span class="kwd">int</span><span class="pln"> a4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">440</span><span class="pun">;</span><span class="pln">
  59. </span><span class="kwd">int</span><span class="pln"> asharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">466</span><span class="pun">;</span><span class="pln">
  60. </span><span class="kwd">int</span><span class="pln"> b4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">493</span><span class="pun">;</span><span class="pln">
  61. </span><span class="kwd">int</span><span class="pln"> c5 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">523</span><span class="pun">;</span><span class="pln">
  62. </span><span class="kwd">int</span><span class="pln"> eighth </span><span class="pun">=</span><span class="pln"> </span><span class="lit">125</span><span class="pun">;</span><span class="pln">
  63. </span><span class="kwd">int</span><span class="pln"> quarter </span><span class="pun">=</span><span class="pln"> </span><span class="lit">250</span><span class="pun">;</span><span class="pln">
  64. </span><span class="kwd">int</span><span class="pln"> half </span><span class="pun">=</span><span class="pln"> </span><span class="lit">500</span><span class="pun">;</span><span class="pln">
  65. </span><span class="kwd">int</span><span class="pln"> melody</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> c5</span><span class="pun">,</span><span class="pln"> b4</span><span class="pun">,</span><span class="pln"> a4</span><span class="pun">,</span><span class="pln"> g4</span><span class="pun">,</span><span class="pln"> g4</span><span class="pun">,</span><span class="pln"> a4 </span><span class="pun">};</span><span class="pln">
  66. </span><span class="kwd">int</span><span class="pln"> duration</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{(</span><span class="pln">quarter </span><span class="pun">+</span><span class="pln"> half</span><span class="pun">),</span><span class="pln"> quarter</span><span class="pun">,</span><span class="pln"> half</span><span class="pun">,</span><span class="pln"> half</span><span class="pun">,</span><span class="pln">
  67. </span><span class="pun">(</span><span class="pln">half </span><span class="pun">+</span><span class="pln"> eighth</span><span class="pun">),</span><span class="pln"> </span><span class="pun">(</span><span class="pln">half </span><span class="pun">+</span><span class="pln"> half</span><span class="pun">)</span><span class="pln">
  68. </span><span class="pun">};</span><span class="pln">
  69. </span><span class="kwd">void</span><span class="pln"> setup</span><span class="pun">()</span><span class="pln">
  70. </span><span class="pun">{</span><span class="pln">
  71. </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">(</span><span class="lit">9600</span><span class="pun">);</span><span class="pln">
  72. </span><span class="com">// start LCD screen</span><span class="pln">
  73. lcd</span><span class="pun">.</span><span class="pln">init</span><span class="pun">();</span><span class="pln">
  74. lcd</span><span class="pun">.</span><span class="pln">backlight</span><span class="pun">();</span><span class="pln">
  75. lcd</span><span class="pun">.</span><span class="pln">home</span><span class="pun">();</span><span class="pln">
  76. </span><span class="com">// set modes for all output/inputs</span><span class="pln">
  77. pinMode</span><span class="pun">(</span><span class="pln">QUIET_SWITCH</span><span class="pun">,</span><span class="pln"> INPUT_PULLUP</span><span class="pun">);</span><span class="pln">
  78. pinMode</span><span class="pun">(</span><span class="pln">LED</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  79. pinMode</span><span class="pun">(</span><span class="pln">SPEAKER</span><span class="pun">,</span><span class="pln"> OUTPUT</span><span class="pun">);</span><span class="pln">
  80. pinMode</span><span class="pun">(</span><span class="pln">POT_PIN_HOUR</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  81. pinMode</span><span class="pun">(</span><span class="pln">POT_PIN_MIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  82. </span><span class="com">// inital state of speaker</span><span class="pln">
  83. digitalWrite</span><span class="pun">(</span><span class="pln">SPEAKER</span><span class="pun">,</span><span class="pln"> LOW</span><span class="pun">);</span><span class="pln">
  84. </span><span class="pun">}</span><span class="pln">
  85. </span><span class="kwd">void</span><span class="pln"> loop</span><span class="pun">()</span><span class="pln">
  86. </span><span class="pun">{</span><span class="pln">
  87. </span><span class="kwd">char</span><span class="pln"> data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// state</span><span class="pln">
  88. </span><span class="kwd">int</span><span class="pln"> distance </span><span class="pun">=</span><span class="pln"> sonar</span><span class="pun">.</span><span class="pln">ping_cm</span><span class="pun">();</span><span class="pln"> </span><span class="com">// ultrasonic ranger</span><span class="pln">
  89. </span><span class="kwd">int</span><span class="pln"> deltaX </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">// distnance between sonar and object</span><span class="pln">
  90. </span><span class="kwd">int</span><span class="pln"> potVal_hour </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">POT_PIN_HOUR</span><span class="pun">);</span><span class="pln"> </span><span class="com">// knob for hour increment</span><span class="pln">
  91. </span><span class="kwd">int</span><span class="pln"> potVal_min </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">POT_PIN_MIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">// knob for min increment</span><span class="pln">
  92. </span><span class="kwd">bool</span><span class="pln"> movement </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">// if object was moved -> true (deltaX > detection_zone)</span><span class="pln">
  93. </span><span class="com">// map values from potentiometers to hour/min/sec scale - range = [14sec, 24 hours]</span><span class="pln">
  94. reset_threshold </span><span class="pun">=</span><span class="pln"> calculate_threshold</span><span class="pun">(</span><span class="pln">potVal_hour</span><span class="pun">,</span><span class="pln"> potVal_min</span><span class="pun">);</span><span class="pln">
  95. </span><span class="com">// translate potVal to ms scale (1 - reset_threshold)</span><span class="pln">
  96. lcd</span><span class="pun">.</span><span class="pln">clear</span><span class="pun">();</span><span class="pln">
  97. lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"hour: "</span><span class="pun">);</span><span class="pln">
  98. lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">reset_threshold </span><span class="pun">/</span><span class="pln"> HOUR_MS</span><span class="pun">);</span><span class="pln">
  99. lcd</span><span class="pun">.</span><span class="pln">setCursor</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
  100. lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"min: "</span><span class="pun">);</span><span class="pln">
  101. lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">((</span><span class="pln">reset_threshold </span><span class="pun">/</span><span class="pln"> MIN_MS</span><span class="pun">)</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">);</span><span class="pln">
  102. lcd</span><span class="pun">.</span><span class="pln">setCursor</span><span class="pun">(</span><span class="lit">9</span><span class="pun">,</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
  103. lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"sec: "</span><span class="pun">);</span><span class="pln">
  104. lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">((</span><span class="pln">reset_threshold </span><span class="pun">/</span><span class="pln"> SEC_MS</span><span class="pun">)</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">);</span><span class="pln">
  105. run_time </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">
  106. </span><span class="com">// Quiet mode detection</span><span class="pln">
  107. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">digitalRead</span><span class="pun">(</span><span class="pln">QUIET_SWITCH</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"> </span><span class="pun">{</span><span class="pln">
  108. </span><span class="com">// mode - true (LEDs)</span><span class="pln">
  109. quietMode </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln">
  110. </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  111. </span><span class="com">// mode - false (Sound)</span><span class="pln">
  112. quietMode </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  113. </span><span class="pun">}</span><span class="pln">
  114. </span><span class="com">// Sonar distance detection (lack of motion detection)</span><span class="pln">
  115. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">distance </span><span class="pun">></span><span class="pln"> deltaX</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  116. sonar_zeros_in_row </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln"> </span><span class="com">// smoothing fn</span><span class="pln">
  117. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">sonar_zeros_in_row </span><span class="pun">==</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">// 3 in a row to avoid false positives</span><span class="pln">
  118. </span><span class="com">// object was moved for sure</span><span class="pln">
  119. movement </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
  120. data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'1'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// change state</span><span class="pln">
  121. sonar_zeros_in_row </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">// reset this smoothing variable</span><span class="pln">
  122. </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  123. </span><span class="com">// stay in the same state of '0'</span><span class="pln">
  124. data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">;</span><span class="pln">
  125. </span><span class="pun">}</span><span class="pln">
  126. </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  127. </span><span class="com">// distance stays under 5</span><span class="pln">
  128. movement </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  129. sonar_zeros_in_row </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
  130. data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// stay in the same state of '0'</span><span class="pln">
  131. </span><span class="pun">}</span><span class="pln">
  132. </span><span class="com">// object moved - set state</span><span class="pln">
  133. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">movement </span><span class="pun">==</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">
  134. data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'2'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// state of object has moved</span><span class="pln">
  135. </span><span class="pun">}</span><span class="pln">
  136. </span><span class="com">// object has been unmoved for too long</span><span class="pln">
  137. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">((</span><span class="pln">run_time </span><span class="pun">-</span><span class="pln"> prev_time</span><span class="pun">)</span><span class="pln"> </span><span class="pun">>=</span><span class="pln"> reset_threshold</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  138. prev_time </span><span class="pun">=</span><span class="pln"> run_time</span><span class="pun">;</span><span class="pln"> </span><span class="com">// reset timers</span><span class="pln">
  139. data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'1'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// change state to '1' - sound the alarm</span><span class="pln">
  140. </span><span class="pun">}</span><span class="pln">
  141. </span><span class="com">// reset led after 5 seconds</span><span class="pln">
  142. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">led_on </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">true</span><span class="pln"> </span><span class="pun">&&</span><span class="pln"> </span><span class="pun">(</span><span class="pln">run_time </span><span class="pun">-</span><span class="pln"> led_start_time</span><span class="pun">)</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="lit">5000</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  143. led_on </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  144. </span><span class="com">// turn off LEDs</span><span class="pln">
  145. </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"> LED_COUNT</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln">
  146. </span><span class="pun">{</span><span class="pln">
  147. colors</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"> rgb_color</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">);</span><span class="pln">
  148. </span><span class="pun">}</span><span class="pln">
  149. ledStrip</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">colors</span><span class="pun">,</span><span class="pln"> LED_COUNT</span><span class="pun">);</span><span class="pln">
  150. </span><span class="pun">}</span><span class="pln">
  151. </span><span class="com">// STATE '1'</span><span class="pln">
  152. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">data </span><span class="pun">==</span><span class="pln"> </span><span class="str">'1'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  153. </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">quietMode </span><span class="pun">==</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  154. </span><span class="com">// quiet alarm</span><span class="pln">
  155. led_start_time </span><span class="pun">=</span><span class="pln"> run_time</span><span class="pun">;</span><span class="pln">
  156. led_on </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
  157. </span><span class="com">// upload rainbow color based on time to LEDs</span><span class="pln">
  158. </span><span class="kwd">byte</span><span class="pln"> time </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"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
  159. </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="typ">uint16_t</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"> LED_COUNT</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln">
  160. </span><span class="pun">{</span><span class="pln">
  161. </span><span class="kwd">byte</span><span class="pln"> x </span><span class="pun">=</span><span class="pln"> time </span><span class="pun">-</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
  162. colors</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"> rgb_color</span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> </span><span class="lit">255</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> x</span><span class="pun">,</span><span class="pln"> x</span><span class="pun">);</span><span class="pln">
  163. </span><span class="pun">}</span><span class="pln">
  164. </span><span class="com">// Write the colors to the LED strip.</span><span class="pln">
  165. ledStrip</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">colors</span><span class="pun">,</span><span class="pln"> LED_COUNT</span><span class="pun">);</span><span class="pln">
  166. delay</span><span class="pun">(</span><span class="lit">10</span><span class="pun">);</span><span class="pln">
  167. </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  168. </span><span class="com">// play sound alarm</span><span class="pln">
  169. </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> thisNote </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> thisNote </span><span class="pun"><</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln"> thisNote</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  170. </span><span class="com">// Loop through the notes in the array.</span><span class="pln">
  171. </span><span class="typ">TimerFreeTone</span><span class="pun">(</span><span class="pln">SPEAKER</span><span class="pun">,</span><span class="pln"> melody</span><span class="pun">[</span><span class="pln">thisNote</span><span class="pun">],</span><span class="pln"> duration</span><span class="pun">[</span><span class="pln">thisNote</span><span class="pun">]);</span><span class="pln">
  172. </span><span class="com">// Play thisNote for its coresponding duration.</span><span class="pln">
  173. delay</span><span class="pun">(</span><span class="lit">50</span><span class="pun">);</span><span class="pln"> </span><span class="com">// Short delay between notes.</span><span class="pln">
  174. </span><span class="pun">}</span><span class="pln">
  175. </span><span class="pun">}</span><span class="pln">
  176. </span><span class="com">// STATE '2' - reset reminder</span><span class="pln">
  177. </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">data </span><span class="pun">==</span><span class="pln"> </span><span class="str">'2'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  178. prev_time </span><span class="pun">=</span><span class="pln"> run_time</span><span class="pun">;</span><span class="pln">
  179. </span><span class="com">// STATE '0' - do nothing</span><span class="pln">
  180. </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">data </span><span class="pun">==</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  181. </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"waiting"</span><span class="pun">);</span><span class="pln">
  182. </span><span class="pun">}</span><span class="pln">
  183. </span><span class="typ">Serial</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">""</span><span class="pun">);</span><span class="pln">
  184. </span><span class="pun">}</span><span class="pln">
  185. </span><span class="com">/* Usage: Function used to scale potentiometer values (2 of them - one for
  186. * hours and one for min) to a scale of [14sec, 24 hours] as evenly as
  187. * possible
  188. * map potvals (0-1023)
  189. */</span><span class="pln">
  190. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> calculate_threshold</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> potVal_hour</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> potVal_min</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  191. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> one_percent_hour </span><span class="pun">=</span><span class="pln"> SCALE_POT </span><span class="pun">/</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">
  192. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> one_percent_min </span><span class="pun">=</span><span class="pln"> SCALE_POT </span><span class="pun">/</span><span class="pln"> </span><span class="pun">(</span><span class="lit">100</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">60</span><span class="pun">);</span><span class="pln">
  193. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> result_hour</span><span class="pun">;</span><span class="pln">
  194. </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> result_min</span><span class="pun">;</span><span class="pln">
  195. </span><span class="kwd">float</span><span class="pln"> hour_percent </span><span class="pun">=</span><span class="pln"> </span><span class="pun">((</span><span class="kwd">float</span><span class="pun">)</span><span class="pln">potVal_hour </span><span class="pun">/</span><span class="pln"> </span><span class="lit">1023.0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">
  196. </span><span class="kwd">float</span><span class="pln"> min_percent </span><span class="pun">=</span><span class="pln"> </span><span class="pun">((</span><span class="kwd">float</span><span class="pun">)</span><span class="pln">potVal_min </span><span class="pun">/</span><span class="pln"> </span><span class="lit">409.0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">
  197. result_hour </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="pln">hour_percent </span><span class="pun">*</span><span class="pln"> one_percent_hour</span><span class="pun">;</span><span class="pln">
  198. result_min </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="pln">min_percent </span><span class="pun">*</span><span class="pln"> one_percent_min</span><span class="pun">;</span><span class="pln">
  199. </span><span class="kwd">return</span><span class="pln"> result_hour </span><span class="pun">+</span><span class="pln"> result_min</span><span class="pun">;</span><span class="pln">
  200. </span><span class="pun">}</span>
<span class="com">/*
   Eric Mendez
   emendez
   Project 2: "The Reminder"

   Description: Detects the lack of motion of an object for
   programmable time intervals to help remind the user to do 
   a certain task at each of those time intervals.

   Inputs: switch(quiet mode), ultrasonic ranger(distance measuring),
           2 potentiometers (hour and min)
   Outputs: speaker, LED strip, LCD screen
*/</span><span class="pln">

</span><span class="com">#include</span><span class="pln"> </span><span class="str"><Wire.h></span><span class="pln">
</span><span class="com">#include</span><span class="pln"> </span><span class="str"><LiquidCrystal_I2C.h></span><span class="pln">
</span><span class="com">#include</span><span class="pln"> </span><span class="str"><NewPing.h></span><span class="pln">
</span><span class="com">#include</span><span class="pln"> </span><span class="str"><TimerFreeTone.h></span><span class="pln">
</span><span class="com">#include</span><span class="pln"> </span><span class="str"><PololuLedStrip.h></span><span class="pln">

</span><span class="com">// set up devices and set const variables</span><span class="pln">
</span><span class="typ">LiquidCrystal_I2C</span><span class="pln"> lcd</span><span class="pun">(</span><span class="lit">0x3F</span><span class="pun">,</span><span class="pln"> </span><span class="lit">16</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">#define</span><span class="pln"> TRIGGER_PIN  </span><span class="lit">9</span><span class="pln">  </span><span class="com">// Arduino pin tied to trigger pin on the ultrasonic sensor.</span><span class="pln">
</span><span class="com">#define</span><span class="pln"> ECHO_PIN     </span><span class="lit">8</span><span class="pln">  </span><span class="com">// Arduino pin tied to echo pin on the ultrasonic sensor.</span><span class="pln">
</span><span class="com">#define</span><span class="pln"> MAX_DISTANCE </span><span class="lit">200</span><span class="pln"> </span><span class="com">// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.</span><span class="pln">

</span><span class="typ">NewPing</span><span class="pln"> sonar</span><span class="pun">(</span><span class="pln">TRIGGER_PIN</span><span class="pun">,</span><span class="pln"> ECHO_PIN</span><span class="pun">,</span><span class="pln"> MAX_DISTANCE</span><span class="pun">);</span><span class="pln"> </span><span class="com">// NewPing setup of pins and maximum distance.</span><span class="pln">

</span><span class="com">// Create an ledStrip object and specify the pin it will use.</span><span class="pln">
</span><span class="typ">PololuLedStrip</span><span class="pun"><</span><span class="lit">6</span><span class="pun">></span><span class="pln"> ledStrip</span><span class="pun">;</span><span class="pln">

</span><span class="com">// Create a buffer for holding the colors (3 bytes per color).</span><span class="pln">
</span><span class="com">#define</span><span class="pln"> LED_COUNT </span><span class="lit">25</span><span class="pln">
rgb_color colors</span><span class="pun">[</span><span class="pln">LED_COUNT</span><span class="pun">];</span><span class="pln">


</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> QUIET_SWITCH </span><span class="pun">=</span><span class="pln"> </span><span class="lit">7</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> LED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> SPEAKER </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> POT_PIN_HOUR </span><span class="pun">=</span><span class="pln"> A1</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> POT_PIN_MIN </span><span class="pun">=</span><span class="pln"> A2</span><span class="pun">;</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"> SCALE_POT </span><span class="pun">=</span><span class="pln"> </span><span class="lit">86400000</span><span class="pun">;</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"> HOUR_MS </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">*(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">*(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">1000</span><span class="pun">;</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"> MIN_MS </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">*(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">1000</span><span class="pun">;</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"> SEC_MS </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">1000</span><span class="pun">;</span><span class="pln">

</span><span class="com">// set up global changable variables</span><span class="pln">
</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> reset_threshold </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> run_time </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> prev_time </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> led_start_time </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">bool</span><span class="pln"> led_on </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">int</span><span class="pln"> quietMode </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> sonar_zeros_in_row </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">// note intervals for song</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> a3 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">220</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> </span><span class="typ">Asharp3</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">233</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> b3 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">246</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> c4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">261</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> csharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">277</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> d4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">293</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> dsharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">311</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> e4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">329</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> f4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">349</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> fsharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">369</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> g4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">392</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> gsharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">415</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> a4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">440</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> asharp4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">466</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> b4 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">493</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> c5 </span><span class="pun">=</span><span class="pln"> </span><span class="lit">523</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> eighth </span><span class="pun">=</span><span class="pln"> </span><span class="lit">125</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> quarter </span><span class="pun">=</span><span class="pln"> </span><span class="lit">250</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> half </span><span class="pun">=</span><span class="pln"> </span><span class="lit">500</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> melody</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> c5</span><span class="pun">,</span><span class="pln"> b4</span><span class="pun">,</span><span class="pln"> a4</span><span class="pun">,</span><span class="pln"> g4</span><span class="pun">,</span><span class="pln"> g4</span><span class="pun">,</span><span class="pln"> a4 </span><span class="pun">};</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> duration</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{(</span><span class="pln">quarter </span><span class="pun">+</span><span class="pln"> half</span><span class="pun">),</span><span class="pln"> quarter</span><span class="pun">,</span><span class="pln"> half</span><span class="pun">,</span><span class="pln"> half</span><span class="pun">,</span><span class="pln">
                  </span><span class="pun">(</span><span class="pln">half </span><span class="pun">+</span><span class="pln"> eighth</span><span class="pun">),</span><span class="pln"> </span><span class="pun">(</span><span class="pln">half </span><span class="pun">+</span><span class="pln"> half</span><span class="pun">)</span><span class="pln">
                 </span><span class="pun">};</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="typ">Serial</span><span class="pun">.</span><span class="kwd">begin</span><span class="pun">(</span><span class="lit">9600</span><span class="pun">);</span><span class="pln">

  </span><span class="com">// start LCD screen</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="pln">init</span><span class="pun">();</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="pln">backlight</span><span class="pun">();</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="pln">home</span><span class="pun">();</span><span class="pln">

  </span><span class="com">// set modes for all output/inputs</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">QUIET_SWITCH</span><span class="pun">,</span><span class="pln"> INPUT_PULLUP</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">LED</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">SPEAKER</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">POT_PIN_HOUR</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">
  pinMode</span><span class="pun">(</span><span class="pln">POT_PIN_MIN</span><span class="pun">,</span><span class="pln"> INPUT</span><span class="pun">);</span><span class="pln">

  </span><span class="com">// inital state of speaker</span><span class="pln">
  digitalWrite</span><span class="pun">(</span><span class="pln">SPEAKER</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">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="kwd">char</span><span class="pln"> data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// state</span><span class="pln">
  </span><span class="kwd">int</span><span class="pln"> distance </span><span class="pun">=</span><span class="pln"> sonar</span><span class="pun">.</span><span class="pln">ping_cm</span><span class="pun">();</span><span class="pln"> </span><span class="com">// ultrasonic ranger</span><span class="pln">
  </span><span class="kwd">int</span><span class="pln"> deltaX </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">// distnance between sonar and object</span><span class="pln">
  </span><span class="kwd">int</span><span class="pln"> potVal_hour </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">POT_PIN_HOUR</span><span class="pun">);</span><span class="pln"> </span><span class="com">// knob for hour increment</span><span class="pln">
  </span><span class="kwd">int</span><span class="pln"> potVal_min </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">POT_PIN_MIN</span><span class="pun">);</span><span class="pln"> </span><span class="com">// knob for min increment</span><span class="pln">
  </span><span class="kwd">bool</span><span class="pln"> movement </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">// if object was moved -> true (deltaX > detection_zone)</span><span class="pln">

  </span><span class="com">// map values from potentiometers to hour/min/sec scale - range = [14sec, 24 hours]</span><span class="pln">
  reset_threshold </span><span class="pun">=</span><span class="pln"> calculate_threshold</span><span class="pun">(</span><span class="pln">potVal_hour</span><span class="pun">,</span><span class="pln"> potVal_min</span><span class="pun">);</span><span class="pln">
  
  </span><span class="com">// translate potVal to ms scale (1 - reset_threshold)</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="pln">clear</span><span class="pun">();</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"hour: "</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">reset_threshold </span><span class="pun">/</span><span class="pln"> HOUR_MS</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="pln">setCursor</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"min: "</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">((</span><span class="pln">reset_threshold </span><span class="pun">/</span><span class="pln"> MIN_MS</span><span class="pun">)</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="pln">setCursor</span><span class="pun">(</span><span class="lit">9</span><span class="pun">,</span><span class="lit">1</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"sec: "</span><span class="pun">);</span><span class="pln">
  lcd</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">((</span><span class="pln">reset_threshold </span><span class="pun">/</span><span class="pln"> SEC_MS</span><span class="pun">)</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="lit">60</span><span class="pun">);</span><span class="pln">

  run_time </span><span class="pun">=</span><span class="pln"> millis</span><span class="pun">();</span><span class="pln">

  </span><span class="com">// Quiet mode detection</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">digitalRead</span><span class="pun">(</span><span class="pln">QUIET_SWITCH</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"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// mode - true (LEDs)</span><span class="pln">
    quietMode </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</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="pun">{</span><span class="pln">
    </span><span class="com">// mode - false (Sound)</span><span class="pln">
    quietMode </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">

  </span><span class="com">// Sonar distance detection (lack of motion detection)</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">distance </span><span class="pun">></span><span class="pln"> deltaX</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    sonar_zeros_in_row </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln"> </span><span class="com">// smoothing fn</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">sonar_zeros_in_row </span><span class="pun">==</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">// 3 in a row to avoid false positives</span><span class="pln">
      </span><span class="com">// object was moved for sure</span><span class="pln">
      movement </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
      data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'1'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// change state</span><span class="pln">
      sonar_zeros_in_row </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">// reset this smoothing variable</span><span class="pln">
    </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="com">// stay in the same state of '0'</span><span class="pln">
      data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'0'</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="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="com">// distance stays under 5</span><span class="pln">
    movement </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
    sonar_zeros_in_row </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
    data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// stay in the same state of '0'</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// object moved - set state</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">movement </span><span class="pun">==</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">
    data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'2'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// state of object has moved</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
  
  </span><span class="com">// object has been unmoved for too long</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">((</span><span class="pln">run_time </span><span class="pun">-</span><span class="pln"> prev_time</span><span class="pun">)</span><span class="pln"> </span><span class="pun">>=</span><span class="pln"> reset_threshold</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    prev_time </span><span class="pun">=</span><span class="pln"> run_time</span><span class="pun">;</span><span class="pln"> </span><span class="com">// reset timers</span><span class="pln">
    data </span><span class="pun">=</span><span class="pln"> </span><span class="str">'1'</span><span class="pun">;</span><span class="pln"> </span><span class="com">// change state to '1' - sound the alarm</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// reset led after 5 seconds</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">led_on </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">true</span><span class="pln"> </span><span class="pun">&&</span><span class="pln"> </span><span class="pun">(</span><span class="pln">run_time </span><span class="pun">-</span><span class="pln"> led_start_time</span><span class="pun">)</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="lit">5000</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    led_on </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">// turn off LEDs</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"> LED_COUNT</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">
      colors</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"> rgb_color</span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</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">
    ledStrip</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">colors</span><span class="pun">,</span><span class="pln"> LED_COUNT</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">

  </span><span class="com">// STATE '1'</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">data </span><span class="pun">==</span><span class="pln"> </span><span class="str">'1'</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">quietMode </span><span class="pun">==</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="com">// quiet alarm</span><span class="pln">
      led_start_time </span><span class="pun">=</span><span class="pln"> run_time</span><span class="pun">;</span><span class="pln">
      led_on </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">// upload rainbow color based on time to LEDs</span><span class="pln">
      </span><span class="kwd">byte</span><span class="pln"> time </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"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
      </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="typ">uint16_t</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"> LED_COUNT</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">byte</span><span class="pln"> x </span><span class="pun">=</span><span class="pln"> time </span><span class="pun">-</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
        colors</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"> rgb_color</span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> </span><span class="lit">255</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> x</span><span class="pun">,</span><span class="pln"> x</span><span class="pun">);</span><span class="pln">
      </span><span class="pun">}</span><span class="pln">

      </span><span class="com">// Write the colors to the LED strip.</span><span class="pln">
      ledStrip</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln">colors</span><span class="pun">,</span><span class="pln"> LED_COUNT</span><span class="pun">);</span><span class="pln">
      delay</span><span class="pun">(</span><span class="lit">10</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="pun">{</span><span class="pln">
      </span><span class="com">// play sound alarm</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"> thisNote </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> thisNote </span><span class="pun"><</span><span class="pln"> </span><span class="lit">6</span><span class="pun">;</span><span class="pln"> thisNote</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="com">// Loop through the notes in the array.</span><span class="pln">
        </span><span class="typ">TimerFreeTone</span><span class="pun">(</span><span class="pln">SPEAKER</span><span class="pun">,</span><span class="pln"> melody</span><span class="pun">[</span><span class="pln">thisNote</span><span class="pun">],</span><span class="pln"> duration</span><span class="pun">[</span><span class="pln">thisNote</span><span class="pun">]);</span><span class="pln">

        </span><span class="com">// Play thisNote for its coresponding duration.</span><span class="pln">
        delay</span><span class="pun">(</span><span class="lit">50</span><span class="pun">);</span><span class="pln"> </span><span class="com">// Short delay between notes.</span><span class="pln">
      </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
  </span><span class="com">// STATE '2' - reset reminder</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">data </span><span class="pun">==</span><span class="pln"> </span><span class="str">'2'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    prev_time </span><span class="pun">=</span><span class="pln"> run_time</span><span class="pun">;</span><span class="pln">
  </span><span class="com">// STATE '0' - do nothing</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">data </span><span class="pun">==</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="typ">Serial</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="str">"waiting"</span><span class="pun">);</span><span class="pln">
  </span><span class="pun">}</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="str">""</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">/*    Usage: Function used to scale potentiometer values (2 of them - one for
 * hours and one for min) to a scale of [14sec, 24 hours] as evenly as 
 * possible
 * map potvals (0-1023)  
 */</span><span class="pln">
</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> calculate_threshold</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> potVal_hour</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> potVal_min</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> one_percent_hour </span><span class="pun">=</span><span class="pln"> SCALE_POT </span><span class="pun">/</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">
  </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> one_percent_min </span><span class="pun">=</span><span class="pln"> SCALE_POT </span><span class="pun">/</span><span class="pln"> </span><span class="pun">(</span><span class="lit">100</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">60</span><span class="pun">);</span><span class="pln">
  </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> result_hour</span><span class="pun">;</span><span class="pln">
  </span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> result_min</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">float</span><span class="pln"> hour_percent </span><span class="pun">=</span><span class="pln"> </span><span class="pun">((</span><span class="kwd">float</span><span class="pun">)</span><span class="pln">potVal_hour </span><span class="pun">/</span><span class="pln"> </span><span class="lit">1023.0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">
  </span><span class="kwd">float</span><span class="pln"> min_percent </span><span class="pun">=</span><span class="pln"> </span><span class="pun">((</span><span class="kwd">float</span><span class="pun">)</span><span class="pln">potVal_min </span><span class="pun">/</span><span class="pln"> </span><span class="lit">409.0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit">100</span><span class="pun">;</span><span class="pln">

  result_hour </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="pln">hour_percent </span><span class="pun">*</span><span class="pln"> one_percent_hour</span><span class="pun">;</span><span class="pln">
  result_min </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">unsigned</span><span class="pln"> </span><span class="kwd">long</span><span class="pun">)</span><span class="pln">min_percent </span><span class="pun">*</span><span class="pln"> one_percent_min</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">return</span><span class="pln"> result_hour </span><span class="pun">+</span><span class="pln"> result_min</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span>