by Karan Gugle and Lillie Widmayer


Description: Device that indicates how crooked or level a picture is by dimming/brightening an LED depending on how crooked the picture is and playing notes of varying pitch based on the brightness of the LED.
Progress Images:



Schematic:

- <span class="com">//Double Transducer: Picture Level</span><span class="pln">
- </span><span class="com">//This code reads data from the ultrasonic sensors and controls LED brightness and pitch of sound coming from the speaker.</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"><toneAC.h></span><span class="pln">
- </span><span class="com">//Ultrasonic sensors and photoresistor need to be connected to analog pins</span><span class="pln">
- </span><span class="com">//LED needs to be connected to a PWM pin to vary brightness</span><span class="pln">
- </span><span class="com">#define</span><span class="pln"> TRIG1 </span><span class="lit">12</span><span class="pln">
- </span><span class="com">#define</span><span class="pln"> ECHO1 </span><span class="lit">11</span><span class="pln">
- </span><span class="com">#define</span><span class="pln"> MAX1 </span><span class="lit">200</span><span class="pln">
- </span><span class="com">#define</span><span class="pln"> TRIG2 </span><span class="lit">13</span><span class="pln">
- </span><span class="com">#define</span><span class="pln"> ECHO2 </span><span class="lit">10</span><span class="pln">
- </span><span class="com">#define</span><span class="pln"> MAX2 </span><span class="lit">200</span><span class="pln">
- </span><span class="typ">NewPing</span><span class="pln"> sensors</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"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">// Sensor object array</span><span class="pln">
- </span><span class="typ">NewPing</span><span class="pun">(</span><span class="pln">TRIG1</span><span class="pun">,</span><span class="pln"> ECHO1</span><span class="pun">,</span><span class="pln"> MAX1</span><span class="pun">),</span><span class="pln">
- </span><span class="typ">NewPing</span><span class="pun">(</span><span class="pln">TRIG2</span><span class="pun">,</span><span class="pln"> ECHO2</span><span class="pun">,</span><span class="pln"> MAX2</span><span class="pun">)</span><span class="pln">
- </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">8</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"> PHOTORESISTOR </span><span class="pun">=</span><span class="pln"> A0</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">5</span><span class="pun">;</span><span class="pln">
- </span><span class="kwd">const</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> BASE </span><span class="pun">=</span><span class="pln"> </span><span class="lit">33.65</span><span class="pun">;</span><span class="pln"> </span><span class="com">//distance between two sensors</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">
- pinMode</span><span class="pun">(</span><span class="pln">PHOTORESISTOR</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">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">
- </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="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">int</span><span class="pln"> dist1</span><span class="pun">;</span><span class="pln">
- </span><span class="kwd">int</span><span class="pln"> dist2</span><span class="pun">;</span><span class="pln">
- delay</span><span class="pun">(</span><span class="lit">200</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">uint8_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"> </span><span class="lit">2</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="com">// Loop through each sensor and read data.</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">//time delay in order to read data from both sensors</span><span class="pln">
- </span><span class="kwd">if</span><span class="pun">(</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"> </span><span class="pun">{</span><span class="pln">
- dist1 </span><span class="pun">=</span><span class="pln"> sensors</span><span class="pun">[</span><span class="pln">i</span><span class="pun">].</span><span class="pln">ping_cm</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">
- dist2 </span><span class="pun">=</span><span class="pln"> sensors</span><span class="pun">[</span><span class="pln">i</span><span class="pun">].</span><span class="pln">ping_cm</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">/*
- print statements for debugging:
- Serial.print("dist 1 = ");
- Serial.print(dist1);
- Serial.print(" dist2 = ");
- Serial.println(dist2);
- */</span><span class="pln">
- </span><span class="kwd">int</span><span class="pln"> light </span><span class="pun">=</span><span class="pln"> analogRead</span><span class="pun">(</span><span class="pln">PHOTORESISTOR</span><span class="pun">);</span><span class="pln">
- </span><span class="kwd">int</span><span class="pln"> diff </span><span class="pun">=</span><span class="pln"> abs</span><span class="pun">(</span><span class="pln">dist1 </span><span class="pun">-</span><span class="pln"> dist2</span><span class="pun">);</span><span class="pln">
- </span><span class="kwd">float</span><span class="pln"> angle </span><span class="pun">=</span><span class="pln"> atan</span><span class="pun">(</span><span class="pln">diff</span><span class="pun">/</span><span class="pln">BASE</span><span class="pun">)*(</span><span class="lit">180</span><span class="pun">/</span><span class="lit">3.14</span><span class="pun">);</span><span class="pln"> </span><span class="com">//angle calculated through trigonometry</span><span class="pln">
- </span><span class="kwd">float</span><span class="pln"> brightness </span><span class="pun">=</span><span class="pln"> log</span><span class="pun">((</span><span class="pln">angle </span><span class="pun">+</span><span class="pln"> </span><span class="lit">9.0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="lit">90</span><span class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="pun">-</span><span class="lit">15.0</span><span class="pun">;</span><span class="pln"> </span><span class="com">//inverse relationship</span><span class="pln">
- </span><span class="com">//between angle and brightness of LED</span><span class="pln">
- </span><span class="com">/*
- More print statements for debugging:
- Serial.print("angle = ");
- Serial.println(angle);
- Serial.print("light = ");
- Serial.println(light);
- */</span><span class="pln">
- analogWrite</span><span class="pun">(</span><span class="pln">LED</span><span class="pun">,</span><span class="pln"> brightness</span><span class="pun">);</span><span class="pln">
- </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">light </span><span class="pun">></span><span class="pln"> </span><span class="lit">380</span><span class="pun">)</span><span class="pln"> toneAC</span><span class="pun">(</span><span class="lit">4500</span><span class="pun">);</span><span class="pln"> </span><span class="com">//varying tone pitch based on LED brightness</span><span class="pln">
- </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">light </span><span class="pun">></span><span class="pln"> </span><span class="lit">300</span><span class="pun">)</span><span class="pln"> toneAC</span><span class="pun">(</span><span class="lit">3500</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="pun">(</span><span class="pln">light </span><span class="pun">></span><span class="pln"> </span><span class="lit">200</span><span class="pun">)</span><span class="pln"> toneAC</span><span class="pun">(</span><span class="lit">2500</span><span class="pun">);</span><span class="pln">
- </span><span class="kwd">else</span><span class="pln"> toneAC</span><span class="pun">(</span><span class="lit">2000</span><span class="pun">);</span><span class="pln">
- </span><span class="pun">}</span><span class="pln"> </span>
We encountered challenging situations while building the Picture Level and learned a lot in the process. One of the major difficulties we had was with the ultrasonic sensors, which measured the distance to the nearest object. Both of us had only worked with ultrasonic rangers through the class exercise, so it was easy for us to get a single sensor working. We wired up the second sensor the same way we had done for the first, but ended up only being able to read a single ranger’s output at any given time. We ended solving the problem by creating a sensor array and reading the output of both sensors sequentially and learned a few things about the NewPing library in the process.
Another challenge we had was with the light and photoresistor relationship. We placed an LED directly adjacent to the photoresistor to capture how it dimmed and brightened according to the angle of the “painting”. However, the surrounding ambient light and the limited range of brightness the LED could produce led to a very small range of photoresistor values (300 – 350). This made it very hard for us to produce an accurate tone from the speaker, because we had such an inaccurate and limited range from the first transducer step. We decided to use the log() function to spread out the range of the photoresistor, so that it would be artificially more sensitive to changes in LED brightness.
Although these challenges were hard to overcome, we think it was smart to use sensors we had worked with before. Instead of fiddling around with the wiring / basics, we were able to concentrate on solving system-based challenges, which gave way to more thought and empirical testing. Our ultrasonic rangers encountered difficulties in accuracy due to the sequential reading of output and the delays we had set in the code, which is something we would adjust if we had to demo again.
Overall, we were able to create what we thought was an interesting project that required us to experiment and adjust as we worked. It brought together familiar sensors from class and applications of those sensors that were totally new to us. One aspect of the project both of us enjoyed was working with K’NEX to build housing for our breadboards, Arduino, and battery. Both of us see K’NEX as a good way to quickly prototype and hold together complex creations in the design phase. For the next project, we’d both like to focus more on brainstorming and planning. After seeing some of our other classmates creations, it would have beneficial if we could have created a more cohesive set of transducers.
Comments are closed.