tag:blogger.com,1999:blog-32196532736747089002024-03-05T09:38:44.990+01:00Getting Into ArduinoDaniel Fureloshttp://www.blogger.com/profile/18259717346490561724noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-3219653273674708900.post-42873830602224972852014-06-11T00:17:00.000+02:002014-06-11T17:20:07.787+02:00Practice - Remotely controlling a car with Arduino by using Xbee<h2>
About</h2>
<div>
<div style="text-align: justify;">
In this post we explain the process as usual. However, if you prefer it, you can download our class presentation slides by clicking on the dropbox link below:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<a href="https://www.dropbox.com/s/ejy74bryu14cfh6/XSSF_finalPres.pdf" rel="nofollow" style="background-color: white; color: #3b5998; cursor: pointer; font-size: 14.399999618530273px; line-height: 15.455999374389648px; text-align: left; text-decoration: none;" target="_blank"><span style="font-family: Times, Times New Roman, serif;">https://www.dropbox.com/s/ejy74bryu14cfh6/XSSF_finalPres.pdf</span></a></div>
</div>
<h2>
Description</h2>
<div style="text-align: justify;">
This time we are going to show you our final project for the Wireless Sensor Networks course, which consists in a remote controlled car built with an Arduino.<br />
<br />
Communication between the remote control and the car is going to be supported with XBee's. As usual, here is the list of the needed material to the final build:</div>
<ul>
<li><span style="font-size: large;"><span style="font-size: small;">Hookup wires</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">1x Arduino Uno</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">2x XBee (with XBee explorer)</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">2x </span></span><span class="_5yl5" data-reactid=".85.$mid=11402311343935=2ac33d0b7b805f93502.2:0.0.0.0.0"><span class="null"><span class="_5yl5" data-reactid=".85.$mid=11402311343935=2ac33d0b7b805f93502.2:0.0.0.0.0"><span class="null">BC547</span></span> Transistor</span></span><span style="font-size: large;"><span style="font-size: small;"> </span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">2x 1N4007 Diodes</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">2x 1KOhm Resistors</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">3x 10KOhm Resistors</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">3x Push Buttons </span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">6x 1,5V Batteries (2x AAA + 4 x AA)</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">2x Battery Holders (one with capcacity 2 and the other with cap. 4)</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">2x Motors</span></span></li>
<li><span style="font-size: large;"><span style="font-size: small;">3x Wheels</span> <span style="font-size: small;">(one for support)</span></span></li>
<li><span style="font-size: large;">... </span></li>
</ul>
<div style="text-align: justify;">
Before showing the circuit and the code used, we want to explain you some features about the electronics that we have applied on the design, which will roughly give you the needed knowledge:</div>
<div style="text-align: justify;">
<br /></div>
<h3>
Voltage batteries</h3>
<div style="text-align: justify;">
When using circuits with different voltages, you have to connect their grounds together to provide a <b>common ground</b>. In our case, we have used 3V(2x 1,5V) and 6V (4x 1,5V) batteries for feeding one XBee and the motors, respectively.</div>
<div style="text-align: justify;">
<br /></div>
<h3>
<b>Transistor</b></h3>
<div style="text-align: justify;">
A transistor is an electronic device that can be used on circuits for amplifying or switching purposes. The transistor that we have used (of type Negative-Positive-Negative, NPN) is bipolar junction type and has three terminals (base, collector and emitter), which currents and voltages depend on the others, i.e. current on the base terminal controls the current on collector and emitter.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
In our case, we have used transistors as switches, changing their state from OFF to ON as desired, in order to have control on the power supply. This can be done because transistors do not let the current path unless a saturation voltage on the Collector pin is reached.</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuLD-YF2arjVkTzK504L8mz0YsXqPL-0_FrpMWHUv_FQIUa5B7gnjEsyvYLm6NbYSbTYPN_ycXpCJ3H2ynEWc9BVJRQtZVs8_6KnOy6spNUgJWC838lbiojp7LB6UYDJDtFM-kZRHZ9l4/s1600/npn.gif" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuLD-YF2arjVkTzK504L8mz0YsXqPL-0_FrpMWHUv_FQIUa5B7gnjEsyvYLm6NbYSbTYPN_ycXpCJ3H2ynEWc9BVJRQtZVs8_6KnOy6spNUgJWC838lbiojp7LB6UYDJDtFM-kZRHZ9l4/s1600/npn.gif" height="198" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure : NPN Transistor Scheme [1]</td></tr>
</tbody></table>
<div style="text-align: justify;">
When maximum Collector current flows the transistor is said to be Saturated. The value of the Base resistor determines how much input voltage is required and corresponding Base current to switch the transistor fully “ON”. In our case, we had to implement a voltage divisor in order to get an input base intensity low enough to make the transistor act as a <b>digital switch</b>.</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp-qiMEcDwq7KNWFoY6_IAsq942PbywbEJc6dKF8MxRhsBhJTS0v1P3awqzMj-hHrpjBQEZcsXg90BDZV2DzBKYSvk_qNU3KA3lWqy9xR8iNfhyphenhyphentb8iC0cWe7U4CCN1_IxLRsuoppMlEg/s1600/transistor2.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp-qiMEcDwq7KNWFoY6_IAsq942PbywbEJc6dKF8MxRhsBhJTS0v1P3awqzMj-hHrpjBQEZcsXg90BDZV2DzBKYSvk_qNU3KA3lWqy9xR8iNfhyphenhyphentb8iC0cWe7U4CCN1_IxLRsuoppMlEg/s1600/transistor2.jpg" height="200" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure : <span class="_5yl5" data-reactid=".85.$mid=11402311343935=2ac33d0b7b805f93502.2:0.0.0.0.0"><span class="null"><span class="_5yl5" data-reactid=".85.$mid=11402311343935=2ac33d0b7b805f93502.2:0.0.0.0.0"><span class="null">BC547</span></span> Transisto<span style="font-size: x-small;">r [2]</span></span></span></td></tr>
</tbody></table>
<h3>
<b>Diode</b></h3>
<div style="text-align: justify;">
Diode is a polarized component which can go only one way in the circuit (it let the current path just in one direction) and it has a stripe on one end. That end is the negative end, or cathode, of the diode. The other end is the positive end, or anode. The anode of the diode is connected to the ground of the motor and the cathode of the diode to the power of the motor.<br />
<br />
This may seem backward, and in fact, it is. The diode will help prevent any back-voltage generated by the motor from going back into the circuit. Back voltage will flow in the opposite direction of the voltage supply (must be avoided). The diodes we have used are Diodes 1N4007.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB6cMZg96S0IcTAiVV-fVXES1CQszVC5EdWV01q-q9t8Iw7z_Yf0FBr7TZ5d9-YTwbLgVVzx0rUEGp-1KdiK43f43dbXokfmIzF-k_-LaLWchvpeq6F8fJYwXiXdVvsJarM0nsUFUzLns/s1600/diodo-1n4007.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB6cMZg96S0IcTAiVV-fVXES1CQszVC5EdWV01q-q9t8Iw7z_Yf0FBr7TZ5d9-YTwbLgVVzx0rUEGp-1KdiK43f43dbXokfmIzF-k_-LaLWchvpeq6F8fJYwXiXdVvsJarM0nsUFUzLns/s1600/diodo-1n4007.jpg" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Diode 1N4007</td></tr>
</tbody></table>
</div>
<div style="text-align: justify;">
<br />
<h3>
Assembly of the pieces</h3>
<div>
To mount the car we used the breadboard as the support of all the materials (e.g. XBee Explorer, hookup wires, batteries, motors). We needed some way to stick all these materials; therefore we used some adhesive tape along with mini-boards of cork so that motors and batteries could be well fixed to the breadboard.</div>
<div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkJ4fWh_9rzc-fqfJ7TFN568KwLILoVRh8tQogn0B1A_0OYXKt5Xsu3CD3rFDi3sGCSkUWGSZaDUNzBrrTvk5Ynaraf_0Ol-kZgj5h_wKCZZFn6Omleq7jUL6iZ6BC0i3adcrBw-9Lz98/s1600/CAM00490.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkJ4fWh_9rzc-fqfJ7TFN568KwLILoVRh8tQogn0B1A_0OYXKt5Xsu3CD3rFDi3sGCSkUWGSZaDUNzBrrTvk5Ynaraf_0Ol-kZgj5h_wKCZZFn6Omleq7jUL6iZ6BC0i3adcrBw-9Lz98/s1600/CAM00490.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Car ready to be build (left circuit is the controller)</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_qc6RjZqevYqVUioNVpEaH3neDyqrXEClj-y_Fwd3fby1yTmvNgqwToxThgQFoDZ422ydcXVmS3XMOWP5SfVzyP7sFEq20jMzmA68-Y9h3ZhIXPChKaLtiMeizzN8woyWQzkVZtlZI98/s1600/CAM00495.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_qc6RjZqevYqVUioNVpEaH3neDyqrXEClj-y_Fwd3fby1yTmvNgqwToxThgQFoDZ422ydcXVmS3XMOWP5SfVzyP7sFEq20jMzmA68-Y9h3ZhIXPChKaLtiMeizzN8woyWQzkVZtlZI98/s1600/CAM00495.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Put the motors over the first cork layer and stick them with adhesive tape (I)</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB5F3KWiUALCgzptOjJ8-0djcqmgNXtjW60MTTmcpmOSg26RJllYjrl22mGwwaM5zUDl1uBXiuJRWSzyHF0kNUfYGJtD5GSaZY90lb65IhFAr_77163Yj1fPYLgBfdQIC5i9POcGiP2x0/s1600/CAM00496.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB5F3KWiUALCgzptOjJ8-0djcqmgNXtjW60MTTmcpmOSg26RJllYjrl22mGwwaM5zUDl1uBXiuJRWSzyHF0kNUfYGJtD5GSaZY90lb65IhFAr_77163Yj1fPYLgBfdQIC5i9POcGiP2x0/s1600/CAM00496.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Put the motors over the first cork layer and stick them with adhesive tape (II)</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdCihoYRi8yHrvZbIJJDw9M5IzsMfYKICHeffS-NAtvAVnYA_F0enOUx5pi5VvNf6y1GjFNImZxnc0uaaLWHuQfjFKO-1fDgSxmzUxWbsakbZkIIZ55ztWkssEd7Tb5gxeuOfLKlJbwwM/s1600/CAM00497.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdCihoYRi8yHrvZbIJJDw9M5IzsMfYKICHeffS-NAtvAVnYA_F0enOUx5pi5VvNf6y1GjFNImZxnc0uaaLWHuQfjFKO-1fDgSxmzUxWbsakbZkIIZ55ztWkssEd7Tb5gxeuOfLKlJbwwM/s1600/CAM00497.jpg" height="320" width="240" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Cork layer used to mount the car</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQT531hM_TNcEwwDC0fp3T3NlGif9tkWgCIOdGvbyEFSPjXsY_QszCARJKF_PVaEryUG56pBomTjUaod-b1zrcqGUAfvZypw6qVDBrb_ibZSE95tNyxj7G2o8BJlm2l9z08KfhraAGinc/s1600/CAM00498.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQT531hM_TNcEwwDC0fp3T3NlGif9tkWgCIOdGvbyEFSPjXsY_QszCARJKF_PVaEryUG56pBomTjUaod-b1zrcqGUAfvZypw6qVDBrb_ibZSE95tNyxj7G2o8BJlm2l9z08KfhraAGinc/s1600/CAM00498.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Stick the batteries to the last cork layer (there are 3 layers)</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzK5G3tS7uMhvmf1uKfwF8FE4HnI80slO3uuTVWUOPP7D7mukXN1gy3U1-VxJ_AF_OeF-P2PDemDL58AwddelqI3KfPc3Tf8Ck4Q6sZ_KhcQxjPFGEYb41JoGInW7C4XgczlgnO-xDw4E/s1600/CAM00499.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzK5G3tS7uMhvmf1uKfwF8FE4HnI80slO3uuTVWUOPP7D7mukXN1gy3U1-VxJ_AF_OeF-P2PDemDL58AwddelqI3KfPc3Tf8Ck4Q6sZ_KhcQxjPFGEYb41JoGInW7C4XgczlgnO-xDw4E/s1600/CAM00499.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Final circuit (without battery feeding the XBee)</td></tr>
</tbody></table>
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmoH8dWDDIUO9Fg6Igrb1HFEyVfmKluMsUqw4e9Q2OruxNDlSt59wR8in9s93CLDZVZXozZlxjB0rLsEXeSkGAkSDghNdzOm8ilCwJzPBD0m4Ry9ICAsn8X-YdQ7eEdUHjrqB32tsQjVg/s1600/CAM00504.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmoH8dWDDIUO9Fg6Igrb1HFEyVfmKluMsUqw4e9Q2OruxNDlSt59wR8in9s93CLDZVZXozZlxjB0rLsEXeSkGAkSDghNdzOm8ilCwJzPBD0m4Ry9ICAsn8X-YdQ7eEdUHjrqB32tsQjVg/s1600/CAM00504.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Final circuit (with battery feeding the XBee)</td></tr>
</tbody></table>
<div>
Moreover, we had problems with motors and wheels since the axis of the motors was not wide enough to be put in the wheel. To solve this we used some elastic weave put along the motor axis and then we put some adhesive tape over it. This made enough space for the wheel to fit well in the axis. Another possibility, which could work better, consists in adding blu-tack in the joint between the wheel and the axis. In any case, try to get a wheel that fits perfectly the motor axis to avoid these shoddy works.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlv-66DVi-BeqZFCMErZt51btr9QQxsGIYwXHaszsu7wqYYwdX2m_Hbn3kk1DcRCP0xYQJcYs-xGBWIrHpWDmXDL8wf3iZFPM-2-syV2znDx-PISTkj7pFKq4il_B1znP3EERe3MavOFc/s1600/CAM00487.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlv-66DVi-BeqZFCMErZt51btr9QQxsGIYwXHaszsu7wqYYwdX2m_Hbn3kk1DcRCP0xYQJcYs-xGBWIrHpWDmXDL8wf3iZFPM-2-syV2znDx-PISTkj7pFKq4il_B1znP3EERe3MavOFc/s1600/CAM00487.jpg" height="320" width="240" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Motor axis covered by elastic weave to fit into the wheel hole</td></tr>
</tbody></table>
<br />
Respect to the motors, it was necessary to weld them to hookup wires since it was not done. The motors we used are the ones at [3].<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIDkGSQ7x2PmarctO1rbeaVm2A9m8kT_AVArchiaf_TOb3PTMGeHzx2c2DY8_g_BKCW4_oX1lQAGu0Wkt2qV8IR-IVnjFbFAUUdpcvfc8HmQ_fPWHuwC2evY06GaN86jbeyw5-fz8_1sk/s1600/CAM00482.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIDkGSQ7x2PmarctO1rbeaVm2A9m8kT_AVArchiaf_TOb3PTMGeHzx2c2DY8_g_BKCW4_oX1lQAGu0Wkt2qV8IR-IVnjFbFAUUdpcvfc8HmQ_fPWHuwC2evY06GaN86jbeyw5-fz8_1sk/s1600/CAM00482.jpg" height="320" width="240" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Welding the hookup wires to the motor (I)</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6DNn5aGPC1p05oKmCW8kOoJC2jznxQW-VConR0FBLCYzBXBJjnXXN1vqfocP2ZKJKg_PfNALTtJ_3zBKi7NmYixs2aYhxZdoKR1WtULe7ihyphenhyphenAzBzO93c5f1ONyY7uvC5QzIuwaSEd1ds/s1600/CAM00484.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6DNn5aGPC1p05oKmCW8kOoJC2jznxQW-VConR0FBLCYzBXBJjnXXN1vqfocP2ZKJKg_PfNALTtJ_3zBKi7NmYixs2aYhxZdoKR1WtULe7ihyphenhyphenAzBzO93c5f1ONyY7uvC5QzIuwaSEd1ds/s1600/CAM00484.jpg" height="320" width="240" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Welding the hookup wires to the motor</td></tr>
</tbody></table>
<br />
There is also a support wheel which is not actioned by a motor. We also stuck to the last cork layer using blu-tack. It made that the height of the breadboard increased and it made not work completely well; ideally, it would have been put such that the height of the breadboard was completely straight.</div>
<div>
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkqhVrHmXGeZEquaBm5S2mLO9Lirdr4UKaS8j2mfL9jaAekzM3I73oUid4t0ZtKL98cUbxEgYe8-CfxcYL3ULvN6Ail_eSsZQdrAxIpclbgqXIwNDNH6KS2-tjHST1aEThW-L_OeQue4E/s1600/10469217_10204145393937912_3278203474971165508_n.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkqhVrHmXGeZEquaBm5S2mLO9Lirdr4UKaS8j2mfL9jaAekzM3I73oUid4t0ZtKL98cUbxEgYe8-CfxcYL3ULvN6Ail_eSsZQdrAxIpclbgqXIwNDNH6KS2-tjHST1aEThW-L_OeQue4E/s1600/10469217_10204145393937912_3278203474971165508_n.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Support wheel stuck with blu-tack</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
Finally, after adding all these components we have the final result:</div>
<div>
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGn56sMbJN660Qo2GLvVR8vcw7FvPAA96vbxKpNIEhMqNdZO4z2aPD_Q-HU8yt1Nl2Iv4FZZ7DrcQbITdPD606LwKrXMkzRyaZkps3TuN0PkjhperClBAL8XOlrZe92gp5-QciFoboTVU/s1600/10349017_10204145369617304_8468604330160886334_n.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGn56sMbJN660Qo2GLvVR8vcw7FvPAA96vbxKpNIEhMqNdZO4z2aPD_Q-HU8yt1Nl2Iv4FZZ7DrcQbITdPD606LwKrXMkzRyaZkps3TuN0PkjhperClBAL8XOlrZe92gp5-QciFoboTVU/s1600/10349017_10204145369617304_8468604330160886334_n.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Final car with the remote control to drive it</td></tr>
</tbody></table>
</div>
<h2 style="text-align: justify;">
Circuits</h2>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-mQVtnOTQBe1eepEkrZL52vHrEY_9GgflTidlaaDMjdrIq0XKmbDBVwYullC8-YpEa5-4Yf70MqlwguTwFFr_cCYDjcywSSuzTMqLokj7fnCh40Mpt_X5TzmYScAJTqyShN8XBk9larI/s1600/carProject_Control_fritzing_bb.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-mQVtnOTQBe1eepEkrZL52vHrEY_9GgflTidlaaDMjdrIq0XKmbDBVwYullC8-YpEa5-4Yf70MqlwguTwFFr_cCYDjcywSSuzTMqLokj7fnCh40Mpt_X5TzmYScAJTqyShN8XBk9larI/s1600/carProject_Control_fritzing_bb.png" height="320" width="253" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Controller circuit</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRJ0IckA03IsUIODmCgfBbIXr1B-TnYmbwDyV5Ca2mJHKFFwS_G131nGbiBEUOimpYA3ITTueFsy_I3vXkl21lRTwcXuLPM-WmjFdSPncoyuEmeICpfIsSrAsvrvQg5y9P1GoExjXD1jg/s1600/car+fritz.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRJ0IckA03IsUIODmCgfBbIXr1B-TnYmbwDyV5Ca2mJHKFFwS_G131nGbiBEUOimpYA3ITTueFsy_I3vXkl21lRTwcXuLPM-WmjFdSPncoyuEmeICpfIsSrAsvrvQg5y9P1GoExjXD1jg/s1600/car+fritz.png" height="241" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure: Car circuit</td></tr>
</tbody></table>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div>
<br /></div>
<h2 style="text-align: justify;">
Code</h2>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * This code is used to control the remote car by sending</span>
<span style="color: #60a0b0; font-style: italic;"> * messages through an XBee.</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Pins of the Arduino to which the switches to move the car are connected</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> LEFT_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> FORW_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">3</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> RIGHT_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">4</span>;
<span style="color: #60a0b0; font-style: italic;">// Pins to which we will transmit the information we want to send to the XBee</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> D1_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">8</span>; <span style="color: #60a0b0; font-style: italic;">// corresponds to right wheel</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> D2_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">9</span>; <span style="color: #60a0b0; font-style: italic;">// corresponds to left wheel</span>
<span style="color: #60a0b0; font-style: italic;">// Keeps track of the desired remote on/off state (left, right, forward)</span>
<span style="color: #902000;">int</span> lRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #902000;">int</span> rRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #902000;">int</span> fRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #60a0b0; font-style: italic;">// Keeps track of the previous remote on/off state</span>
<span style="color: #902000;">int</span> last_lRI<span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #902000;">int</span> last_rRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #902000;">int</span> last_fRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #60a0b0; font-style: italic;">// records last time the remote was re-set to keep it in sync</span>
<span style="color: #902000;">unsigned</span> <span style="color: #902000;">long</span> lastSent <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
<span style="color: #60a0b0; font-style: italic;">// declare the switch (directions) pins as inputs </span>
pinMode(LEFT_PIN, INPUT);
pinMode(FORW_PIN, INPUT);
pinMode(RIGHT_PIN, INPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
setRemoteIndicators();
sendInformation();
}
<span style="color: #60a0b0; font-style: italic;">// Read the value of the switches</span>
<span style="color: #60a0b0; font-style: italic;">// digitalRead() checks to see if there is voltage on the pin or not </span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setRemoteIndicators</span>()
{
<span style="color: #007020; font-weight: bold;">if</span> (digitalRead(LEFT_PIN) <span style="color: #666666;">==</span> HIGH) <span style="color: #60a0b0; font-style: italic;">// move left</span>
{
lRI <span style="color: #666666;">=</span> <span style="color: #007020;">true</span>;
rRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
fRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
}
<span style="color: #007020; font-weight: bold;">else</span> <span style="color: #007020; font-weight: bold;">if</span> (digitalRead(FORW_PIN) <span style="color: #666666;">==</span> HIGH) <span style="color: #60a0b0; font-style: italic;">// move forward</span>
{
lRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
rRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
fRI <span style="color: #666666;">=</span> <span style="color: #007020;">true</span>;
}
<span style="color: #007020; font-weight: bold;">else</span> <span style="color: #007020; font-weight: bold;">if</span> (digitalRead(RIGHT_PIN) <span style="color: #666666;">==</span> HIGH) <span style="color: #60a0b0; font-style: italic;">// move right</span>
{
lRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
rRI <span style="color: #666666;">=</span> <span style="color: #007020;">true</span>;
fRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
}
<span style="color: #007020; font-weight: bold;">else</span> <span style="color: #60a0b0; font-style: italic;">// do not move</span>
{
lRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
rRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
fRI <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
}
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">sendInformation</span>()
{
<span style="color: #60a0b0; font-style: italic;">// set the indicators immediately when there's a state change</span>
<span style="color: #007020; font-weight: bold;">if</span> (lRI <span style="color: #666666;">!=</span> last_lRI)
{
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>lRI) setRemoteState(<span style="color: #40a070;">0x4</span>,<span style="color: #4070a0;">'1'</span>);
<span style="color: #007020; font-weight: bold;">if</span> (lRI) setRemoteState(<span style="color: #40a070;">0x5</span>,<span style="color: #4070a0;">'1'</span>);
last_lRI <span style="color: #666666;">=</span> lRI;
}
delay(<span style="color: #40a070;">10</span>);
<span style="color: #007020; font-weight: bold;">if</span> (rRI <span style="color: #666666;">!=</span> last_rRI)
{
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>rRI) setRemoteState(<span style="color: #40a070;">0x4</span>,<span style="color: #4070a0;">'2'</span>);
<span style="color: #007020; font-weight: bold;">if</span> (rRI) setRemoteState(<span style="color: #40a070;">0x5</span>,<span style="color: #4070a0;">'2'</span>);
last_rRI <span style="color: #666666;">=</span> rRI;
}
delay(<span style="color: #40a070;">10</span>);
<span style="color: #007020; font-weight: bold;">if</span> (fRI <span style="color: #666666;">!=</span> last_fRI)
{
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>fRI)
{
setRemoteState(<span style="color: #40a070;">0x4</span>,<span style="color: #4070a0;">'1'</span>);
setRemoteState(<span style="color: #40a070;">0x4</span>,<span style="color: #4070a0;">'2'</span>);
}
<span style="color: #007020; font-weight: bold;">if</span> (fRI)
{
setRemoteState(<span style="color: #40a070;">0x5</span>,<span style="color: #4070a0;">'1'</span>);
setRemoteState(<span style="color: #40a070;">0x5</span>,<span style="color: #4070a0;">'2'</span>);
}
last_fRI <span style="color: #666666;">=</span> fRI;
}
<span style="color: #60a0b0; font-style: italic;">// re-set the indicator occasionally in case it's out of sync</span>
<span style="color: #007020; font-weight: bold;">if</span> (millis() <span style="color: #666666;">-</span> lastSent <span style="color: #666666;">></span> <span style="color: #40a070;">10000</span> )
{
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>lRI) setRemoteState(<span style="color: #40a070;">0x4</span>,<span style="color: #4070a0;">'1'</span>);
<span style="color: #007020; font-weight: bold;">if</span> (lRI) setRemoteState(<span style="color: #40a070;">0x5</span>,<span style="color: #4070a0;">'1'</span>);
delay(<span style="color: #40a070;">10</span>);
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>rRI) setRemoteState(<span style="color: #40a070;">0x4</span>,<span style="color: #4070a0;">'2'</span>);
<span style="color: #007020; font-weight: bold;">if</span> (rRI) setRemoteState(<span style="color: #40a070;">0x5</span>,<span style="color: #4070a0;">'2'</span>);
lastSent <span style="color: #666666;">=</span> millis();
}
}
<span style="color: #60a0b0; font-style: italic;">// Send msg through the xbee (note that the frame has to be formed)</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setRemoteState</span>(<span style="color: #902000;">int</span> value, <span style="color: #902000;">char</span> pinNum)
{
Serial.write(<span style="color: #40a070;">0x7e</span>);<span style="color: #60a0b0; font-style: italic;">//Start byte</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);<span style="color: #60a0b0; font-style: italic;">//Length</span>
Serial.write(<span style="color: #40a070;">0x10</span>);<span style="color: #60a0b0; font-style: italic;">//Length High</span>
Serial.write(<span style="color: #40a070;">0x17</span>);<span style="color: #60a0b0; font-style: italic;">//AT Command Request</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);<span style="color: #60a0b0; font-style: italic;">//Frame ID</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);<span style="color: #60a0b0; font-style: italic;">//Serial Number of Destination</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write(<span style="color: #40a070;">0xFF</span>);
Serial.write(<span style="color: #40a070;">0xFF</span>);<span style="color: #60a0b0; font-style: italic;">//End of Serial Number of Destinition</span>
<span style="color: #60a0b0; font-style: italic;">// 16 bit of recipient or 0xFFFE if unknown</span>
Serial.write(<span style="color: #40a070;">0xFF</span>);
Serial.write(<span style="color: #40a070;">0xFE</span>);
Serial.write(<span style="color: #40a070;">0x02</span>);<span style="color: #60a0b0; font-style: italic;">//Apply changes immediately</span>
<span style="color: #60a0b0; font-style: italic;">//Command name in ASCII characters</span>
Serial.write(<span style="color: #4070a0;">'D'</span>);
Serial.write(pinNum);
<span style="color: #60a0b0; font-style: italic;">//command data in as many bytes after length bytes</span>
Serial.write(value);
<span style="color: #60a0b0; font-style: italic;">//checksum is all bytes after length bytes</span>
<span style="color: #902000;">long</span> sum <span style="color: #666666;">=</span> <span style="color: #40a070;">0x17</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0xFF</span><span style="color: #666666;">+</span> <span style="color: #40a070;">0xFF</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0xFF</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0xFE</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0x02</span> <span style="color: #666666;">+</span> <span style="color: #4070a0;">'D'</span> <span style="color: #666666;">+</span> pinNum <span style="color: #666666;">+</span> value;
Serial.write (<span style="color: #40a070;">0xFF</span> <span style="color: #666666;">-</span> ( sum <span style="color: #666666;">&</span> <span style="color: #40a070;">0xFF</span>) );
delay(<span style="color: #40a070;">10</span>); <span style="color: #60a0b0; font-style: italic;">// avoiding overwhelming</span>
}
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<h2>
Video
</h2>
<div style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/fjI3n6i-iIo" width="420"></iframe>
</div>
<h2 style="text-align: left;">
References</h2>
[1] <a href="http://www.electronics-tutorials.ws/transistor/tran_2.html" target="_blank">www.electronics-tutorials.ws/transistor/tran_2.html </a><br />
[2] <a href="http://www.bhashatech.com/transistors/48-npn-bc547.html">www.bhashatech.com/transistors/48-npn-bc547.html</a><br />
[3] <a href="http://www.bricogeek.com/shop/motores/220-motor-micro-metal-dc-con-reductora-298-1.html">http://www.bricogeek.com/shop/motores/220-motor-micro-metal-dc-con-reductora-298-1.html</a>Anonymoushttp://www.blogger.com/profile/02012194402490053567noreply@blogger.com0tag:blogger.com,1999:blog-3219653273674708900.post-66913732804611115812014-06-03T08:57:00.001+02:002014-06-04T21:35:37.413+02:00Practice - Measuring Light Pollution in Barcelona<h2>
Description</h2>
<div style="text-align: justify;">
This time we are going to measure some values with sensors and send them automatically to a Visualization Platform (xively.com) through automatically generated json code.</div>
<div style="text-align: justify;">
The first part consits in a brief explanation about how to configure the system and how to send our first measurements.</div>
<div style="text-align: justify;">
Finally, we are going to extend all this with the aim to provide real evidences of how light pollution affects us and our environment (animals, plants...). As usual, we now provide a list of the material need for this practice, including HW and SW.</div>
<br />
The materials we will need are the following:<br />
<ul>
<li>Hookup wires</li>
<li>2x XBee (1 configured as ZigBee Router API, 1 configured as ZigBee Coordinator API)</li>
<li>2x XBee Explorer </li>
<li>2x resistors of 22 kOhms (one is for backup)</li>
<li>2x LDR</li>
<li>1x Arduino Uno (to feed one of the XBees)</li>
<li>1x Computer</li>
</ul>
<br />
The software you will need is the following:<br />
<ul>
<li>XCTU.</li>
<li>Curl <a href="http://curl.haxx.se/download.html">Download here</a> </li>
<li>Python <a href="https://www.python.org/downloads/">Download here</a></li>
<li>XBee library for Python <a href="https://pypi.python.org/pypi/XBee/2.1.0">Download here</a></li>
<li>Pyserial library for Python <a href="https://pypi.python.org/pypi/pyserial">Download here</a> (<a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/" target="_blank">mirror</a>) [2].</li>
<li>Xively platform to upload data <a href="https://xively.com/">Access here</a></li>
</ul>
<h2>
Part 1</h2>
<div style="text-align: justify;">
As in previous lab, we have an XBee set to ZigBee Router API and the other XBee set to ZigBee Coordinator API.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The router has to contain same PAN ID as coordinator and its address in the destination field. The field AP must be set to 2 (API enable) and we also have to configure port DO into ADC in order to receive data from the sensor.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Note that we are setting the IO Sampling Rate with 400 ms, which means that we send packets with rate 2.5 packets/second. We did that in order not to collapse Xively when we upload data.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Before sending data to Xively we must ensure that we have available "curl" command to allow communication with the Internet. If you are on windows you should download an executable and add it to your path (if you are working with cmd). You can access your computer's path variable by accessing environment variables on properties menu.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Another issue you must take into account before executing the code is to set the API endpoint with "https" or "http". It is possible that you find troubles with certifieds using "https", so we recomend you to use "http".</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Now we are able to execute the code! If did not have already created an account on Xivel, you can access their webpage and create one. You will also need to add a new device and provide a description of it in order to obtain the "<span class="module-line-label three columns"><span class="has-tip tip-right tip-fixed">API Endpoint</span><span class="has-tip tip-right tip-fixed">" and the "API Key", which must be included in your python code.</span></span></div>
<div style="text-align: justify;">
<span class="module-line-label three columns"><span class="has-tip tip-right tip-fixed"><br /></span></span></div>
<div style="text-align: justify;">
<span class="module-line-label three columns"><span class="has-tip tip-right tip-fixed">As the image below shows, we have uploaded our measurements into the Xively platform:</span></span></div>
<span class="module-line-label three columns"><span class="has-tip tip-right tip-fixed"></span></span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3n-sin7Nb8PmrOl_fAjBhALC7uCU6p5bHUFmLV1XDXK0Oe6hwVt0hEMecK8ABPI76LTDkZ8MOOej6AXQRg2gG5pIG7HHzwNFjSscGrCLEyHn7JJRboCpSEvcWh_qDBfC6jdE170QBN1A/s1600/image1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3n-sin7Nb8PmrOl_fAjBhALC7uCU6p5bHUFmLV1XDXK0Oe6hwVt0hEMecK8ABPI76LTDkZ8MOOej6AXQRg2gG5pIG7HHzwNFjSscGrCLEyHn7JJRboCpSEvcWh_qDBfC6jdE170QBN1A/s1600/image1.png" height="263" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Measurements on Xively</td></tr>
</tbody></table>
<h3>
Circuit Part 1</h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIXJn9STrsa2gMEtqrkOfh5sggzD4kSmcYBozUo6AF2ZeTmdr4MqGZOVpsgMyKgzCObb0jowONMIP7T1R3kEkUru5reUE_OwjV0JT-z34JLB7iy9Y57T8ZNpKmkdd3D9Vxe44-DNHMeW0/s1600/circuit1.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIXJn9STrsa2gMEtqrkOfh5sggzD4kSmcYBozUo6AF2ZeTmdr4MqGZOVpsgMyKgzCObb0jowONMIP7T1R3kEkUru5reUE_OwjV0JT-z34JLB7iy9Y57T8ZNpKmkdd3D9Vxe44-DNHMeW0/s1600/circuit1.jpg" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Picture Circuit Part 1</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjevPMQ6NoVOiGq0pfG0XwMSe91IVVNp512loy6edOn2VRiKdy8CAfRXNRgXAQxpQoMY2VjG3PjpLROCn3CWs5AP8rntAbPcn5mL-2C9a_Qp1n3uIVkn-8AwTw2uQ5PXJzVGXPwTcOJJvw/s1600/circui1fritz.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjevPMQ6NoVOiGq0pfG0XwMSe91IVVNp512loy6edOn2VRiKdy8CAfRXNRgXAQxpQoMY2VjG3PjpLROCn3CWs5AP8rntAbPcn5mL-2C9a_Qp1n3uIVkn-8AwTw2uQ5PXJzVGXPwTcOJJvw/s1600/circui1fritz.jpg" height="219" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: Circuit Part 1</td></tr>
</tbody></table>
<h3>
Code Part 1 [3]</h3>
<div style="text-align: justify;">
The following code reads incoming values of light measurements done on the coordinator side, creates a JSON file and executes the command for uploading this file to Xively platform. A new JSON file is done and upload every time we receive data.</div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;"># Derived from code by Alejandro Andreu</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">commands</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">json</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">time</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">os</span>
<span style="color: #008800; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">serial</span> <span style="color: #008800; font-weight: bold;">import</span> SerialException
<span style="color: #008800; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #008800; font-weight: bold;">import</span> ZigBee
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0;">'Asynchronously printing data from remote XBee'</span>
serial_port <span style="color: #333333;">=</span> serial<span style="color: #333333;">.</span>Serial(<span style="background-color: #fff0f0;">'COM5'</span>, <span style="color: #0000dd; font-weight: bold;">9600</span>)
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">print_data</span>(data):
<span style="color: #dd4422;">"""</span>
<span style="color: #dd4422;"> This method is called whenever data is received.</span>
<span style="color: #dd4422;"> Its only argument is the data within the frame.</span>
<span style="color: #dd4422;"> """</span>
<span style="color: #008800; font-weight: bold;">print</span> data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>keys()[<span style="color: #0000dd; font-weight: bold;">0</span>]
<span style="color: #888888;"># Create a JSON file and fill it with the received samples</span>
json_data <span style="color: #333333;">=</span> {}
json_data[<span style="background-color: #fff0f0;">'version'</span>] <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">'0.2'</span>
json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">=</span> ()
json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">=</span> json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">+</span> ({<span style="background-color: #fff0f0;">'id'</span>: data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>keys()[<span style="color: #0000dd; font-weight: bold;">0</span>], <span style="background-color: #fff0f0;">'current_value'</span>: <span style="color: #007020;">str</span>(data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>values()[<span style="color: #0000dd; font-weight: bold;">0</span>])},)
<span style="color: #888888;"># Add more datastreams if needed</span>
<span style="color: #008800; font-weight: bold;">with</span> <span style="color: #007020;">open</span>(<span style="background-color: #fff0f0;">'Xbee2Xively.json'</span>, mode<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">'w'</span>) <span style="color: #008800; font-weight: bold;">as</span> f:
json<span style="color: #333333;">.</span>dump(json_data, f, indent <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">4</span>)
<span style="color: #888888;"># Upload information to Xively. Use your own Api Key and feed identifier</span>
os<span style="color: #333333;">.</span>popen(<span style="background-color: #fff0f0;">'curl --request PUT --data-binary @Xbee2Xively.json --header "X-ApiKey:g14aNzsNGLD0UOvi4wFXpTjzqfliK0JcfgpU0GnubyGUBzKi" --verbose https://api.xively.com/v2/feeds/779242912'</span>)
zigbee <span style="color: #333333;">=</span> ZigBee(serial_port, escaped <span style="color: #333333;">=</span> <span style="color: #007020;">True</span>, callback <span style="color: #333333;">=</span> print_data)
time<span style="color: #333333;">.</span>sleep(<span style="color: #0000dd; font-weight: bold;">200</span>)
zigbee<span style="color: #333333;">.</span>halt();
serial_port<span style="color: #333333;">.</span>close()
</pre>
</div>
<br />
<h2>
Part 2</h2>
<div>
<h3>
Introduction/About</h3>
</div>
<div style="text-align: justify;">
In this part we are going to experiment how light pollution affects Barcelona. About light
pollution, so far, there is little social conscience, even though it generates
numerous harmful consequences such as energy waste (and greenhouse gas
emissions resulting from their production). This also effects to human and
animals health and psychology, and, even some people could find not a priority,
it also damages astronomy activity and generates general loss of perception of
the Universe [1]. In fact, it has been shown in recent years that prolonged exposure
of the trees to artificial light can cause the trees out of control and produce
oxygen at night instead of absorbing carbon dioxide (which is one of the main
purposes of plant trees in the city). Hence, we would like to contribute a
little by making this project and divulge a bit the problematic.<br />
<br />
<div class="MsoNormal">
<span lang="EN-US">As obvious,
we consider that nowadays it is a must to have well illuminated streets at
nights, but it should be a must to provide this light in an adequate, accurate
and efficient way. As an example, all
sent laterally light, upward or to paces where it is not needed, does not provide
security and visibility and is a waste of energy (environment harm) and money
(we pay lot of taxes and the contributors have the right to make our
politicians to invest where necessary).<o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><br /></span></div>
<div class="MsoNormal">
<span lang="EN-US">Therefore,
we can get some experimental data and go to city hall and force them to:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
</div>
<ul>
<li><span lang="EN-US" style="text-indent: -18pt;">Install
streets bulbs horizontally. Use shielded lamps and luminous flux directed only
downward.</span></li>
<li><span lang="EN-US" style="font-family: Symbol; text-indent: -18pt;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"></span></span><span lang="EN-US" style="text-indent: -18pt;">Install
preferably sodium vapor lamps of low pressure and adequate power to use.</span></li>
<li><span style="font-family: Symbol;">T</span><span lang="EN-US" style="text-indent: -18pt;">urn
off Monumental and advertising lighting after midnight (there will not be any citizen
or tourist contemplating any monument).</span></li>
<li><span lang="EN-US" style="font-family: Symbol; text-indent: -18pt;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"></span></span><span lang="EN-US" style="text-indent: -18pt;">Prohibit
light or laser guns, and any projector that directs light toward the sky.</span></li>
<li><span lang="EN-US" style="font-family: Symbol; text-indent: -18pt;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"></span></span><span lang="EN-US" style="text-indent: -18pt;">Use
asymmetrical floodlights without inclination.</span></li>
<li><span style="font-family: Symbol;">R</span><span lang="EN-US" style="text-indent: -18pt;">educe
consumption in peak hours, at dawn, by brownout on the public or the selective
off lights.</span></li>
</ul>
<!--[if !supportLists]--><br />
<div class="MsoNormal">
<span lang="EN-US"><br /><a href="http://www.um.es/cieloscuro/documentos/divulgacion/cluminica_dos_palabras.pdf"></a><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">In short,
the only way to control light pollution is to reduce the amount of light sent
to heaven and reduce general consumption. We have to <b>respect</b> the night ecosystem.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="text-align: center;">
<br /><!--[endif]-->
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge7a5Mkw5t4D1C97TYvORIkeOrybp1oukzyS_XxY8E3m-Xvlb5iRP2Y_QlxtCaqup1Y8kBci-AF91qf60SBhMVIxddXwcvrinugN2JBBNvSJJYDim1J0Bo6zJ6Se_nhwWUH60sv6l-YeY/s1600/iberia.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge7a5Mkw5t4D1C97TYvORIkeOrybp1oukzyS_XxY8E3m-Xvlb5iRP2Y_QlxtCaqup1Y8kBci-AF91qf60SBhMVIxddXwcvrinugN2JBBNvSJJYDim1J0Bo6zJ6Se_nhwWUH60sv6l-YeY/s1600/iberia.jpg" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: Spanish night map [5]</td></tr>
</tbody></table>
<br /><!--[endif]--></div>
<h3 style="text-align: start;">
Getting experimental data</h3>
To do this, we are going to use the same procedure as before but we are also adding another sensor for backup. The information of this last sensor would be included on the same json file that we sended before in order to compare the uploaded values on the Xively platform.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The previous IO Sampling Rate was 400 ms, but now we have twice amount of data, so we set this field on 1000 ms, that means 0.5 packet per second (we have 2 sources).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
As we do not have enough sensor's precision, we are just going to compare the results obtained of measuring during the day and the night with the theoretical illuminance values, which are described below:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXE0H37aEDWLLSyXMlxP8MBSVWRxMgZM19Db5ErORpnDp0d2Mdji8wxBSqT4XGi5Q1G4TPo4ODIDeGYwS_Va8ef5z4v3yFDH9KQ1bbOg8BS6pfJus_258JAFQ1vFcbzGjSbagx0KQyAA8/s1600/table1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXE0H37aEDWLLSyXMlxP8MBSVWRxMgZM19Db5ErORpnDp0d2Mdji8wxBSqT4XGi5Q1G4TPo4ODIDeGYwS_Va8ef5z4v3yFDH9KQ1bbOg8BS6pfJus_258JAFQ1vFcbzGjSbagx0KQyAA8/s1600/table1.png" height="128" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Table 1: Illuminance values [4]</td></tr>
</tbody></table>
<div style="text-align: justify;">
We can see that the equivalence on day-night values must be 120000:1 in the extreme cases. In our case, we obtain measurements on a cloudy day with shadows, so our equivalence should be 500:1.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The following pictures shows the measurements done by both sensors, which are done with the data received from Xively using the following cmd command:<br />
<br />
<i>curl --request GET http://api.xively.com/v2/feeds/YOUR_FEED/datastreams/YOUR_CHANNEL.csv?key=YOUR_API_KEY&start=2013-12-22&duration=24hours&interval=900&limit=100&interval_type=discrete" > data.csv</i></div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinaBaZjY6fGjebLy-bswFhpekuNQ_P4FgFqEspu4OV5aoQov-hP_2lwUtuJ_Z4uUna91ba1wwOl5820LF3rsez70TW4T3S5CQVJULcVSl61VM3zuIFkFcpZ8elApmxdQ6A56GZc80JU04/s1600/adc0.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinaBaZjY6fGjebLy-bswFhpekuNQ_P4FgFqEspu4OV5aoQov-hP_2lwUtuJ_Z4uUna91ba1wwOl5820LF3rsez70TW4T3S5CQVJULcVSl61VM3zuIFkFcpZ8elApmxdQ6A56GZc80JU04/s1600/adc0.png" height="237" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: ADC-0 measurements</td></tr>
</tbody></table>
<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg500U-ej_kso5iBk3jgphep4OKdAtzTYcbE7BG7MpeQWY3EMgHCADwYpmJmitZCrMW_WLe04djiiPVb_vlB85Q4WHKy4ohyVNOqSuX_leS7Tw-ZEm49PBNRksPB2gH77V7jiqD5_MK9vo/s1600/adc1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg500U-ej_kso5iBk3jgphep4OKdAtzTYcbE7BG7MpeQWY3EMgHCADwYpmJmitZCrMW_WLe04djiiPVb_vlB85Q4WHKy4ohyVNOqSuX_leS7Tw-ZEm49PBNRksPB2gH77V7jiqD5_MK9vo/s1600/adc1.png" height="237" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: ADC-1 measurements</td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
We can observe that measured light on the night is not far enough from received during a shinny day, so we can observe an equivalence of 4:1, which is so far away of our assumptions.</div>
Note that both graphs show the same data from 23:00 of the first day to 0.00 of the second day. This is because Xively gaves us a graphic with the last token value (at 23:30) extended until the moment we requested the data.<br />
<h3>
Circuit Part 2</h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKMLtVWZfJRO00yo9lqVZfhJBk5mS2eE-9npEBXryfa7bVfxusD3PIw6aUDrcxI7LsDKTtjv1G_oh1543I_gFL7X5hDOq1j4FVFw3MdbQczFbrT3xuMjtY7vXj2SitGdMJ_imttROIyJ0/s1600/circuit2.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKMLtVWZfJRO00yo9lqVZfhJBk5mS2eE-9npEBXryfa7bVfxusD3PIw6aUDrcxI7LsDKTtjv1G_oh1543I_gFL7X5hDOq1j4FVFw3MdbQczFbrT3xuMjtY7vXj2SitGdMJ_imttROIyJ0/s1600/circuit2.jpg" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 7: Picture Circuit Part 2</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgotinyGHXw9h2IYbiiKylK4XhvPvKOMHUWSkcp4r_k_XPnDIbIEh-CPq06X8yGG2ux91CvUPyyYch6rhYVtMca9_Ux3fm4mbWlpMP3M47Sn1EyYIRf5qXUzp5QF-QMzjQHMTRugWactmM/s1600/circui2fritz.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgotinyGHXw9h2IYbiiKylK4XhvPvKOMHUWSkcp4r_k_XPnDIbIEh-CPq06X8yGG2ux91CvUPyyYch6rhYVtMca9_Ux3fm4mbWlpMP3M47Sn1EyYIRf5qXUzp5QF-QMzjQHMTRugWactmM/s1600/circui2fritz.jpg" height="236" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 8: Circuit Part 2</td></tr>
</tbody></table>
<h3>
Code Part 2 [3]</h3>
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;"># Derived from code by Alejandro Andreu</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">commands</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">json</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">time</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">os</span>
<span style="color: #008800; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">serial</span> <span style="color: #008800; font-weight: bold;">import</span> SerialException
<span style="color: #008800; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #008800; font-weight: bold;">import</span> ZigBee
<span style="color: #008800; font-weight: bold;">print</span> <span style="background-color: #fff0f0;">'Asynchronously printing data from remote XBee'</span>
serial_port <span style="color: #333333;">=</span> serial<span style="color: #333333;">.</span>Serial(<span style="background-color: #fff0f0;">'COM5'</span>, <span style="color: #0000dd; font-weight: bold;">9600</span>)
<span style="color: #008800; font-weight: bold;">def</span> <span style="color: #0066bb; font-weight: bold;">print_data</span>(data):
<span style="color: #dd4422;">"""</span>
<span style="color: #dd4422;"> This method is called whenever data is received.</span>
<span style="color: #dd4422;"> Its only argument is the data within the frame.</span>
<span style="color: #dd4422;"> """</span>
<span style="color: #008800; font-weight: bold;">print</span> data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>keys()[<span style="color: #0000dd; font-weight: bold;">0</span>]
<span style="color: #008800; font-weight: bold;">print</span> data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>values()[<span style="color: #0000dd; font-weight: bold;">0</span>]
<span style="color: #008800; font-weight: bold;">print</span> data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>keys()[<span style="color: #0000dd; font-weight: bold;">1</span>]
<span style="color: #008800; font-weight: bold;">print</span> data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>values()[<span style="color: #0000dd; font-weight: bold;">1</span>]
<span style="color: #888888;"># Create a JSON file and fill it with the received samples</span>
json_data <span style="color: #333333;">=</span> {}
json_data[<span style="background-color: #fff0f0;">'version'</span>] <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">'0.2'</span>
json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">=</span> ()
json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">=</span> json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">+</span> ({<span style="background-color: #fff0f0;">'id'</span>: data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>keys()[<span style="color: #0000dd; font-weight: bold;">0</span>], <span style="background-color: #fff0f0;">'current_value'</span>: <span style="color: #007020;">str</span>(data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>values()[<span style="color: #0000dd; font-weight: bold;">0</span>])},)
<span style="color: #008800; font-weight: bold;">with</span> <span style="color: #007020;">open</span>(<span style="background-color: #fff0f0;">'Xbee2Xively.json'</span>, mode<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">'w'</span>) <span style="color: #008800; font-weight: bold;">as</span> f:
json<span style="color: #333333;">.</span>dump(json_data, f, indent <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">4</span>)
<span style="color: #888888;"># Add data from adc-1 pin</span>
json_data1 <span style="color: #333333;">=</span> {}
json_data1[<span style="background-color: #fff0f0;">'version'</span>] <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">'0.2'</span>
json_data1[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">=</span> ()
json_data1[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">=</span> json_data[<span style="background-color: #fff0f0;">'datastreams'</span>] <span style="color: #333333;">+</span> ({<span style="background-color: #fff0f0;">'id'</span>: data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>keys()[<span style="color: #0000dd; font-weight: bold;">1</span>], <span style="background-color: #fff0f0;">'current_value'</span>: <span style="color: #007020;">str</span>(data[<span style="background-color: #fff0f0;">'samples'</span>][<span style="color: #0000dd; font-weight: bold;">0</span>]<span style="color: #333333;">.</span>values()[<span style="color: #0000dd; font-weight: bold;">1</span>])},)
<span style="color: #008800; font-weight: bold;">with</span> <span style="color: #007020;">open</span>(<span style="background-color: #fff0f0;">'Xbee2Xively.json'</span>, mode<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">'w'</span>) <span style="color: #008800; font-weight: bold;">as</span> f:
json<span style="color: #333333;">.</span>dump(json_data1, f, indent <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">4</span>)
zigbee <span style="color: #333333;">=</span> ZigBee(serial_port, callback <span style="color: #333333;">=</span> print_data)
<span style="color: #008800; font-weight: bold;">while</span> <span style="color: #007020;">True</span>:
<span style="color: #008800; font-weight: bold;">try</span>:
time<span style="color: #333333;">.</span>sleep(<span style="color: #0000dd; font-weight: bold;">500</span>)
<span style="color: #008800; font-weight: bold;">except</span> <span style="color: red; font-weight: bold;">KeyboardInterrupt</span>:
<span style="color: #008800; font-weight: bold;">break</span>
zigbee<span style="color: #333333;">.</span>halt();
serial_port<span style="color: #333333;">.</span>close()
</pre>
</div>
<br />
<h2>
<span style="font-size: large;">References</span></h2>
<span style="font-size: small;">[1] Python software and libraries, retrieved from: <a href="https://www.python.org/">https://www.python.org/</a></span><br />
<span style="font-size: small;">[2] XBee library for Arduino, retrieved from: <a href="https://code.google.com/p/xbee-arduino/">https://code.google.com/p/xbee-arduino/ </a></span><br />
[3] Version of the code "upload_to_xively.py" from Jaume Barceló and Luis Sanabria<br />
<span lang="EN-US">[4] Light pollution information from "Universidad de Murcia" report, retrieved from: <a href="http://www.um.es/cieloscuro/documentos/divulgacion/cluminica_dos_palabras.pdf">http://www.um.es/cieloscuro/documentos/divulgacion/cluminica_dos_palabras.pdf</a></span> <br />
[5] Image from "celfosc", retrieved from: <a href="http://www.celfosc.org/">http://www.celfosc.org/</a><a href="http://www.celfosc.org/"></a><br />
[6] Illuminance information from Wikipedia, retrieved from: <a href="http://en.wikipedia.org/wiki/Daylight">http://en.wikipedia.org/wiki/Daylight</a> <br />
<br />Anonymoushttp://www.blogger.com/profile/13363214092472820759noreply@blogger.com0tag:blogger.com,1999:blog-3219653273674708900.post-85458377951596813042014-05-24T19:51:00.004+02:002014-05-24T19:51:39.483+02:00Practice - Activating a Fan Depending on the Temperature<h2>
Description</h2>
<div style="text-align: justify;">
In this lab we have been experimenting with the comunication between an XBee connected to a PC and another XBee that is taking signals from a sensor to send them (this signals are caught using the DIO ports of the XBee explorer). The information that comes from the sensors is sent from an XBee to the other; the information received by the second XBee will be read serially with a computer using a Python program, that will process the read (e.g. showing it or generating a response). Both XBees work in API mode as in the last practice (Morse Code).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
In the first part we will show the messages sent by an XBee which is connected to a LDR, whereas in the second part a motor will be turned on when the temperature reaches a defined value (e.g. 30ºC).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The material we will need is the following:</div>
<div style="text-align: justify;">
<ul style="text-align: start;">
<li>Hookup wires.</li>
<li>1x Arduino board.</li>
<li>1x USB A-to-B cable for the Arduino.</li>
<li>1x XBee radio configured as ZigBee Coordinator API.</li>
<li>1x XBee radio configured as ZigBee Router API.</li>
<li>2x USB cable for the XBee.</li>
<li>2x XBee Explorer.</li>
<li>1x Breakout board.</li>
<li>1x Motor.</li>
<li>1x Temperature Sensor (LM35).</li>
<li>1x Transistor.</li>
<li>1x LDR (Light Dependent Resistor).</li>
</ul>
<div style="text-align: justify;">
Moreover, again we leave here some of the software you will need (the same as in the previous practice). </div>
<div style="text-align: left;">
<ul>
<li>XCTU.</li>
<li><a href="https://www.python.org/downloads/" target="_blank">Python 2.7</a>.</li>
<li><a href="https://pypi.python.org/pypi/XBee/2.1.0" target="_blank">XBee library for Python</a>.</li>
<li><a href="https://pypi.python.org/pypi/pyserial" target="_blank">Pyserial library for Python</a> (<a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/" target="_blank">mirror</a>).</li>
<li><a href="https://drive.google.com/folderview?id=0B_wZzfmSl2b0NmdoVzdNR1F4R00&usp=sharing" target="_blank">XBee library for Arduino</a>.</li>
</ul>
<div>
<span style="text-align: justify;">The installation is explained in the previous practice too, review it if you have not seen it yet.</span></div>
<h2>
<span style="text-align: justify;">Part 1 - Warm Up</span></h2>
</div>
</div>
<div style="text-align: justify;">
As a warm up we did the activity explained in the course guide [1]. It consists in a circuit formed by an LDR, whose analogic values are sent to an XBee through one of its pins; this information is received at another XBee which is plugged to a computer. This computer executes a Python program which reads the messages when received and shows them through the screen. Note that any Arduino is being used here.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhERYH56U0ywmUWqd-vEq0GychEFjAdymR7Nxn_tmzTvF-LXHm76wy-m38KyylQ55VYieb6L1WeNwgT2g07q3HGikxrt2WT17AcI-w_iq1azhUxNgsFVPKngYEh-G-IT4Wu1QVR_ProSQo/s1600/warm-up.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhERYH56U0ywmUWqd-vEq0GychEFjAdymR7Nxn_tmzTvF-LXHm76wy-m38KyylQ55VYieb6L1WeNwgT2g07q3HGikxrt2WT17AcI-w_iq1azhUxNgsFVPKngYEh-G-IT4Wu1QVR_ProSQo/s1600/warm-up.png" height="116" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Setup of the XBee that sends LDR state to the computer connected XBee</td></tr>
</tbody></table>
<br /></div>
<div style="text-align: justify;">
In the following screenshots you can see the messages that are sent. You can see that two of them in a red rectangle: the first one represents a measure with the room plenty of light, and the second one without light.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij1A1J-V007OXpGqYtpkZ7WKKnqobaxcSuFM7W-KTX96ZLvJSj2IXC5mZXolxKnTaWI38zgyHkRaZb1MCcQFiVVfdw53VAQNAl-mLVEKMO7ZSZSBatpGXYEtf0Nlofsr8YPW_jUXYEktE/s1600/receiving+values+from+sensor.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij1A1J-V007OXpGqYtpkZ7WKKnqobaxcSuFM7W-KTX96ZLvJSj2IXC5mZXolxKnTaWI38zgyHkRaZb1MCcQFiVVfdw53VAQNAl-mLVEKMO7ZSZSBatpGXYEtf0Nlofsr8YPW_jUXYEktE/s1600/receiving+values+from+sensor.jpg" height="200" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Receiving values from XBee</td></tr>
</tbody></table>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
This task can be done in two different ways: sinchronously and asinchronously. The code is attached below.<br />
<br /></div>
<h3 style="text-align: justify;">
Synchronous code</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #007020; font-weight: bold;">import</span> ZigBee
<span style="color: #007020; font-weight: bold;">print</span> <span style="color: #4070a0;">'Printing data from remote XBee'</span>
serial_port <span style="color: #666666;">=</span> serial<span style="color: #666666;">.</span>Serial(<span style="color: #4070a0;">'COM4'</span>, <span style="color: #40a070;">9600</span>)
zigbee <span style="color: #666666;">=</span> ZigBee(serial_port, escape<span style="color: #666666;">=</span><span style="color: #007020;">True</span>)
<span style="color: #007020; font-weight: bold;">while</span> <span style="color: #007020;">True</span>:
<span style="color: #007020; font-weight: bold;">try</span>:
<span style="color: #007020; font-weight: bold;">print</span> zigbee<span style="color: #666666;">.</span>wait_read_frame()
<span style="color: #007020; font-weight: bold;">except</span> <span style="color: #007020;">KeyboardInterrupt</span>:
<span style="color: #007020; font-weight: bold;">break</span>
zigbee<span style="color: #666666;">.</span>halt()
serial_port<span style="color: #666666;">.</span>close()</pre>
</td></tr>
</tbody></table>
</div>
<h3 style="text-align: justify;">
</h3>
<h3 style="text-align: justify;">
Asynchronous code</h3>
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">time</span>
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #007020; font-weight: bold;">import</span> ZigBee
<span style="color: #007020; font-weight: bold;">print</span> <span style="color: #4070a0;">'Asynchronously printing data from remote XBee'</span>
serial_port <span style="color: #666666;">=</span> serial<span style="color: #666666;">.</span>Serial(<span style="color: #4070a0;">'/dev/ttyUSB0'</span>, <span style="color: #40a070;">9600</span>)
<span style="color: #007020; font-weight: bold;">def</span> <span style="color: #06287e;">print_data</span>(data):
<span style="color: #4070a0; font-style: italic;">"""</span>
<span style="color: #4070a0; font-style: italic;"> This method is called whenever data is received.</span>
<span style="color: #4070a0; font-style: italic;"> Its only argument is the data within the frame.</span>
<span style="color: #4070a0; font-style: italic;"> """</span>
<span style="color: #007020; font-weight: bold;">print</span> data[<span style="color: #4070a0;">'samples'</span>]
zigbee <span style="color: #666666;">=</span> ZigBee(serial_port, escaped<span style="color: #666666;">=</span><span style="color: #007020;">True</span>, callback <span style="color: #666666;">=</span> print_data)
<span style="color: #007020; font-weight: bold;">while</span> <span style="color: #007020;">True</span>:
<span style="color: #007020; font-weight: bold;">try</span>:
time<span style="color: #666666;">.</span>sleep(<span style="color: #40a070;">0.001</span>)
<span style="color: #007020; font-weight: bold;">except</span> <span style="color: #007020;">KeyboardInterrupt</span>:
<span style="color: #007020; font-weight: bold;">break</span>
zigbee<span style="color: #666666;">.</span>halt()
serial_port<span style="color: #666666;">.</span>close()</pre>
</td></tr>
</tbody></table>
</div>
<br />
<h2>
Part 2 - Activating a Fan</h2>
<div style="text-align: justify;">
On this extra project part, we are going to design a system that will allow us to refrigerate a server's room (e.g. a CDN). The heat production produced by the servers on data center is one of the main problems that administrators have to face. The heat excess in servers room negatively impacts the equipment performance and shortens their useful life. Also, if we are dealing with critical systems like nuclear centers, having a real-time control on the temperature is a priority. So, let us try to develop a system that automatically refrigerates the servers when needed, making the administrators could invest their time in other useful tasks.</div>
<br />
On the <b>room's side</b> we need:<br />
<br />
<ul>
<li>1x Temperature Sensor.</li>
<li>1x Fan (motor).</li>
<li>1x Xbee.</li>
<li>1x Arduino Uno.</li>
<li>A way to feed the Arduino (a normal PC will be enough).</li>
</ul>
<br />
On the <b>control center</b> we will need:<br />
<br />
<ul>
<li>1x Xbee</li>
<li>1x PC running python code</li>
</ul>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFvHJOUtPfqofCbPhSi2wDmLrW88HbOAkblK8aZtukXXT7ChwBVhxYBqSan7yd8pzn-sW9H5XH_fB3ax8uEtpJuRhx3XUEAc9VNCqQWuJekqOxAQ4nayd4wf-cXfCB3kFZlU_Rn7g2eXM/s1600/Imagen1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFvHJOUtPfqofCbPhSi2wDmLrW88HbOAkblK8aZtukXXT7ChwBVhxYBqSan7yd8pzn-sW9H5XH_fB3ax8uEtpJuRhx3XUEAc9VNCqQWuJekqOxAQ4nayd4wf-cXfCB3kFZlU_Rn7g2eXM/s1600/Imagen1.png" height="185" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: System overview</td></tr>
</tbody></table>
<div style="text-align: justify;">
As shown above, the two main parts of the whole system communicate each other by using a pair of Xbee modules. On the server side, the Xbee will send the data captured by the temperature sensor. The Xbee situated on the control center side will get this data and forward it to the PC, which runs a python code that is able to decode the received data and compare it with a predefined temperature threshold. When the threshold is overcome, the code will generate an alert packet advising the Arduino on the servers side to activate the ventilator (motor). This packet, naturally, will be forwarded from the control center Xbee to the server side Xbee.</div>
<br />
<div style="text-align: justify;">
It is important to note that, because of collisions high probability, the Xbee on the servers side will transmit the temperature data periodically. That is, sending a temperature data packet, waiting for possible motor activation packet, and then, if no packet is received, sending the next captured temperature.<br />
<br />
Respect to the code, we have to take into account that the LM35 temperature sensor does not provide the temperature directly. We have found in [2] that a formula has to be computed in order to get the temperature depending on the voltage; since our voltage is 3.3 V, the formula we get is: <span style="font-family: Courier New, Courier, monospace;">3.3 * 100 * readValue / 1024</span>. However, in the code there is this option and another one that we have manually computed by knowing the real temperature of the room in which we were doing the lab: <span style="font-family: Courier New, Courier, monospace;">readValue * 18.0 / 212.0</span>.<br />
<h3>
Phographs</h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFdAlriO-Cm6UmvvpesB5-X9kDphMzRxih_PJQVo4iysx4ABFTxz1LmiVDO1bzFHgAavrL8iDaNx_UOnTA-uFbi8XMbF7_D-FmJfHG1oZzF6T_Duq3osHBFmqDhkP8YT5M0kRW1LVnas4/s1600/final_image2.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFdAlriO-Cm6UmvvpesB5-X9kDphMzRxih_PJQVo4iysx4ABFTxz1LmiVDO1bzFHgAavrL8iDaNx_UOnTA-uFbi8XMbF7_D-FmJfHG1oZzF6T_Duq3osHBFmqDhkP8YT5M0kRW1LVnas4/s1600/final_image2.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: Photograph of the whole system</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg78GK4nlqeJCFg6OByLjvTKoCaR96VzQ5qhrFipc7OJB8ipRz0ICmgGZ0seKhFGXBWSPsP9JLCLA8DlN5vgCYJZ0OpMhHC_6TaKB8SyasPolPVbTkSkXhyphenhyphen05TXyscs8bSzkNSPMbtrjPY/s1600/final_image.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg78GK4nlqeJCFg6OByLjvTKoCaR96VzQ5qhrFipc7OJB8ipRz0ICmgGZ0seKhFGXBWSPsP9JLCLA8DlN5vgCYJZ0OpMhHC_6TaKB8SyasPolPVbTkSkXhyphenhyphen05TXyscs8bSzkNSPMbtrjPY/s1600/final_image.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: Photograph of the sensor (temperature) and actuator (motor) system</td></tr>
</tbody></table>
<div>
<br /></div>
<h3>
Final setup image</h3>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCPP7qtPQahSdmGdWiURUH3N7DcDHtKPw8gi6impMrhSlmZxSH7Uuq7VFG7U91-xVL6cErLOEhajNTvfDTR0bsjHIwq_dMDy96aO8qkz2PBFB110Rh150y6EQHg_irLX4lfTgENRExFzQ/s1600/MotorBadge_bb.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCPP7qtPQahSdmGdWiURUH3N7DcDHtKPw8gi6impMrhSlmZxSH7Uuq7VFG7U91-xVL6cErLOEhajNTvfDTR0bsjHIwq_dMDy96aO8qkz2PBFB110Rh150y6EQHg_irLX4lfTgENRExFzQ/s1600/MotorBadge_bb.png" height="256" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: Final setup of the temperature based fan</td></tr>
</tbody></table>
<h3>
Video</h3>
<div style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/CrcvOAjoEak" width="560"></iframe>
</div>
<div>
<div style="text-align: center;">
<br /></div>
</div>
<h3 style="text-align: left;">
Arduino Code</h3>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">// named constants for the switch and motor pins</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> STATE_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>; <span style="color: #60a0b0; font-style: italic;">// the number of the State pin</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> FAN_PIN <span style="color: #666666;">=</span> <span style="color: #40a070;">5</span>; <span style="color: #60a0b0; font-style: italic;">// the number of the motor pin</span>
<span style="color: #902000;">int</span> DETstate <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; <span style="color: #60a0b0; font-style: italic;">// variable for reading the state detector status</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>() {
<span style="color: #60a0b0; font-style: italic;">// initialize the motor pin as an output:</span>
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
<span style="color: #60a0b0; font-style: italic;">// initialize the switch pin as an input:</span>
pinMode(DETstate, INPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>(){
<span style="color: #60a0b0; font-style: italic;">// read the state of the Threshold value:</span>
DETstate <span style="color: #666666;">=</span> digitalRead(STATE_PIN);
<span style="color: #60a0b0; font-style: italic;">// check if the activationg motor order is rx</span>
<span style="color: #007020; font-weight: bold;">if</span> (DETstate <span style="color: #666666;">==</span> HIGH) {
<span style="color: #60a0b0; font-style: italic;">// turn fan on: </span>
digitalWrite(FAN_PIN, HIGH);
}
<span style="color: #007020; font-weight: bold;">else</span> {
<span style="color: #60a0b0; font-style: italic;">// turn fan off:</span>
digitalWrite(FAN_PIN, LOW);
}
delay(<span style="color: #40a070;">10</span>);
}
</pre>
</td></tr>
</tbody></table>
</div>
<div>
<br /></div>
<h3>
Python Code (XBee connected to computer)</h3>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #007020; font-weight: bold;">import</span> ZigBee
<span style="color: #007020; font-weight: bold;">def</span> <span style="color: #06287e;">getTemperatureFromData</span>(data):
temperature <span style="color: #666666;">=</span> data[<span style="color: #4070a0;">'samples'</span>][<span style="color: #40a070;">0</span>][<span style="color: #4070a0;">'adc-0'</span>]
<span style="color: #60a0b0; font-style: italic;"># temperature = temperature*18.0/212.0</span>
temperature <span style="color: #666666;">=</span> <span style="color: #40a070;">3.3</span> <span style="color: #666666;">*</span> temperature <span style="color: #666666;">*</span> <span style="color: #40a070;">100.0</span> <span style="color: #666666;">/</span> <span style="color: #40a070;">1024.0</span>
<span style="color: #007020; font-weight: bold;">return</span> temperature
<span style="color: #007020; font-weight: bold;">def</span> <span style="color: #06287e;">turnFanOn</span>(xbee):
<span style="color: #007020; font-weight: bold;">print</span> <span style="color: #4070a0;">'Threshold exceeded! Activating Fans'</span>
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'remote_at'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x13\xA2\x00\x40\x8B\x96\x2E</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
options<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x02</span><span style="color: #4070a0;">'</span>,
command<span style="color: #666666;">=</span><span style="color: #4070a0;">'D1'</span>,
parameter<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x05</span><span style="color: #4070a0;">'</span>)
<span style="color: #007020; font-weight: bold;">def</span> <span style="color: #06287e;">turnFanOff</span>(xbee):
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'remote_at'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x13\xA2\x00\x40\x8B\x96\x2E</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
options<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x02</span><span style="color: #4070a0;">'</span>,
command<span style="color: #666666;">=</span><span style="color: #4070a0;">'D1'</span>,
parameter<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x04</span><span style="color: #4070a0;">'</span>)
<span style="color: #007020; font-weight: bold;">def</span> <span style="color: #06287e;">main</span>():
<span style="color: #007020; font-weight: bold;">print</span> <span style="color: #4070a0;">'Receiving temperature values...'</span>
serialPort <span style="color: #666666;">=</span> serial<span style="color: #666666;">.</span>Serial(<span style="color: #4070a0;">'COM4'</span>, <span style="color: #40a070;">9600</span>)
xbee <span style="color: #666666;">=</span> ZigBee(serialPort, escaped<span style="color: #666666;">=</span><span style="color: #007020;">True</span>)
<span style="color: #007020; font-weight: bold;">while</span> <span style="color: #007020;">True</span>:
<span style="color: #007020; font-weight: bold;">try</span>:
data <span style="color: #666666;">=</span> xbee<span style="color: #666666;">.</span>wait_read_frame()
<span style="color: #007020; font-weight: bold;">if</span> data<span style="color: #666666;">.</span>keys()<span style="color: #666666;">.</span>count(<span style="color: #4070a0;">'samples'</span>) <span style="color: #666666;">!=</span> <span style="color: #40a070;">0</span>:
temperature <span style="color: #666666;">=</span> getTemperatureFromData(data)
<span style="color: #007020; font-weight: bold;">print</span> temperature
<span style="color: #007020; font-weight: bold;">if</span> (temperature <span style="color: #666666;">></span> <span style="color: #40a070;">21.0</span>):
turnFanOn(xbee)
<span style="color: #007020; font-weight: bold;">else</span>:
turnFanOff(xbee)
<span style="color: #007020; font-weight: bold;">except</span> <span style="color: #007020;">KeyboardInterrupt</span>:
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'remote_at'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x13\xA2\x00\x40\x8B\x96\x2E</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
options<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x02</span><span style="color: #4070a0;">'</span>,
command<span style="color: #666666;">=</span><span style="color: #4070a0;">'D1'</span>,
parameter<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x04</span><span style="color: #4070a0;">'</span>)
<span style="color: #007020; font-weight: bold;">break</span>
xbee<span style="color: #666666;">.</span>halt()
serialPort<span style="color: #666666;">.</span>close()
<span style="color: #007020; font-weight: bold;">if</span> __name__ <span style="color: #666666;">==</span> <span style="color: #4070a0;">'__main__'</span>:
main()
<span style="color: #007020; font-weight: bold;">else</span>:
<span style="color: #007020; font-weight: bold;">pass</span>
</pre>
</td></tr>
</tbody></table>
</div>
<div>
<br /></div>
</div>
<h2>
References</h2>
<div>
[1] Course guide: <a href="http://www.handsonwsn.org/guide.pdf">http://www.handsonwsn.org/guide.pdf</a><br />
[2] LM35 Higher Resolution: <a href="http://playground.arduino.cc/Main/LM35HigherResolution">http://playground.arduino.cc/Main/LM35HigherResolution</a></div>
[3] Pictures:<br />
<br />
<ul>
<li>Server rag: <a href="http://tallerpractico.com/wp-content/uploads/2014/02/server_multiple.png">http://tallerpractico.com/wp-content/uploads/2014/02/server_multiple.png</a></li>
<li>Temperature sensor: <a href="http://www.senseaware.com/wp-content/themes/senseaware/images/img-icon2-large.gif">http://www.senseaware.com/wp-content/themes/senseaware/images/img-icon2-large.gif</a></li>
<li>Fan: <a href="https://cdn1.iconfinder.com/data/icons/STROKE/computer_gadgets/png/400/cpu_fan.png">https://cdn1.iconfinder.com/data/icons/STROKE/computer_gadgets/png/400/cpu_fan.png</a></li>
<li>PC: <a href="http://iconizer.net/files/Vista_Style_Hardware_&_Devices/orig/PortableComputer.png">http://iconizer.net/files/Vista_Style_Hardware_&_Devices/orig/PortableComputer.png</a></li>
<li>Xbee: <a href="http://www.robosoftsystems.co.in/roboshop/media/catalog/product/cache/1/image/500x500/9df78eab33525d08d6e5fb8d27136e95/x/b/xbeeadaptor2.jpg">http://www.robosoftsystems.co.in/roboshop/media/catalog/product/cache/1/image/500x500/9df78eab33525d08d6e5fb8d27136e95/x/b/xbeeadaptor2.jpg</a></li>
<li>Python logo: <a href="https://www.python.org/static/community_logos/python-logo-master-v3-TM.png">https://www.python.org/static/community_logos/python-logo-master-v3-TM.png</a></li>
<li>Android Uno: <a href="http://www.sacosta.org/rfid_castellano/logo_arduino.jpg">http://www.sacosta.org/rfid_castellano/logo_arduino.jpg</a></li>
</ul>
Anonymoushttp://www.blogger.com/profile/02012194402490053567noreply@blogger.com0tag:blogger.com,1999:blog-3219653273674708900.post-26043483095574744502014-05-14T21:53:00.003+02:002014-05-16T12:15:34.867+02:00Practice - Morse Code Player<div style="text-align: justify;">
<h2>
Description</h2>
This time we are going to perform a system that executes specific tasks according to the communication between two XBee. As usual, we are dividing this practicaL exercise into two parts. The first one will show you how to configurate the XBee modules and the Arduino in order to blink a LED at the receiver part. The second part extends that functionality, so we are going to create a Morse code player, which reproduces the characters introduced on the sender side into Morse. To coordinate all this stuff we are going to use Python code with XBee libraries on the sender side.</div>
<div style="text-align: justify;">
<br /></div>
So, the material that we are going to need is exposed below:<br />
<ul>
<li>Hookup wires.</li>
<li>1 x Arduino board.</li>
<li>1 x USB A-to-B cable for the Arduino.</li>
<li>1 x XBee radio configured as ZigBee Coordinator API.</li>
<li>1 x XBee radio configured as ZigBee Router API.</li>
<li>1 x Breakout board.</li>
<li>1 x USB cable for the XBee.</li>
<li>1 x LED.</li>
<li>1 x Buzzer for output.</li>
</ul>
The software that you will need to support all this is:<br />
<ul>
<li>XCTU.</li>
<li>Python [1]. <a href="https://www.python.org/downloads/">Download here</a>.</li>
<li>XBee library for Python [1]. <a href="https://pypi.python.org/pypi/XBee/2.1.0">Download here</a>.</li>
<li>Pyserial library for Python [1]. <a href="https://pypi.python.org/pypi/pyserial">Download here</a> or <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/">Download here 2</a>.</li>
<li>XBee library for Arduino [2]. <a href="https://drive.google.com/folderview?id=0B_wZzfmSl2b0NmdoVzdNR1F4R00&usp=sharing">Download here</a> .</li>
</ul>
<div>
Remember you can find the code used in the <a href="http://getting-into-arduino.blogspot.com.es/p/code.html" target="_blank">code section</a>.</div>
<ul>
</ul>
<h2>
<b><span style="font-size: large;">Part 1</span></b></h2>
<div style="text-align: justify;">
The first thing we must do is to install and configure python and the needed libraries for our purpose. After downloading the Python we should see a wizard like the shown on the image below:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7eNtF-22c4s85GEbbFwe_yLa3QAvBG5xV6106f3M8nzyBxQVSsZAYVM3Se9GmVEAq_gUPJJTrZ-Qqexb-BGPwQ4NzQ4cxk2snJlG97W70jS4R59wGwgR0PvKoDJv3HJhJPy479jvR5M/s1600/wiz1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj7eNtF-22c4s85GEbbFwe_yLa3QAvBG5xV6106f3M8nzyBxQVSsZAYVM3Se9GmVEAq_gUPJJTrZ-Qqexb-BGPwQ4NzQ4cxk2snJlG97W70jS4R59wGwgR0PvKoDJv3HJhJPy479jvR5M/s1600/wiz1.png" height="261" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Python Wizard</td></tr>
</tbody></table>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFX1PMGC6Sfhp5UzZn34UsAeo4uzeN8hSOEBL3g6rKVg2yi7Yp-HjqWHAcIGGEPqKOc8csgrFDFpO3ayHf7dlXNrKy8-ksrpp8fshsTsODF7HaooFb3UrrgOLLQh2AwdD_TmXU5V1WkSE/s1600/wiz1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<div style="text-align: justify;">
You only have to follow the steps and install it. If you want to execute Python programs with "cmd" tool you should add the directory of the Python to the "path" variable (System > Advanced system settings > Environment variables > System variables > Path ). After doing that you should see the Python version at cmd by typing "python":</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMcVkscGuIwXrgEGGuesDh3jxC2BkCUJZUHmzOBRZu4tEu0P3gdOJwVaU2iJUuJn63ysmyHhFzNek2ulSzbBlqPCfCut7WZou4BPPwNKJGBVQcn1opyVF8WwgyC-RZEXElPgYsDpkuBBY/s1600/cmd+2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMcVkscGuIwXrgEGGuesDh3jxC2BkCUJZUHmzOBRZu4tEu0P3gdOJwVaU2iJUuJn63ysmyHhFzNek2ulSzbBlqPCfCut7WZou4BPPwNKJGBVQcn1opyVF8WwgyC-RZEXElPgYsDpkuBBY/s1600/cmd+2.png" height="202" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Python installation and configuration check</td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
To complete the configuration of the system we must install the XBee and PySerial libraries by the following way:</div>
<ul>
<li style="text-align: justify;">Download the XBee library from the given link (if you have problems with it you can go to the 2nd link and pick the version that suits better to you computer).</li>
</ul>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY346vImJIjyl_ssp_DuGixqz6HOJ4DDu5UHqz0Ypqwm3JaTM0MlQ4V1ImnSGQh-HrlqU12dKznpP77855mDje96jQvAntULgZHAkdK8aKnVfot4PoKAfd7z9o-DOS_7SNuZpsNf5ugBI/s1600/wiz0.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY346vImJIjyl_ssp_DuGixqz6HOJ4DDu5UHqz0Ypqwm3JaTM0MlQ4V1ImnSGQh-HrlqU12dKznpP77855mDje96jQvAntULgZHAkdK8aKnVfot4PoKAfd7z9o-DOS_7SNuZpsNf5ugBI/s1600/wiz0.png" height="237" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: XBee library download</td></tr>
</tbody></table>
<ul>
<li style="text-align: justify;"> Install it by typing "python setup.py install" on the directory where you stored it (C:\Users\User\Destop\XBee-2.1.0 in our case):</li>
</ul>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq5H1nz6739JSVksn7nu_pA6UIFBMGU4_dkcsZ49FL7AAJyku_AbZuvzpSvnFRIXqOb8xLI0uW9G1TeXdFeKjlBxgWj5I-IFQwQ3YM4lHAVusUHz6w9ysjMdwvktqf5rT6rqgcD9_IRT4/s1600/cmd+1.png" height="202" style="margin-left: auto; margin-right: auto;" width="400" /></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: XBee library install</td></tr>
</tbody></table>
<ul>
<li style="text-align: justify;">Download the "pyserial" library and to exactly the same to install it.</li>
</ul>
<div style="text-align: justify;">
Now we have the software properly installed, but the last thing we must do before building the circuit and implementing the code is to set the XBee modules on the API mode. For the coordinator we set it as "ZigBee Coordinator API":</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaRs32RLzWqEMWarh7_IN7dbU648y3oPhcCMhb3OsB6lu43N-EX1dapoucUuTF1vAuyaIZoyPzYR3Voh64fCaLxo8jxp5Z0_xhYyDcnAHrDtKhnNeQ-FMoJRUVYPgEWyvPyWtPCHNVA2g/s1600/wiz.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaRs32RLzWqEMWarh7_IN7dbU648y3oPhcCMhb3OsB6lu43N-EX1dapoucUuTF1vAuyaIZoyPzYR3Voh64fCaLxo8jxp5Z0_xhYyDcnAHrDtKhnNeQ-FMoJRUVYPgEWyvPyWtPCHNVA2g/s1600/wiz.png" height="305" title="" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: XBee mode</td></tr>
</tbody></table>
<div style="text-align: justify;">
We do the same for the router ("ZigBee Router API"). We also have to enable the API mode at both XBee by setting the "AP" field with number 2.</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8tkpmaojDE8WMOUxMD6d-qCDk_zsI06eNAB9SjSd8NYqHSWwJWgA4wNAHZ8AS93aF6hgcSr-k5E2l0j4_zdO7z_MwjX6RjjGx6BSOVNwOyZG_vMzxIrQ1Ld4meI5S48fTSpNI4sgRk6E/s1600/xctu1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8tkpmaojDE8WMOUxMD6d-qCDk_zsI06eNAB9SjSd8NYqHSWwJWgA4wNAHZ8AS93aF6hgcSr-k5E2l0j4_zdO7z_MwjX6RjjGx6BSOVNwOyZG_vMzxIrQ1Ld4meI5S48fTSpNI4sgRk6E/s1600/xctu1.png" height="300" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: Enable API mode</td><td class="tr-caption" style="text-align: center;"><br /></td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
<span style="font-size: small;">The </span><span style="font-size: large;"><span style="font-size: small;">circuit that we have to build by now is quite simple: a LED connected to the </span></span><span style="font-size: small;">D1 port of the XBee (note that in this case we only use the arduino to feed the XBee):</span></div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaJCCHILpNDudgzFFagGGnfvfDtQkZJ-vDXroCaGkHBQW0MbYVXZfkPKNQUj5iZJFG8oXPk7knYxNMZGPZlDEuyb9kCfLjjjsZHa99UOtQy1DYl2ukU4MOlHczPbMdhDZ0B9BuhsN1iNs/s1600/part1.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaJCCHILpNDudgzFFagGGnfvfDtQkZJ-vDXroCaGkHBQW0MbYVXZfkPKNQUj5iZJFG8oXPk7knYxNMZGPZlDEuyb9kCfLjjjsZHa99UOtQy1DYl2ukU4MOlHczPbMdhDZ0B9BuhsN1iNs/s1600/part1.jpg" height="248" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 7: Circuit part 1</td></tr>
</tbody></table>
<div style="text-align: justify;">
<span style="font-size: large;"><span style="font-size: small;">The code that we must implement on the sender side will send packets to the receiver XBee (we can also send broadcast packets on the same PAN ID with dest_addr_long='\x00\x00\x00\x00\x00\x00\xFF\xFF') that will enable and disable the D1 port in order to let the current path and activate the LED. We also have to take into account in which port we have placed the XBee (COM5 in our case).</span></span></div>
<span style="font-size: large;"><span style="font-size: small;"><br /></span></span>
<span style="font-size: large;"><span style="font-size: small;">To execute it we go to the directory where it is placed and we type "python nameOfTheFile.py"</span></span><br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK8qDZoEgr1dp8m3FGE6BpLK3lfINQpJk3AfJORzS5nUhdAZBBYB1IyFjN_uGjOWmlMEmR_CfOOTN-1cCAGMEkEA8egHVp3khLHt34taWSh-4yzpw6LgEMhxVX2vBUQhjUGO3I4W34-rQ/s1600/cmd3.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK8qDZoEgr1dp8m3FGE6BpLK3lfINQpJk3AfJORzS5nUhdAZBBYB1IyFjN_uGjOWmlMEmR_CfOOTN-1cCAGMEkEA8egHVp3khLHt34taWSh-4yzpw6LgEMhxVX2vBUQhjUGO3I4W34-rQ/s1600/cmd3.png" height="203" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 8: Code execution</td></tr>
</tbody></table>
<span style="font-size: large;"><span style="font-size: small;"></span></span><br />
<h3>
<span style="font-size: large;"><span style="font-size: small;"><b>Code Part 1 [3]</b></span></span></h3>
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">#! /usr/bin/python</span>
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #007020; font-weight: bold;">import</span> ZigBee
<span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">time</span>
ser <span style="color: #666666;">=</span> serial<span style="color: #666666;">.</span>Serial(<span style="color: #4070a0;">'COM5'</span>, <span style="color: #40a070;">9600</span>)
xbee <span style="color: #666666;">=</span> ZigBee(ser, escaped<span style="color: #666666;">=</span><span style="color: #007020;">True</span>)
<span style="color: #007020; font-weight: bold;">while</span> <span style="color: #007020;">True</span>:
<span style="color: #007020; font-weight: bold;">try</span>:
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'remote_at'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x13\xA2\x00\x40\x8B\x45\xC8</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
options<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x02</span><span style="color: #4070a0;">'</span>,
command<span style="color: #666666;">=</span><span style="color: #4070a0;">'D1'</span>,
parameter<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x05</span><span style="color: #4070a0;">'</span>)
<span style="color: #007020; font-weight: bold;">print</span>(<span style="color: #4070a0;">"Open port, LED ON on other side"</span>)
time<span style="color: #666666;">.</span>sleep(<span style="color: #40a070;">5</span>)
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'remote_at'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x13\xA2\x00\x40\x8B\x45\xC8</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
options<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x02</span><span style="color: #4070a0;">'</span>,
command<span style="color: #666666;">=</span><span style="color: #4070a0;">'D1'</span>,
parameter<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x04</span><span style="color: #4070a0;">'</span>)
<span style="color: #007020; font-weight: bold;">print</span>(<span style="color: #4070a0;">"Closed port, LED OFF on the other side"</span>)
time<span style="color: #666666;">.</span>sleep(<span style="color: #40a070;">5</span>)
<span style="color: #007020; font-weight: bold;">except</span> <span style="color: #007020;">KeyboardInterrupt</span>:
<span style="color: #007020; font-weight: bold;">break</span>
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'remote_at'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x00\x00\x00\x00\x00\xFF\xFF</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
options<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x02</span><span style="color: #4070a0;">'</span>,
command<span style="color: #666666;">=</span><span style="color: #4070a0;">'D1'</span>,
parameter<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x04</span><span style="color: #4070a0;">'</span>)
ser<span style="color: #666666;">.</span>close()
</pre>
</td></tr>
</tbody></table>
</div>
<b><span style="font-size: large;"><span style="font-size: small;"><br /></span></span></b>
<b><span style="font-size: large;"><span style="font-size: small;">Images Part 1</span></span></b><br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNcXa6TU-JMUBl6ti7o_y5dIBR4EUgzrhYG1NZqQO4OEjq59t2rGpj-c8GZ6yxyfvR0PTPsif0T2onWRIfuIYuDeK8kHd4oIHLI_-BB-z1-bjRI_PBJnhoff1wv_vGHqBTwN7E4Ks6OLU/s1600/part1.1.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNcXa6TU-JMUBl6ti7o_y5dIBR4EUgzrhYG1NZqQO4OEjq59t2rGpj-c8GZ6yxyfvR0PTPsif0T2onWRIfuIYuDeK8kHd4oIHLI_-BB-z1-bjRI_PBJnhoff1wv_vGHqBTwN7E4Ks6OLU/s1600/part1.1.jpg" height="300" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 9: Image Circuit Part 1</td></tr>
</tbody></table>
<br />
<b><span style="font-size: large;"><b><span style="font-size: large;">Part 2</span></b></span></b><br />
<br />
<div style="text-align: justify;">
<span style="font-size: small;">In this second part we are going to change a bit the implementation of the sender side and we are going to add functionalities that the Arduino must implement</span> in order to play the received characters in morse code. The figure below shows the International Morse Code, which we used at the Arduino code for the player:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWhcTpswBB6zDhAZbPk4PgRv4pcGuAaS5YqIclbcoWHH-qym3o814Lr2OvpLv7DYAs7vVAMCgQoCcvim2fQIKYMDU-RM6xhJI1_YL6Z9Ocx8pNMFIyBiOW4inccQTReBJhV_PRP89eXBw/s1600/International_Morse_Code.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWhcTpswBB6zDhAZbPk4PgRv4pcGuAaS5YqIclbcoWHH-qym3o814Lr2OvpLv7DYAs7vVAMCgQoCcvim2fQIKYMDU-RM6xhJI1_YL6Z9Ocx8pNMFIyBiOW4inccQTReBJhV_PRP89eXBw/s1600/International_Morse_Code.png" height="400" width="310" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 10: International Morse Code [4]</td><td class="tr-caption" style="text-align: center;"><br /></td><td class="tr-caption" style="text-align: center;"><br /></td><td class="tr-caption" style="text-align: center;"><br /></td></tr>
</tbody></table>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6SYo5lJ4oC1DZFTb2PpWMNZIQNtFyj3VdcEGgJMWzZdHJc9Tq6I049cb03oDFvMQT5hpx8e7IH15sxY08oq9c6M6BmtGNEBcdGNWn8ieGwcq8srs503FNUBhUY3we4a0SuhybmUzlc5w/s1600/part1_fritzing.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<div style="text-align: justify;">
We have the same scenario as in part 1, but we have added a LED and a buzzer, which are connected to the Arduino in order to process the information received from the XBee (which receives the characters from the sender XBee).</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj944pIyxdzaAfiLRqI5ZVI_WWgiLlK3mC2LI8MVpIRNk7RuiBlkioHwLd1mMhNuv63X3UBCy0FTrDp9RKny1YDAjMJgdLzb7Nb6iq54gpYZ3xbeZnl27wP_qKeBIMsC5_sVYhSklyx1b0/s1600/part2_fritzing.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj944pIyxdzaAfiLRqI5ZVI_WWgiLlK3mC2LI8MVpIRNk7RuiBlkioHwLd1mMhNuv63X3UBCy0FTrDp9RKny1YDAjMJgdLzb7Nb6iq54gpYZ3xbeZnl27wP_qKeBIMsC5_sVYhSklyx1b0/s1600/part2_fritzing.jpg" height="248" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 11: Circuit part 2</td></tr>
</tbody></table>
<div style="text-align: justify;">
<span style="font-size: small;">The next step is to implement the "Sender" and the "MorsePlayer" codes and we will see how the LED is turned ON and the buzzers sounds when we receive a character, playing it in morse code. The caracthers are introduced through the keyboard using the Python console.</span><br />
<h3>
<b>I</b><b>mages</b></h3>
<h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-Zb1hJAxZpULG83A-Pt67t7x1ZkCr1bxnAo_DykYFG9qohvZDjTNeYAd7PgQx9MqyoMLisHu-5bOGshOoCHBZ01nY4zhyphenhyphenSPRDmjgD1FCSjnMDGn1T0gFB-lQzl1UkPxN45u6NTxIHj5o/s1600/part2.1.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-Zb1hJAxZpULG83A-Pt67t7x1ZkCr1bxnAo_DykYFG9qohvZDjTNeYAd7PgQx9MqyoMLisHu-5bOGshOoCHBZ01nY4zhyphenhyphenSPRDmjgD1FCSjnMDGn1T0gFB-lQzl1UkPxN45u6NTxIHj5o/s1600/part2.1.JPG" height="300" width="400" /></a></td></tr>
<tr><td class="tr-caption"><span style="font-size: x-small;">Figure 12: Circuit part 2 (1)</span></td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD_KKDTW2cnh51CYPCUrcU6MoY_UitzJdvmBF0yNQQK5WvMOJCsv7acMj_XkJROkPoocd9Ok5OmGUHYGOiuatFJeCNaZ05h-Ut7k4Ilvddc-uj2Sc6-QVnWCd9IQP669h1TlviBIovap4/s1600/part2.2.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD_KKDTW2cnh51CYPCUrcU6MoY_UitzJdvmBF0yNQQK5WvMOJCsv7acMj_XkJROkPoocd9Ok5OmGUHYGOiuatFJeCNaZ05h-Ut7k4Ilvddc-uj2Sc6-QVnWCd9IQP669h1TlviBIovap4/s1600/part2.2.JPG" height="300" width="400" /></a></td></tr>
<tr><td class="tr-caption"><span style="font-size: x-small;">Figure 13: Cicuit part 2 (2)</span></td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAVJ0ViiCqsSZ29wQ_bdIF4QXtNxdy9MxpHMLot7yyaEqB9J-iTDxgXRitTatnjU7vc6uiRiB_HE23oOAm76Ifk0NKjL06L8mecpa0js7I_9-77etASxFU5CvKj3ActVuaaT5XpHTqnjU/s1600/part2.3.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAVJ0ViiCqsSZ29wQ_bdIF4QXtNxdy9MxpHMLot7yyaEqB9J-iTDxgXRitTatnjU7vc6uiRiB_HE23oOAm76Ifk0NKjL06L8mecpa0js7I_9-77etASxFU5CvKj3ActVuaaT5XpHTqnjU/s1600/part2.3.JPG" height="300" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="font-size: 13px;"><span style="font-size: x-small;">Figure 14: Circuit part 2 (3)</span><br />
<div>
<br /></div>
</td></tr>
</tbody></table>
</h3>
<h3>
<span style="font-size: small;">Video</span></h3>
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/xi_vTtGnGKU" width="560"></iframe>
</div>
<br />
<span style="font-size: small;"><b> </b></span><br />
<span style="font-size: small;"><b>Code Part 2: Sender [5]</b></span><br />
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">#! /usr/bin/python</span>
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">xbee</span> <span style="color: #007020; font-weight: bold;">import</span> ZigBee
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">array</span> <span style="color: #007020; font-weight: bold;">import</span> <span style="color: #666666;">*</span>
<span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">serial</span>
<span style="color: #007020; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">time</span>
<span style="color: #007020; font-weight: bold;">from</span> <span style="color: #0e84b5; font-weight: bold;">datetime</span> <span style="color: #007020; font-weight: bold;">import</span> datetime
ser <span style="color: #666666;">=</span> serial<span style="color: #666666;">.</span>Serial(<span style="color: #4070a0;">'COM4'</span>, <span style="color: #40a070;">9600</span>)
xbee <span style="color: #666666;">=</span> ZigBee(ser, escaped<span style="color: #666666;">=</span><span style="color: #007020;">True</span>)
<span style="color: #007020; font-weight: bold;">while</span> <span style="color: #007020;">True</span>:
<span style="color: #007020; font-weight: bold;">try</span>:
<span style="color: #007020;">input</span> <span style="color: #666666;">=</span> <span style="color: #007020;">raw_input</span>(<span style="color: #4070a0;">'Enter your letter:'</span>)
<span style="color: #007020; font-weight: bold;">for</span> c <span style="color: #007020; font-weight: bold;">in</span> <span style="color: #007020;">input</span>:
tstart <span style="color: #666666;">=</span> datetime<span style="color: #666666;">.</span>now()
xbee<span style="color: #666666;">.</span>send(<span style="color: #4070a0;">'tx'</span>,
frame_id<span style="color: #666666;">=</span><span style="color: #4070a0;">'A'</span>,
dest_addr_long<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\x00\x13\xA2\x00\x40\x8B\x45\xC8</span><span style="color: #4070a0;">'</span>,
dest_addr<span style="color: #666666;">=</span><span style="color: #4070a0;">'</span><span style="color: #4070a0; font-weight: bold;">\xFF\xFE</span><span style="color: #4070a0;">'</span>,
data<span style="color: #666666;">=</span>c)
response <span style="color: #666666;">=</span> xbee<span style="color: #666666;">.</span>wait_read_frame()
tend <span style="color: #666666;">=</span> datetime<span style="color: #666666;">.</span>now()
<span style="color: #007020; font-weight: bold;">print</span> c
<span style="color: #007020; font-weight: bold;">print</span> tend<span style="color: #666666;">-</span>tstart
time<span style="color: #666666;">.</span>sleep(<span style="color: #40a070;">2</span>)
<span style="color: #007020; font-weight: bold;">except</span> <span style="color: #007020;">KeyboardInterrupt</span>:
<span style="color: #007020; font-weight: bold;">break</span>
ser<span style="color: #666666;">.</span>close()
</pre>
</td></tr>
</tbody></table>
</div>
<span style="font-size: small;"><b><b><br /></b></b></span>
<br />
<h3>
<span style="font-size: small;"><b><b>Code Part 2: </b>Morse Player </b></span></h3>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #007020;">#include <XBee.h></span>
<span style="color: #60a0b0; font-style: italic;">// Pins</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> ledPin <span style="color: #666666;">=</span> <span style="color: #40a070;">13</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> buzzerPin <span style="color: #666666;">=</span> <span style="color: #40a070;">5</span>;
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Morse Codes</span>
<span style="color: #60a0b0; font-style: italic;"> * "s" for short (.), "l" for long (-) </span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">char</span> longNote <span style="color: #666666;">=</span> <span style="color: #4070a0;">'l'</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">char</span> shortNote <span style="color: #666666;">=</span> <span style="color: #4070a0;">'s'</span>;
<span style="color: #60a0b0; font-style: italic;">// Morse code for Alphabet</span>
<span style="color: #007020; font-weight: bold;">const</span> String letter[<span style="color: #40a070;">26</span>] <span style="color: #666666;">=</span> {
<span style="color: #4070a0;">"sl"</span>,<span style="color: #4070a0;">"lsss"</span>,<span style="color: #4070a0;">"lsls"</span>,<span style="color: #4070a0;">"lss"</span>,<span style="color: #4070a0;">"s"</span>,<span style="color: #4070a0;">"ssls"</span>,<span style="color: #4070a0;">"lls"</span>,<span style="color: #4070a0;">"ssss"</span>,<span style="color: #4070a0;">"ss"</span>,<span style="color: #4070a0;">"slll"</span>,<span style="color: #4070a0;">"lsl"</span>,
<span style="color: #4070a0;">"slss"</span>,<span style="color: #4070a0;">"ll"</span>,<span style="color: #4070a0;">"ls"</span>,<span style="color: #4070a0;">"lll"</span>,<span style="color: #4070a0;">"slls"</span>,<span style="color: #4070a0;">"llsl"</span>,<span style="color: #4070a0;">"sls"</span>,<span style="color: #4070a0;">"sss"</span>,<span style="color: #4070a0;">"l"</span>,<span style="color: #4070a0;">"ssl"</span>,<span style="color: #4070a0;">"sssl"</span>,<span style="color: #4070a0;">"sll"</span>,<span style="color: #4070a0;">"lssl"</span>,<span style="color: #4070a0;">"lsll"</span>,<span style="color: #4070a0;">"llss"</span>};
<span style="color: #60a0b0; font-style: italic;">// Morse code for Numbers</span>
<span style="color: #007020; font-weight: bold;">const</span> String number[<span style="color: #40a070;">10</span>]<span style="color: #666666;">=</span> {
<span style="color: #4070a0;">"lllll"</span>,<span style="color: #4070a0;">"sllll"</span>,<span style="color: #4070a0;">"sslll"</span>,<span style="color: #4070a0;">"sssll"</span>,<span style="color: #4070a0;">"ssssl"</span>,<span style="color: #4070a0;">"sssss"</span>,<span style="color: #4070a0;">"lssss"</span>,<span style="color: #4070a0;">"llsss"</span>,<span style="color: #4070a0;">"lllss"</span>,<span style="color: #4070a0;">"lllls"</span>};
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * XBee objects</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
XBee xbee;
XBeeResponse response;
<span style="color: #60a0b0; font-style: italic;">// create reusable response objects for responses we expect to handle</span>
ZBRxResponse rx;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
Serial.begin(<span style="color: #40a070;">9600</span>);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
xbee <span style="color: #666666;">=</span> XBee();
response <span style="color: #666666;">=</span> XBeeResponse();
rx <span style="color: #666666;">=</span> ZBRxResponse();
flashLed(ledPin, <span style="color: #40a070;">10</span>, <span style="color: #40a070;">50</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
<span style="color: #60a0b0; font-style: italic;">// 1. This will read any data that is available:</span>
xbee.readPacket();
<span style="color: #007020; font-weight: bold;">if</span> (xbee.getResponse().isAvailable())
{
<span style="color: #007020; font-weight: bold;">if</span> (xbee.getResponse().getApiId() <span style="color: #666666;">==</span> ZB_RX_RESPONSE)
{
xbee.getResponse().getZBRxResponse(rx);
Serial.println(rx.getData(<span style="color: #40a070;">0</span>));
playCode(rx.getData(<span style="color: #40a070;">0</span>));
}
}
}
<span style="color: #60a0b0; font-style: italic;">// Flash the LED for certain number of times and delay between flashes</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">flashLed</span>(<span style="color: #902000;">int</span> pin, <span style="color: #902000;">int</span> times, <span style="color: #902000;">int</span> wait)
{
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> times; <span style="color: #666666;">++</span>i)
{
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
<span style="color: #007020; font-weight: bold;">if</span> (i <span style="color: #666666;">+</span> <span style="color: #40a070;">1</span> <span style="color: #666666;"><</span> times)
{
delay(wait);
}
}
}
<span style="color: #60a0b0; font-style: italic;">// Play the required tone for the char or int transmitted</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">playCode</span>(<span style="color: #902000;">int</span> ASCII_value)
{
<span style="color: #902000;">int</span> intValue <span style="color: #666666;">=</span> ASCII_value <span style="color: #666666;">-</span> <span style="color: #4070a0;">'0'</span>;
<span style="color: #60a0b0; font-style: italic;">// Integer read</span>
<span style="color: #007020; font-weight: bold;">if</span> (intValue <span style="color: #666666;">>=</span> <span style="color: #40a070;">0</span> <span style="color: #666666;">&&</span> intValue <span style="color: #666666;"><=</span> <span style="color: #40a070;">9</span>)
{
String toPlay <span style="color: #666666;">=</span> number[intValue];
Serial.println(toPlay);
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> toPlay.length(); <span style="color: #666666;">++</span>i)
{
Serial.println(toPlay[i]);
<span style="color: #007020; font-weight: bold;">if</span>(toPlay.charAt(i) <span style="color: #666666;">==</span> longNote)
largeTone();
<span style="color: #007020; font-weight: bold;">else</span> <span style="color: #007020; font-weight: bold;">if</span>(toPlay.charAt(i) <span style="color: #666666;">==</span> shortNote)
shortTone();
}
}
<span style="color: #007020; font-weight: bold;">else</span> <span style="color: #60a0b0; font-style: italic;">// Char (A-Z) read</span>
{
String toPlay <span style="color: #666666;">=</span> letter[ASCII_value<span style="color: #666666;">-</span><span style="color: #40a070;">65</span>];
Serial.println(toPlay);
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> toPlay.length(); <span style="color: #666666;">++</span>i)
{
Serial.println(toPlay[i]);
<span style="color: #007020; font-weight: bold;">if</span> (toPlay.charAt(i) <span style="color: #666666;">==</span> longNote)
largeTone();
<span style="color: #007020; font-weight: bold;">else</span> <span style="color: #007020; font-weight: bold;">if</span> (toPlay.charAt(i) <span style="color: #666666;">==</span> shortNote)
shortTone();
}
}
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">largeTone</span>()
{
delay(<span style="color: #40a070;">50</span>);
digitalWrite(ledPin, HIGH);
analogWrite(buzzerPin, <span style="color: #40a070;">2000</span>);
delay(<span style="color: #40a070;">500</span>);
digitalWrite(ledPin, LOW);
analogWrite(buzzerPin, <span style="color: #40a070;">0</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">shortTone</span>()
{
delay(<span style="color: #40a070;">50</span>);
digitalWrite(ledPin, HIGH);
analogWrite(buzzerPin, <span style="color: #40a070;">2000</span>);
delay(<span style="color: #40a070;">100</span>);
digitalWrite(ledPin, LOW);
analogWrite(buzzerPin, <span style="color: #40a070;">0</span>);
}
</pre>
</td></tr>
</tbody></table>
</div>
<b><span style="font-size: large;"><span style="font-size: small;"><br /></span></span></b>
<br />
<h2>
<b><span style="font-size: large;">References</span></b></h2>
<span style="font-size: large;"><span style="font-size: small;">[1] Python software and libraries, retrieved from: </span></span><a href="https://www.python.org/" target="_blank">https://www.python.org/</a><br />
<span style="font-size: large;"><span style="font-size: small;">[2] XBee library for Arduino, retrieved from: </span></span><a href="https://code.google.com/p/xbee-arduino/">https://code.google.com/p/xbee-arduino/</a><br />
<span style="font-size: large;"><span style="font-size: small;">[3] Code for activate/desactivate D1 port on the receiver side by Jaume Barceló and Luis Sanabria </span></span><br />
<span style="font-size: large;"><span style="font-size: small;">[4] International Morse Code, retrieved from: </span></span><a href="http://en.wikipedia.org/wiki/Morse_code">http://en.wikipedia.org/wiki/Morse_code</a><br />
<span style="font-size: large;"><span style="font-size: small;">[5] Version of the code "modified3-blink-LED", by Jaume Barceló and Luis Sanabria</span></span>Anonymoushttp://www.blogger.com/profile/13363214092472820759noreply@blogger.com5tag:blogger.com,1999:blog-3219653273674708900.post-50808750493718727382014-05-09T19:10:00.000+02:002014-05-09T19:18:03.119+02:00Practice - Light Based Alarm (Sunset Project)<h2 style="clear: both; text-align: left;">
<span style="text-align: justify;">Description</span></h2>
<div style="text-align: justify;">
This time we are going to show you how we can interact with ambient light through sensors. As usually, we will propose you two main parts for this lab exercise: the first one shows how we implemented the WSN MOOC proposal, while the second part goes beyond that, so we will also show you how to activate a LED and a buzzer on the sensor side in order to prevent thefts.
The stuff we are using this time is showed below [1]:<br />
<ul>
<li>Breadboard (two better than one).</li>
<li>Jumper wire
Arduino UNO (and USB-A-to-B cable).</li>
<li>2 XBee.</li>
<li>2 XBee explorer (and at least one USB-to-mini-USB cable).</li>
<li>Four LEDs (white, red, blue, green).</li>
<li>A buzzer.</li>
<li>A photoresistor (or Light Dependant Resistance).</li>
<li>10Kohm in the dark, 1Kohm in bright light
20Kohm resistor .</li>
</ul>
<div>
Remember you can download the code in the <a href="http://getting-into-arduino.blogspot.com.es/p/code.html" target="_blank">code section</a>.</div>
</div>
<h2>
Part 1</h2>
<div style="text-align: justify;">
The idea is to implement a voltage divider with a fixed resistance and a variable one in order to take the value between them. This will allow us to detect the voltage variations, which will let knowing the state of the ambient light. When the photoresistor detects bright light, its resistance value decreases, so the output voltage decreases. The opposite behavior happens when it detects a dark environment.
The following schema shows you the logical circuit that we are implementing:<br />
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4j7KMUwL-MKWy-w9am5fw8_TPEkAZ5jjXCcUG5dRxBu56duDAAeEE1Db_-RsQk8gd52axCLYjbn03QTjU1A1IN3c2m6hyphenhyphenvzn-BbNTCbIJG69tStGPF_jEYZlEfyTCtMk0TKlbiE1MGFE/s1600/voltage+divider.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4j7KMUwL-MKWy-w9am5fw8_TPEkAZ5jjXCcUG5dRxBu56duDAAeEE1Db_-RsQk8gd52axCLYjbn03QTjU1A1IN3c2m6hyphenhyphenvzn-BbNTCbIJG69tStGPF_jEYZlEfyTCtMk0TKlbiE1MGFE/s1600/voltage+divider.png" height="163" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Voltage divider</td></tr>
</tbody></table>
The value that we are going to find at the point of interest is:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<span style="font-family: Times,"Times New Roman",serif;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaXh3elF10s6bdGbggYit16pMfQSLEabJ227GohwdJxuQs4ANv5wa8BDLXMfNcf1rNjPDoU-6AdbY3td2k3I2Nd6f10WOBZt2BVdBh9HtqEtJUzZ2JA8Bm0lE22FrJRD807rqechplYIw/s1600/voltage+div+formula.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaXh3elF10s6bdGbggYit16pMfQSLEabJ227GohwdJxuQs4ANv5wa8BDLXMfNcf1rNjPDoU-6AdbY3td2k3I2Nd6f10WOBZt2BVdBh9HtqEtJUzZ2JA8Bm0lE22FrJRD807rqechplYIw/s1600/voltage+div+formula.png" /></a></span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
Once we know how the photoresistor works and how we must take benefit of it, we can build the circuit that will turn on a LED depending on the ambient light received on the sensor’s side. The following picture shows the connections that have to be done for this part:
<br />
<div class="MsoNormal">
<span style="font-family: Times,"Times New Roman",serif;"><br /></span></div>
<div class="MsoNormal" style="text-align: center;">
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeCRWPM4PJ72o44Jdy5wJn5acAxu_2Bwh0D-JGmlaxl7eSduBgtKbv7wEHawlZU7gT_cnoRVuoOmgL8aB7B4_1BTacQhj_CVFJSAjwFL_7W3FGR1SbRpm8D6aUp8sqe98sBIDC26XcqKk/s1600/circuit+part+1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeCRWPM4PJ72o44Jdy5wJn5acAxu_2Bwh0D-JGmlaxl7eSduBgtKbv7wEHawlZU7gT_cnoRVuoOmgL8aB7B4_1BTacQhj_CVFJSAjwFL_7W3FGR1SbRpm8D6aUp8sqe98sBIDC26XcqKk/s1600/circuit+part+1.png" height="291" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Sunset connections [2]</td></tr>
</tbody></table>
</div>
Before implementing the code we have to configure the Xbee modules by following these steps:<br />
<br />
1. The processing part must implement coordinator tasks, so we set a PAN ID (BFB7 in our case):<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNxa291IyxBn2AKVbRYfZd8K7bILJlmkDilL_IvDY7Si_2Z_0gODueS6LqP3yoMA6au8toyCN7ywBf-pWBRildpe2At-dQ9ATsp2MzNmJ4MSV-CnCILkocwvd_bHAzmBYUQdh71oFSWPc/s1600/2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNxa291IyxBn2AKVbRYfZd8K7bILJlmkDilL_IvDY7Si_2Z_0gODueS6LqP3yoMA6au8toyCN7ywBf-pWBRildpe2At-dQ9ATsp2MzNmJ4MSV-CnCILkocwvd_bHAzmBYUQdh71oFSWPc/s1600/2.png" height="276" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: Coordinator node</td></tr>
</tbody></table>
<br />
2. The sensor side is the router part, so we set the same PAN ID as for the coordinator and we enable the “JV Channel Verification” field:<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiS6ofnOqeDd9odQ7EVe3DGovEABngKaqUx6FC-9nC_hfAppawjDX7uYquxYdYUJ2lny6iiumJ_cIdR7rqEraCAs7-Idh8Cj3YCD_SMehJR5sXwaok1gqeYJ42briVxAufv4BWIDsE5m4/s1600/1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiS6ofnOqeDd9odQ7EVe3DGovEABngKaqUx6FC-9nC_hfAppawjDX7uYquxYdYUJ2lny6iiumJ_cIdR7rqEraCAs7-Idh8Cj3YCD_SMehJR5sXwaok1gqeYJ42briVxAufv4BWIDsE5m4/s1600/1.png" height="276" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: Router Node 1</td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
3. We also configure the I/O settings that allow us to convert the received values from the circuit into digital characters for transmission: ADC (Analog-Digital Conversion) option.</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzQMPQrYULBEN1gpwil86jMzzTtyiMftx3NTKM9qkOV3SPPDPfdXZ0TdGlIKFX_Zr2u_x6xYxlEgGs_gd2gyf5ISizDCG1fl5LuV_1QdnsEQfeBSQtoP6qWsnNExbvjCgOYOn3ws6jQ8w/s1600/3_D0.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzQMPQrYULBEN1gpwil86jMzzTtyiMftx3NTKM9qkOV3SPPDPfdXZ0TdGlIKFX_Zr2u_x6xYxlEgGs_gd2gyf5ISizDCG1fl5LuV_1QdnsEQfeBSQtoP6qWsnNExbvjCgOYOn3ws6jQ8w/s1600/3_D0.png" height="275" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: Router Node 2</td></tr>
</tbody></table>
<div align="center" class="MsoNormal" style="text-align: center;">
<span style="font-family: Times,"Times New Roman",serif;"></span></div>
<br />
<div style="text-align: justify;">
4. The last step consists in changing the IO Sampling Rate into 255 ms (0xFF) in order to avoid some communication troubles:
</div>
<div class="MsoNormal" style="text-align: left;">
<span style="font-family: Times,"Times New Roman",serif;"><span lang="EN-US" style="mso-ansi-language: EN-US;"><br /></span></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6ekXE6WbfLR2_gyvPVn_X_ZiCAngVzT9yxvxkm6cC-vyfMP3AQ-8eNVNCEKYc6IVit1EIqyrq7o1bpa54q35EH3KJMY4zuzwt1LvAIasdQPcBhLBPMXXPXKE6jNTBeSHDkCIOVScevCw/s1600/4_sampling.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6ekXE6WbfLR2_gyvPVn_X_ZiCAngVzT9yxvxkm6cC-vyfMP3AQ-8eNVNCEKYc6IVit1EIqyrq7o1bpa54q35EH3KJMY4zuzwt1LvAIasdQPcBhLBPMXXPXKE6jNTBeSHDkCIOVScevCw/s1600/4_sampling.png" height="276" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: Router Node 3</td></tr>
</tbody></table>
<h3>
Circuit Photography
</h3>
<div style="text-align: center;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7u3Nue5glCB00WZu2yiKC6f5sCckqgYOgd_77qH3C8WnqnwYFj9CEJOr7aAYnKDy7K3xpfH61w3NmYzyWfW4TqNidhFQqhyNbdy7KRO5cirsneb677eBNQb4sPeNyp-uObwWeuaL1wrM/s1600/CircuitA.jpg"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7u3Nue5glCB00WZu2yiKC6f5sCckqgYOgd_77qH3C8WnqnwYFj9CEJOr7aAYnKDy7K3xpfH61w3NmYzyWfW4TqNidhFQqhyNbdy7KRO5cirsneb677eBNQb4sPeNyp-uObwWeuaL1wrM/s1600/CircuitA.jpg" height="240" width="320" /></a></div>
<span style="font-family: Times,"Times New Roman",serif;"><span lang="EN-US" style="mso-ansi-language: EN-US;"><span style="font-family: Times,"Times New Roman",serif;"><span style="font-size: small;"><b> </b></span></span></span></span><b style="font-family: Times, 'Times New Roman', serif;"> </b></div>
<div class="MsoNormal">
<h3>
Processing Side Code (Part 1)</h3>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #902000;">int</span> LED_NIGHT <span style="color: #666666;">=</span> <span style="color: #40a070;">10</span>;
<span style="color: #902000;">int</span> LED_SUNSET <span style="color: #666666;">=</span> <span style="color: #40a070;">11</span>;
<span style="color: #902000;">int</span> LED_DAY <span style="color: #666666;">=</span> <span style="color: #40a070;">12</span>;
<span style="color: #902000;">int</span> debugLED <span style="color: #666666;">=</span> <span style="color: #40a070;">13</span>;
<span style="color: #902000;">int</span> analogValue <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #60a0b0; font-style: italic;">// Upper boundaries</span>
<span style="color: #902000;">int</span> DAYup <span style="color: #666666;">=</span> <span style="color: #40a070;">750</span>;
<span style="color: #902000;">int</span> SUNSETup <span style="color: #666666;">=</span> <span style="color: #40a070;">950</span>;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>() {
pinMode(LED_DAY,OUTPUT);
pinMode(LED_SUNSET,OUTPUT);
pinMode(LED_NIGHT,OUTPUT);
pinMode(debugLED,OUTPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>() {
digitalWrite(LED_NIGHT, LOW);
digitalWrite(LED_SUNSET, LOW);
digitalWrite(LED_DAY, LOW);
<span style="color: #60a0b0; font-style: italic;">// make sure everything we need is in the buffer</span>
<span style="color: #007020; font-weight: bold;">if</span> (Serial.available() <span style="color: #666666;">>=</span> <span style="color: #40a070;">21</span>) {
<span style="color: #60a0b0; font-style: italic;">// look for the start byte</span>
<span style="color: #007020; font-weight: bold;">if</span> (Serial.read() <span style="color: #666666;">==</span> <span style="color: #40a070;">0x7E</span>) {
<span style="color: #60a0b0; font-style: italic;">//blink debug LED to indicate when data is received</span>
digitalWrite(debugLED, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(debugLED, LOW);
<span style="color: #60a0b0; font-style: italic;">// read the variables that we're not using out of the buffer</span>
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i<span style="color: #666666;"><</span><span style="color: #40a070;">18</span>; i<span style="color: #666666;">++</span>) {
byte discard <span style="color: #666666;">=</span> Serial.read();
}
<span style="color: #902000;">int</span> analogHigh <span style="color: #666666;">=</span> Serial.read();
<span style="color: #902000;">int</span> analogLow <span style="color: #666666;">=</span> Serial.read();
analogValue <span style="color: #666666;">=</span> analogLow <span style="color: #666666;">+</span> (analogHigh <span style="color: #666666;">*</span> <span style="color: #40a070;">256</span>);
Serial.print(<span style="color: #4070a0;">"Analog value "</span>);
Serial.println(analogValue);
}
}
<span style="color: #60a0b0; font-style: italic;">// DAY TIME</span>
<span style="color: #007020; font-weight: bold;">if</span> (analogValue <span style="color: #666666;">></span> <span style="color: #40a070;">0</span> <span style="color: #666666;">&&</span> analogValue <span style="color: #666666;"><=</span> DAYup) {
digitalWrite(LED_DAY, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(LED_DAY, LOW);
}
<span style="color: #60a0b0; font-style: italic;">// SUNSET TIME</span>
<span style="color: #007020; font-weight: bold;">if</span> (analogValue <span style="color: #666666;">></span> DAYup <span style="color: #666666;">&&</span> analogValue <span style="color: #666666;"><=</span> SUNSETup) {
digitalWrite(LED_SUNSET, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(LED_SUNSET, LOW);
}
<span style="color: #60a0b0; font-style: italic;">// NIGHT TIME</span>
<span style="color: #007020; font-weight: bold;">if</span> (analogValue <span style="color: #666666;">></span> SUNSETup <span style="color: #666666;">&&</span> analogValue <span style="color: #666666;"><=</span> <span style="color: #40a070;">1023</span>) {
digitalWrite(LED_NIGHT, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(LED_NIGHT, LOW);
}
}
</pre>
</td></tr>
</tbody></table>
</div>
</div>
<h2>
Part 2
</h2>
<div style="text-align: justify;">
To extend the practical exercise seen on this entry we have deployed a buzzer that makes sounds when the state of the sensor changes. It could be useful for detecting movement (detecting a shadow) and then displaying the alarm to frighten thieves.<br />
<br />
So, the behavior of our circuit is going to be the same as shown before, but we are adding communication between the coordinator and the router, in order to transmit a command to activate the port D1 at the sensor side, that will make the buzzer sound.<br />
<br />
For this purpose, we have to use the "setRemoteState" function, which sends information through the serial channel with the value (as input of the function) that enables or disables the specific port with we are dealing to.<br />
<br />
In our case, we have implemented that the Programmer side checks every 10 seconds if the status of the photoreceptor has changed from day to night. If yes, we call the "setRemoteState" function to send "05". Otherwise, we send "04".<br />
<br />
More information about XBee 802.15.4 digital input/output passing can be found on the following link:
<a href="http://www.digi.com/support/kbase/kbaseresultdetl?id=2188">http://www.digi.com/support/kbase/kbaseresultdetl?id=2188</a><br />
<br /></div>
<h3>
Circuit Assembly
</h3>
<div>
<br /></div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiZuFokZuUDAOdeTRVYlHZPQrEMDIMzF03GT_Oi5LGNpphyNzSHLpbYrJd6srp82MyAIrpOlLE-x77piXoLvmbZsiwIcBt85whFoA7zOj-nZTFjCxuGNFUQQDKNPQnXw4_Pi_J_IZ4btw/s1600/sunsetoFri.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiZuFokZuUDAOdeTRVYlHZPQrEMDIMzF03GT_Oi5LGNpphyNzSHLpbYrJd6srp82MyAIrpOlLE-x77piXoLvmbZsiwIcBt85whFoA7zOj-nZTFjCxuGNFUQQDKNPQnXw4_Pi_J_IZ4btw/s1600/sunsetoFri.png" height="240" width="320" /></a></div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-family: Times,"Times New Roman",serif;"></span></div>
<h3>
Circuit Photographies </h3>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTwvTXG_t-VhKqX-laqJuR-IZ-BNu6WjqZ058GEadh9xTYXyAC8cFOnA7fcqzwK_r4rJUBMli0-iXrPSxj905cRbTfRyClAXRaM36gVLH7VSkNuNJ12GsA_g2UvF2qJJN9IGYhqZvqvZM/s1600/IMG_20140509_163905.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTwvTXG_t-VhKqX-laqJuR-IZ-BNu6WjqZ058GEadh9xTYXyAC8cFOnA7fcqzwK_r4rJUBMli0-iXrPSxj905cRbTfRyClAXRaM36gVLH7VSkNuNJ12GsA_g2UvF2qJJN9IGYhqZvqvZM/s1600/IMG_20140509_163905.jpg" height="240" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicKy0QrtdmSTJHjS0DQHCpF6GezcNQe69-XG2NMatar3QClenD8GKyy7NqH9tDYU9nPZn4obN0tlUMmO-00Wdyaa8WvKuRSDwYzpzKUVFlfvuKfdbiOriBYSpTDLNVDseajBIAfRVg2fs/s1600/IMG_20140509_163910.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicKy0QrtdmSTJHjS0DQHCpF6GezcNQe69-XG2NMatar3QClenD8GKyy7NqH9tDYU9nPZn4obN0tlUMmO-00Wdyaa8WvKuRSDwYzpzKUVFlfvuKfdbiOriBYSpTDLNVDseajBIAfRVg2fs/s1600/IMG_20140509_163910.jpg" height="240" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbjcNhyO3LEppCqL1J5lwATC5D27G3wv3Exr-LgOkbvrYgrTtpLkqznK_U6VE8v5EjhBxQcltgM3jqbtPxbewRmCviklZigkk2rFb7S99rhVa3dzV1DPqVxWFCA4CIgqQR4bD4jbaU4c4/s1600/IMG_20140509_163844.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbjcNhyO3LEppCqL1J5lwATC5D27G3wv3Exr-LgOkbvrYgrTtpLkqznK_U6VE8v5EjhBxQcltgM3jqbtPxbewRmCviklZigkk2rFb7S99rhVa3dzV1DPqVxWFCA4CIgqQR4bD4jbaU4c4/s1600/IMG_20140509_163844.jpg" height="240" width="320" /></a></div>
<div>
<br /></div>
<div>
<h3>
Processing Side Code (Part 2)</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #902000;">int</span> LED_NIGHT <span style="color: #666666;">=</span> <span style="color: #40a070;">10</span>;
<span style="color: #902000;">int</span> LED_SUNSET <span style="color: #666666;">=</span> <span style="color: #40a070;">11</span>;
<span style="color: #902000;">int</span> LED_DAY <span style="color: #666666;">=</span> <span style="color: #40a070;">12</span>;
<span style="color: #902000;">int</span> debugLED <span style="color: #666666;">=</span> <span style="color: #40a070;">13</span>;
<span style="color: #902000;">int</span> analogValue <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #60a0b0; font-style: italic;">// Upper boundaries</span>
<span style="color: #902000;">int</span> DAYup <span style="color: #666666;">=</span> <span style="color: #40a070;">750</span>;
<span style="color: #902000;">int</span> SUNSETup <span style="color: #666666;">=</span> <span style="color: #40a070;">950</span>;
<span style="color: #902000;">int</span> remoteIndicator <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>; <span style="color: #60a0b0; font-style: italic;">// keeps track of the desired remote on/off state</span>
<span style="color: #902000;">int</span> lastRemoteIndicator <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>; <span style="color: #60a0b0; font-style: italic;">// record of prior remote state</span>
<span style="color: #902000;">unsigned</span> <span style="color: #902000;">long</span> lastSent <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; <span style="color: #60a0b0; font-style: italic;">// records last time the remote was re-set to keep it in sync</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>() {
pinMode(LED_DAY,OUTPUT);
pinMode(LED_SUNSET,OUTPUT);
pinMode(LED_NIGHT,OUTPUT);
pinMode(debugLED,OUTPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>() {
digitalWrite(LED_NIGHT, LOW);
digitalWrite(LED_SUNSET, LOW);
digitalWrite(LED_DAY, LOW);
<span style="color: #60a0b0; font-style: italic;">// make sure everything we need is in the buffer</span>
<span style="color: #007020; font-weight: bold;">if</span> (Serial.available() <span style="color: #666666;">>=</span> <span style="color: #40a070;">23</span>) {
<span style="color: #60a0b0; font-style: italic;">// look for the start byte</span>
<span style="color: #007020; font-weight: bold;">if</span> (Serial.read() <span style="color: #666666;">==</span> <span style="color: #40a070;">0x7E</span>) {
<span style="color: #60a0b0; font-style: italic;">//blink debug LED to indicate when data is received</span>
digitalWrite(debugLED, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(debugLED, LOW);
<span style="color: #60a0b0; font-style: italic;">// read the variables that we're not using out of the buffer</span>
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i<span style="color: #666666;"><</span><span style="color: #40a070;">20</span>; i<span style="color: #666666;">++</span>) {
byte discard <span style="color: #666666;">=</span> Serial.read();
}
<span style="color: #902000;">int</span> analogHigh <span style="color: #666666;">=</span> Serial.read();
<span style="color: #902000;">int</span> analogLow <span style="color: #666666;">=</span> Serial.read();
analogValue <span style="color: #666666;">=</span> analogLow <span style="color: #666666;">+</span> (analogHigh <span style="color: #666666;">*</span> <span style="color: #40a070;">256</span>);
Serial.print(<span style="color: #4070a0;">"Analog value "</span>);
Serial.println(analogValue);
}
}
<span style="color: #60a0b0; font-style: italic;">// DAY TIME</span>
<span style="color: #007020; font-weight: bold;">if</span> (analogValue <span style="color: #666666;">></span> <span style="color: #40a070;">0</span> <span style="color: #666666;">&&</span> analogValue <span style="color: #666666;"><=</span> DAYup) {
digitalWrite(LED_DAY, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(LED_DAY, LOW);
remoteIndicator <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
}
<span style="color: #60a0b0; font-style: italic;">// SUNSET TIME</span>
<span style="color: #007020; font-weight: bold;">if</span> (analogValue <span style="color: #666666;">></span> DAYup <span style="color: #666666;">&&</span> analogValue <span style="color: #666666;"><=</span> SUNSETup) {
digitalWrite(LED_SUNSET, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(LED_SUNSET, LOW);
remoteIndicator <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
}
<span style="color: #60a0b0; font-style: italic;">// NIGHT TIME</span>
<span style="color: #007020; font-weight: bold;">if</span> (analogValue <span style="color: #666666;">></span> SUNSETup <span style="color: #666666;">&&</span> analogValue <span style="color: #666666;"><=</span> <span style="color: #40a070;">1023</span>) {
digitalWrite(LED_NIGHT, HIGH);
delay(<span style="color: #40a070;">10</span>);
digitalWrite(LED_NIGHT, LOW);
remoteIndicator <span style="color: #666666;">=</span> <span style="color: #007020;">true</span>;
}
<span style="color: #60a0b0; font-style: italic;">// set the indicator immediately when there's a state change</span>
<span style="color: #007020; font-weight: bold;">if</span> (remoteIndicator <span style="color: #666666;">!=</span> lastRemoteIndicator) {
<span style="color: #007020; font-weight: bold;">if</span> (remoteIndicator<span style="color: #666666;">==</span><span style="color: #007020;">false</span>) setRemoteState(<span style="color: #40a070;">0x4</span>);
<span style="color: #007020; font-weight: bold;">if</span> (remoteIndicator<span style="color: #666666;">==</span><span style="color: #007020;">true</span>) setRemoteState(<span style="color: #40a070;">0x5</span>);
lastRemoteIndicator <span style="color: #666666;">=</span> remoteIndicator;
}
<span style="color: #60a0b0; font-style: italic;">// re-set the indicator occasionally in case it's out of sync</span>
<span style="color: #007020; font-weight: bold;">if</span> (millis() <span style="color: #666666;">-</span> lastSent <span style="color: #666666;">></span> <span style="color: #40a070;">10000</span> ) {
<span style="color: #007020; font-weight: bold;">if</span> (remoteIndicator<span style="color: #666666;">==</span><span style="color: #007020;">false</span>) setRemoteState(<span style="color: #40a070;">0x4</span>);
<span style="color: #007020; font-weight: bold;">if</span> (remoteIndicator<span style="color: #666666;">==</span><span style="color: #007020;">true</span>) setRemoteState(<span style="color: #40a070;">0x5</span>);
lastSent <span style="color: #666666;">=</span> millis();
}
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">setRemoteState</span>(<span style="color: #902000;">int</span> value) {
Serial.write(<span style="color: #40a070;">0x7e</span>);<span style="color: #60a0b0; font-style: italic;">//Start byte</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);<span style="color: #60a0b0; font-style: italic;">//Length</span>
Serial.write(<span style="color: #40a070;">0x10</span>);<span style="color: #60a0b0; font-style: italic;">//Length High</span>
Serial.write(<span style="color: #40a070;">0x17</span>);<span style="color: #60a0b0; font-style: italic;">//AT Command Request</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);<span style="color: #60a0b0; font-style: italic;">//Frame ID</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);<span style="color: #60a0b0; font-style: italic;">//Serial Number of Destination</span>
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write((byte)<span style="color: #40a070;">0x0</span>);
Serial.write(<span style="color: #40a070;">0xFF</span>);
Serial.write(<span style="color: #40a070;">0xFF</span>);<span style="color: #60a0b0; font-style: italic;">//End of Serial Number of Destinition</span>
<span style="color: #60a0b0; font-style: italic;">// 16 bit of recipient or 0xFFFE if unknown</span>
Serial.write(<span style="color: #40a070;">0xFF</span>);
Serial.write(<span style="color: #40a070;">0xFE</span>);
Serial.write(<span style="color: #40a070;">0x02</span>);<span style="color: #60a0b0; font-style: italic;">//Apply changes immediately</span>
<span style="color: #60a0b0; font-style: italic;">//Command name in ASCII characters</span>
Serial.write(<span style="color: #4070a0;">'D'</span>);
Serial.write(<span style="color: #4070a0;">'1'</span>);
<span style="color: #60a0b0; font-style: italic;">//command data in as many bytes after length bytes</span>
Serial.write(value);
<span style="color: #60a0b0; font-style: italic;">//checksum is all bytes after length bytes</span>
<span style="color: #902000;">long</span> sum <span style="color: #666666;">=</span> <span style="color: #40a070;">0x17</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0xFF</span><span style="color: #666666;">+</span> <span style="color: #40a070;">0xFF</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0xFF</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0xFE</span> <span style="color: #666666;">+</span> <span style="color: #40a070;">0x02</span> <span style="color: #666666;">+</span> <span style="color: #4070a0;">'D'</span> <span style="color: #666666;">+</span> <span style="color: #4070a0;">'1'</span> <span style="color: #666666;">+</span> value;
Serial.write (<span style="color: #40a070;">0xFF</span> <span style="color: #666666;">-</span> ( sum <span style="color: #666666;">&</span> <span style="color: #40a070;">0xFF</span>) );
delay(<span style="color: #40a070;">10</span>); <span style="color: #60a0b0; font-style: italic;">// avoiding overwhelming</span>
}
</pre>
</td></tr>
</tbody></table>
</div>
<h3>
</h3>
<h3>
<br /></h3>
<h3>
Buzzer Code</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">// Pin where the buzzer is connected</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> BELL <span style="color: #666666;">=</span> <span style="color: #40a070;">5</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> PIN_STATE <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #60a0b0; font-style: italic;">// Light detector state</span>
<span style="color: #902000;">int</span> DETstate <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #60a0b0; font-style: italic;">// Tone variables</span>
<span style="color: #902000;">float</span> sinVal;
<span style="color: #902000;">int</span> toneVal;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
pinMode(BELL, OUTPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
DETstate <span style="color: #666666;">=</span> digitalRead(PIN_STATE);
<span style="color: #007020; font-weight: bold;">if</span> (DETstate <span style="color: #666666;">==</span> HIGH){
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> x<span style="color: #666666;">=</span><span style="color: #40a070;">0</span>; x<span style="color: #666666;"><</span><span style="color: #40a070;">180</span>; x<span style="color: #666666;">++</span>) {
<span style="color: #60a0b0; font-style: italic;">// convert degrees to radians then obtain sin value</span>
sinVal <span style="color: #666666;">=</span> (sin(x<span style="color: #666666;">*</span>(<span style="color: #40a070;">3.1412</span><span style="color: #666666;">/</span><span style="color: #40a070;">180</span>)));
<span style="color: #60a0b0; font-style: italic;">// generate a frequency from the sin value</span>
toneVal <span style="color: #666666;">=</span> <span style="color: #40a070;">2000</span><span style="color: #666666;">+</span>(<span style="color: #902000;">int</span>(sinVal<span style="color: #666666;">*</span><span style="color: #40a070;">1000</span>));
tone(BELL, toneVal);
}
delay(<span style="color: #40a070;">10</span>);
noTone(BELL);
<span style="color: #60a0b0; font-style: italic;">//analogWrite(BELL, 0); </span>
}
}
</pre>
</td></tr>
</tbody></table>
</div>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
<br /></h3>
<h3 style="text-align: left;">
Video</h3>
<div style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/h7ox8sri2-c" width="560"></iframe>
</div>
<div>
<div style="text-align: center;">
<br /></div>
</div>
<h2>
References</h2>
<div class="MsoNormal">
<div class="MsoNormal">
<div style="text-align: justify;">
[1]
Material list retrieved from “WSN course guide”, chapter 11 (Sunset Sensor), made
by Jaume Barceló and Luis Sanabria </div>
</div>
</div>
<div style="text-align: justify;">
<a href="http://es.scribd.com/doc/156136472/A-course-on-Wireless-Sensor-Networks-WSNs">http://es.scribd.com/doc/156136472/A-course-on-Wireless-Sensor-Networks-WSNs </a></div>
<br />
<div style="text-align: justify;">
[2] Image
retrieved from “WSN course guide”, chapter 11 (Sunset Sensor), made by Jaume
Barceló and Luis Sanabria </div>
<a href="http://es.scribd.com/doc/156136472/A-course-on-Wireless-Sensor-Networks-WSNs">
</a></div>
Anonymoushttp://www.blogger.com/profile/13363214092472820759noreply@blogger.com0tag:blogger.com,1999:blog-3219653273674708900.post-46010953919174498562014-05-01T11:50:00.000+02:002014-05-06T09:47:57.201+02:00Practice - Melody Selector Using XBee<h2>
Description</h2>
<div style="text-align: justify;">
On this section we are going to configure a system in which a buzz is going to be activated by a remote button. For achieve this purpose we are going to establish communication between two Xbee modules connected to different Arduino.<br />
<br /></div>
<div style="text-align: justify;">
We are also showing how we implemented a system in which a melody is displayed in one side according to the number of times a button is pressed on the other side.</div>
<div style="text-align: justify;">
<br />
Our invented project (explained in more detail in Part 2) consists in a melody player using two Arduinos and two XBee's (along with XBee Explorer to connect to the breadboard). In one of the nodes we will select the melody to be played and in the second one the order will be received and the melody will be played; moreover some information will be return from the player to the order node.</div>
<div style="text-align: justify;">
<br /></div>
<div>
The items we are going to need for this build are [1]:</div>
<div>
<ul>
<li>Hookup wires.</li>
<li>Two Arduino boards.</li>
<li>USB A-to-B cable for the Arduinos.</li>
<li>Two 10K resistors.</li>
<li>Two momentary switch or push button for input.</li>
<li>One buzzer for output.</li>
<li>One XBee radio configured as ZigBee Coordinator AT.</li>
<li>One XBee radio configured as ZigBee Router AT.</li>
<li>Two breakout boards.</li>
<li>USB cable for the XBee breakout board.</li>
<li>Two LEDs (one green and one yellow).</li>
<li>1 display.</li>
</ul>
<div>
The software used to complete the task is the same as in the previous practice exercise:</div>
</div>
<ul>
<li><a href="http://ftp1.digi.com/support/utilities/40003026_A.exe" style="text-align: justify;" target="_blank">XCTU Software</a></li>
<li><a href="http://www.ftdichip.com/Drivers/CDM/CDM%20v2.10.00%20WHQL%20Certified.exe" target="_blank">VCP drivers for FTDI</a></li>
<li><a href="https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads" target="_blank">Liquid Crystal Library (for LCD screen)</a></li>
</ul>
<div>
Remember you can download the code from the <a href="http://getting-into-arduino.blogspot.com.es/p/code.html" target="_blank">code section</a>.</div>
<div>
</div>
<div style="text-align: justify;">
</div>
<h2>
Part 1: Wireless Doorbell</h2>
<div style="text-align: justify;">
First of
all we must build the circuits shown on the WSN Course Guide (Section
8) in order to configure the Switch and the Buzz of the system, i.e.,
the button and the buzz. Here are the images of how the cuircuits
must be done:</div>
<div style="text-align: justify;">
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBJxzjElT8o5wAtbbeIVZBwlVQee6CVXCP6gLPjn17U3omE5kZGP5ElBK0qTNmfnkJDRjC4OQSuUOUozyvP9NuQtEDsX5mKgFqeV2dyZ7TSrDfOENqS9LBSEFEUK5QsQyveprCGAOLvO0/s1600/lab3_button_circuit.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBJxzjElT8o5wAtbbeIVZBwlVQee6CVXCP6gLPjn17U3omE5kZGP5ElBK0qTNmfnkJDRjC4OQSuUOUozyvP9NuQtEDsX5mKgFqeV2dyZ7TSrDfOENqS9LBSEFEUK5QsQyveprCGAOLvO0/s1600/lab3_button_circuit.png" height="148" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Button circuit building [2]<br />
<br /></td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjU3SE51pYc4Q-qLSZjU-1Ll9Cl1vCIpclO3fQHYEZxhFQCdnOLqB7AeDkNZwhrTqyfKSj4t0xhF0DxQqTgBmM1Zd5vz3pZJNDMs_VOjPNi5MZTIcWliqBpKrkSughbPVzKej1auVojgFc/s1600/lab3_buzz_circuit.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjU3SE51pYc4Q-qLSZjU-1Ll9Cl1vCIpclO3fQHYEZxhFQCdnOLqB7AeDkNZwhrTqyfKSj4t0xhF0DxQqTgBmM1Zd5vz3pZJNDMs_VOjPNi5MZTIcWliqBpKrkSughbPVzKej1auVojgFc/s1600/lab3_buzz_circuit.png" height="181" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Buzz circuit building [3]</td></tr>
</tbody></table>
<span style="line-height: 100%;"><br /></span>
<br />
<div style="text-align: justify;">
<span style="line-height: 100%;"><span style="line-height: 100%;">Besides the
building of the circuit, we must configure the Xbee modules as we saw
on the previous practice, obtaining a Coordinator AT and a Router AT.</span></span><br />
<br /></div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
On the
figures shown below we can see pictures of both circuits. Note that
we have added two LEDs in the button circuit: one is used for checking that current passes
through the button and the other is used to check that buzz is
buzzing.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNGl9CSmx4B34WS7xIKCiq4gRGoo9pzmt63M1CVAGSwDVfULGsLP1wwNYtHGqj3FZpjijGW-okyKLg4Q1_3C-Z25s7-vT64MmN5f7ueFmJw6lLWpH4hg4hRBorrrijMHAovWsyWbiW5is/s1600/circuit1_button.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNGl9CSmx4B34WS7xIKCiq4gRGoo9pzmt63M1CVAGSwDVfULGsLP1wwNYtHGqj3FZpjijGW-okyKLg4Q1_3C-Z25s7-vT64MmN5f7ueFmJw6lLWpH4hg4hRBorrrijMHAovWsyWbiW5is/s1600/circuit1_button.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: Button Circuit</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCFk6bKfNktba3_gwv4JIYIAX-pziH6boSrYijjtDlpnQfvZ_5yfJw-Eirdl-77AVgLGM6z3dYXSKtwOnXmL-RKlkmddGAyOMn5u42oKbgKBg6hW8ALuLZQHEWlOOQrgT7pTBKurxACFU/s1600/circuit1_buzzer.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCFk6bKfNktba3_gwv4JIYIAX-pziH6boSrYijjtDlpnQfvZ_5yfJw-Eirdl-77AVgLGM6z3dYXSKtwOnXmL-RKlkmddGAyOMn5u42oKbgKBg6hW8ALuLZQHEWlOOQrgT7pTBKurxACFU/s1600/circuit1_buzzer.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: Buzzer Circuit</td></tr>
</tbody></table>
<br /></div>
<div style="text-align: justify;">
Now we show the code that we have used in order to implement the button and the buzz functionalities. This modified code is attached to the <a href="http://getting-into-arduino.blogspot.com.es/p/code.html" target="_blank">code section</a> of this blog.<br />
<br /></div>
<div style="text-align: justify;">
<h3>
Button Code</h3>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #902000;">int</span> BUTTON <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #902000;">int</span> ACK_led <span style="color: #666666;">=</span> <span style="color: #40a070;">12</span>;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
pinMode(BUTTON, INPUT);
<span style="color: #60a0b0; font-style: italic;">// initialize the digital pin as an output.</span>
pinMode(ACK_led, OUTPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>(){
<span style="color: #60a0b0; font-style: italic;">// SEND</span>
<span style="color: #60a0b0; font-style: italic;">// send a capital D over the serial port if the button is pressed</span>
<span style="color: #007020; font-weight: bold;">if</span>(digitalRead(BUTTON)<span style="color: #666666;">==</span>HIGH)
{
Serial.print(<span style="color: #4070a0;">"D"</span>);
delay(<span style="color: #40a070;">300</span>); <span style="color: #60a0b0; font-style: italic;">// prevents overwhelming the serial port</span>
}
<span style="color: #60a0b0; font-style: italic;">// RECEIVE</span>
<span style="color: #007020; font-weight: bold;">if</span> (Serial.available()<span style="color: #666666;">></span><span style="color: #40a070;">0</span>)
{
<span style="color: #007020; font-weight: bold;">if</span> (Serial.read() <span style="color: #666666;">==</span> <span style="color: #4070a0;">'A'</span>)
{
digitalWrite(ACK_led, HIGH);
delay(<span style="color: #40a070;">500</span>);
}
digitalWrite(ACK_led, LOW);
}
}
</pre>
</td></tr>
</tbody></table>
</div>
<h3>
Buzzer Code</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">// Pin where the buzzer is connected</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> BELL <span style="color: #666666;">=</span> <span style="color: #40a070;">5</span>;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
pinMode(BELL, OUTPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
<span style="color: #007020; font-weight: bold;">if</span> (Serial.available() <span style="color: #666666;">></span> <span style="color: #40a070;">0</span>)
{
<span style="color: #007020; font-weight: bold;">if</span> (Serial.read() <span style="color: #666666;">==</span> <span style="color: #4070a0;">'D'</span>)
{
<span style="color: #60a0b0; font-style: italic;">// Send 'A' to tell the other node that</span>
<span style="color: #60a0b0; font-style: italic;">// it is playing a beep in the buzzer</span>
Serial.print(<span style="color: #4070a0;">"A"</span>);
analogWrite(BELL, <span style="color: #40a070;">240</span>);
delay(<span style="color: #40a070;">10</span>);
analogWrite(BELL, <span style="color: #40a070;">0</span>);
}
}
}</pre>
</td></tr>
</tbody></table>
</div>
</div>
<h2 style="text-align: justify;">
Part 2: Melody Player</h2>
<div style="text-align: justify;">
<span style="line-height: 100%;">In this extra part, as explained before, we have two nodes:</span><br />
<ul style="text-align: start;">
<li style="text-align: justify;">One node has an array of melodies that it can play thanks to a buzzer. It receives the melody identifier to play through an XBee connected to the Arduino. Moreover, it sends back the title of the song (it includes an end identifier which in our case is '\n') being played through the XBee too (in case the song does not exist, an error message is sent). When the song has been totally played, an end character is sent.</li>
<li style="text-align: justify;">One node forms sums one to a variable each time a button is pressed; once we have pushed as many times as we wanted, we push another button to send the number through the XBee to the first node described. Then, it receives the title of the song played or an error message if the song did not exist. This title (or error) will be displayed through an LCD screen along with the previously sent number until the character indicating the end of the song is received; in that case, a message that tells the songs has ended is displayed.</li>
</ul>
<div>
Some difficulties appeared during the elaboration of the lab. One of the main problems we faced was the fact that the character that tells the song has ended was never being displayed individually; by doing some experiments we observed that this character was only sent if some other characters were sent previously through the serial connection. Therefore, as we did not find a good solution, we modified the code of Brett Hagman such that each time the value of the pointer <span style="font-family: Courier New, Courier, monospace;">*p</span> is different from <span style="font-family: Courier New, Courier, monospace;">'\0'</span> we send a blank through the serial and when it is <span style="font-family: Courier New, Courier, monospace;">'\0'</span> we send the desired character indicating the end of song. It is a quite dirty way to do it but any more way to do it was found.</div>
<div>
<br /></div>
<div>
What we also notice is that we tried to incorporate all songs Brett Hagman had on his code in the array of songs we introduced. However, some problems were experimented: songs were reproduced in kind of random way (even some songs seemed to have two id's). But after reducing the number of songs to 7 from 15, everything worked fine. Therefore, we think it was something related to memory issues, which is very important to take into account.<br />
<br />
Another problem we faced was the following: from the buzzer we send a string with the song name. Therefore, we thought that in the button part we could read a string as well using <span style="font-family: Courier New, Courier, monospace;">readString()</span> but it did not work well since there was a lot of delay (or even it was not printed). Consequently, we tried to read char by char and putting them into a string; as a result, the performance was much better this time.<br />
<br /></div>
<h3>
</h3>
<h3>
</h3>
<h3>
Circuit schemas</h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9jTARhSRiHXecKz_cj0xi4o_bdfWPwp2M76Yv7d1niWD2exFwlWVTXifXiNBvh9luE5cS6ERY7FFJutmdNm6x76tR2PGZaJgUKmOkrTx0D2nWVvL6Yl-zexp3wEXSlDZiwhbEEhrridw/s1600/part2_button_schema.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9jTARhSRiHXecKz_cj0xi4o_bdfWPwp2M76Yv7d1niWD2exFwlWVTXifXiNBvh9luE5cS6ERY7FFJutmdNm6x76tR2PGZaJgUKmOkrTx0D2nWVvL6Yl-zexp3wEXSlDZiwhbEEhrridw/s1600/part2_button_schema.png" height="215" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: Button circuit</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZxeTRr_Y6qvRlrN4vXgrPlOP5ASbW8uBAoE1D5Iil2cRh16So4PIlVponF04sHkpaUwT0vAhclkcfit-2vtMm2lSPlILIKrMASJ4SBDQcyjMWdGwQPHk8u2F3vnx2o9rJxbpgDIUURHo/s1600/part2_buzzer_schema.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZxeTRr_Y6qvRlrN4vXgrPlOP5ASbW8uBAoE1D5Iil2cRh16So4PIlVponF04sHkpaUwT0vAhclkcfit-2vtMm2lSPlILIKrMASJ4SBDQcyjMWdGwQPHk8u2F3vnx2o9rJxbpgDIUURHo/s1600/part2_buzzer_schema.png" height="231" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: Buzzer circuit</td></tr>
</tbody></table>
<div>
<br /></div>
<h3>
Images</h3>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNS0QBkPwRBWf33eQgsbBUxhA21RBnE0dlAOV4Py1cQO2fYRA_YEVbyxUpVCutr5MboKObY4IQmZk7D5oWTNbLg1ABnTIZb7Y5QdJO5ehbwajk_yU0DgmQsj5-1Wys1ddy9Jbtr-S_u7U/s1600/global_image.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNS0QBkPwRBWf33eQgsbBUxhA21RBnE0dlAOV4Py1cQO2fYRA_YEVbyxUpVCutr5MboKObY4IQmZk7D5oWTNbLg1ABnTIZb7Y5QdJO5ehbwajk_yU0DgmQsj5-1Wys1ddy9Jbtr-S_u7U/s1600/global_image.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 7: Global setup</td></tr>
</tbody></table>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPpiIY1ArRw1Z4_WyPwaZG9QYunEfVNAURWZGvNjQn5EzB6fq42ZnpfMTjxiapwdmU38w8mlvbEaL1_VYt0w3sV1pGVTsvQvGoYN-whVdpROPZXKkMDWNUplpdYTeQSTsOhVlvZw7wBYs/s1600/button_part.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPpiIY1ArRw1Z4_WyPwaZG9QYunEfVNAURWZGvNjQn5EzB6fq42ZnpfMTjxiapwdmU38w8mlvbEaL1_VYt0w3sV1pGVTsvQvGoYN-whVdpROPZXKkMDWNUplpdYTeQSTsOhVlvZw7wBYs/s1600/button_part.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 8: Button part setup</td></tr>
</tbody></table>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPOSAXdoW2-Ggur5RyQd0smw3PIeHkIuFBVVEd4Viu6BuCe-4AcCDHMY-O5l-fpMM_qrHBBYeYi1lGHB1sUj_ymS4VoG96Jkq9564iBEDqpuVh2ktnjfeSsm2P_ne751zZ-tEffGx033s/s1600/buzzer_part.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPOSAXdoW2-Ggur5RyQd0smw3PIeHkIuFBVVEd4Viu6BuCe-4AcCDHMY-O5l-fpMM_qrHBBYeYi1lGHB1sUj_ymS4VoG96Jkq9564iBEDqpuVh2ktnjfeSsm2P_ne751zZ-tEffGx033s/s1600/buzzer_part.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 9: Buzzer part setup</td></tr>
</tbody></table>
<h3 style="clear: both; text-align: left;">
</h3>
<h3 style="clear: both; text-align: left;">
Video</h3>
<div>
<br /></div>
<div style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/UvgrLmfVBVI" width="560"></iframe>
</div>
<div>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
</h3>
<h3 style="text-align: left;">
<br /></h3>
<h3 style="text-align: left;">
Button Part Code</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Button Part</span>
<span style="color: #60a0b0; font-style: italic;"> * While a song is not being played, the user can</span>
<span style="color: #60a0b0; font-style: italic;"> * introduce a song number using a button and sending</span>
<span style="color: #60a0b0; font-style: italic;"> * it to the buzzer part. Once it receives the song</span>
<span style="color: #60a0b0; font-style: italic;"> * name from the buzzer part, it prints it in the lcd</span>
<span style="color: #60a0b0; font-style: italic;"> * screen along with its songId until it has been</span>
<span style="color: #60a0b0; font-style: italic;"> * completely playedd.</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #007020;">#include <LiquidCrystal_I2C.h></span>
<span style="color: #007020;">#include <Wire.h> </span>
<span style="color: #60a0b0; font-style: italic;">// Set the LCD address to 0x27 for a 20 chars 4 line display</span>
<span style="color: #60a0b0; font-style: italic;">// Set the pins on the I2C chip used for LCD connections:</span>
<span style="color: #60a0b0; font-style: italic;">// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol</span>
<span style="color: #60a0b0; font-style: italic;">// Set the LCD I2C address</span>
LiquidCrystal_I2C <span style="color: #06287e;">lcd</span>(<span style="color: #40a070;">0x27</span>, <span style="color: #40a070;">2</span>, <span style="color: #40a070;">1</span>, <span style="color: #40a070;">0</span>, <span style="color: #40a070;">4</span>, <span style="color: #40a070;">5</span>, <span style="color: #40a070;">6</span>, <span style="color: #40a070;">7</span>, <span style="color: #40a070;">3</span>, POSITIVE);
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Constants</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Button that increases the songIndex</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> BUTTON <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #60a0b0; font-style: italic;">// Button that sends the song to the other XBee</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> ENTER <span style="color: #666666;">=</span> <span style="color: #40a070;">3</span>;
<span style="color: #60a0b0; font-style: italic;">// Char used to indicate the end of the song</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">char</span> END_CHAR <span style="color: #666666;">=</span> <span style="color: #4070a0;">'E'</span>;
<span style="color: #60a0b0; font-style: italic;">// String used to indicate the song id is not in the allowed range</span>
<span style="color: #007020; font-weight: bold;">const</span> String SONG_NOT_FOUND <span style="color: #666666;">=</span> <span style="color: #4070a0;">"Song not found"</span>;
<span style="color: #60a0b0; font-style: italic;">// Char used to indicate that the song title has been completely sent</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">char</span> END_SONG_TITLE_CHAR <span style="color: #666666;">=</span> <span style="color: #4070a0;">'\n'</span>;
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Global variables</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #902000;">int</span> songIndex <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
String songName <span style="color: #666666;">=</span> <span style="color: #4070a0;">""</span>;
boolean isPlayingSong <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Helper functions</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Prints information of a song in LCD screen</span>
<span style="color: #60a0b0; font-style: italic;">// First line: songIndex - Second line: songName</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">printCurrentInformation</span>()
{
lcd.clear();
lcd.setCursor(<span style="color: #40a070;">0</span>, <span style="color: #40a070;">0</span>);
lcd.print(songIndex);
lcd.setCursor(<span style="color: #40a070;">0</span>, <span style="color: #40a070;">1</span>);
lcd.print(songName);
}
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Main functions</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
pinMode(BUTTON, INPUT);
pinMode(ENTER, INPUT);
lcd.begin(<span style="color: #40a070;">16</span>,<span style="color: #40a070;">2</span>);
lcd.backlight();
printCurrentInformation();
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
<span style="color: #60a0b0; font-style: italic;">// If a song is being played, not allow to</span>
<span style="color: #60a0b0; font-style: italic;">// modify or send songIndex</span>
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">!</span>isPlayingSong)
{
<span style="color: #60a0b0; font-style: italic;">// Increments the songIndex very time is pressed</span>
<span style="color: #007020; font-weight: bold;">if</span> (digitalRead(BUTTON) <span style="color: #666666;">==</span> HIGH)
{
<span style="color: #666666;">++</span>songIndex;
printCurrentInformation();
delay(<span style="color: #40a070;">300</span>);
}
<span style="color: #60a0b0; font-style: italic;">// Sends the current selected song</span>
<span style="color: #007020; font-weight: bold;">if</span> (digitalRead(ENTER) <span style="color: #666666;">==</span> HIGH)
{
Serial.print(songIndex);
delay(<span style="color: #40a070;">300</span>);
}
}
<span style="color: #60a0b0; font-style: italic;">// If we are receiving sth through the serial connection...</span>
<span style="color: #007020; font-weight: bold;">if</span> (Serial.available() <span style="color: #666666;">></span> <span style="color: #40a070;">0</span>)
{
<span style="color: #60a0b0; font-style: italic;">// It would be easier if we did 'readString()' but</span>
<span style="color: #60a0b0; font-style: italic;">// some delay problems appeared. It is much faster</span>
<span style="color: #60a0b0; font-style: italic;">// it we receive the song name char by char</span>
<span style="color: #902000;">char</span> c <span style="color: #666666;">=</span> Serial.read();
<span style="color: #007020; font-weight: bold;">if</span> (isPlayingSong)
{
<span style="color: #60a0b0; font-style: italic;">// Reset the lcd screen showing that the song has ended</span>
<span style="color: #007020; font-weight: bold;">if</span> (c <span style="color: #666666;">==</span> END_CHAR)
{
songIndex <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
songName <span style="color: #666666;">=</span> <span style="color: #4070a0;">"End of the song"</span>;
isPlayingSong <span style="color: #666666;">=</span> <span style="color: #007020;">false</span>;
printCurrentInformation();
songName <span style="color: #666666;">=</span> <span style="color: #4070a0;">""</span>; <span style="color: #60a0b0; font-style: italic;">// restore song name</span>
}
}
<span style="color: #007020; font-weight: bold;">else</span>
{
<span style="color: #60a0b0; font-style: italic;">// Is the symbol that tells that the songName has been</span>
<span style="color: #60a0b0; font-style: italic;">// completely introduced</span>
<span style="color: #007020; font-weight: bold;">if</span> (c <span style="color: #666666;">==</span> END_SONG_TITLE_CHAR)
{
<span style="color: #60a0b0; font-style: italic;">// If the songIndex did not exist, </span>
<span style="color: #007020; font-weight: bold;">if</span> (songName <span style="color: #666666;">==</span> SONG_NOT_FOUND)
{
songIndex <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
printCurrentInformation();
songName <span style="color: #666666;">=</span> <span style="color: #4070a0;">""</span>;
}
<span style="color: #007020; font-weight: bold;">else</span>
{
isPlayingSong <span style="color: #666666;">=</span> <span style="color: #007020;">true</span>;
printCurrentInformation();
}
}
<span style="color: #007020; font-weight: bold;">else</span>
{
<span style="color: #60a0b0; font-style: italic;">// Keep forming the name of the song</span>
songName <span style="color: #666666;">+=</span> c;
}
}
}
}
</pre>
</td></tr>
</tbody></table>
</div>
<div>
<br /></div>
<h3>
<span style="line-height: 100%;">Buzzer Part Code</span></h3>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Buzzer Part</span>
<span style="color: #60a0b0; font-style: italic;"> * Plays the specified melody by the sender and</span>
<span style="color: #60a0b0; font-style: italic;"> * sends a text to the other node specyfing which</span>
<span style="color: #60a0b0; font-style: italic;"> * song is being played.</span>
<span style="color: #60a0b0; font-style: italic;"> *</span>
<span style="color: #60a0b0; font-style: italic;"> * Based on Brett Hagman code (www.roguerobotics.com)</span>
<span style="color: #60a0b0; font-style: italic;"> * http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Macros defining the notes features (from Brett Hagman code)</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #007020;">#define OCTAVE_OFFSET 0</span>
<span style="color: #007020;">#define TONE_PIN 5</span>
<span style="color: #007020;">#define NOTE_C4 262</span>
<span style="color: #007020;">#define NOTE_CS4 277</span>
<span style="color: #007020;">#define NOTE_D4 294</span>
<span style="color: #007020;">#define NOTE_DS4 311</span>
<span style="color: #007020;">#define NOTE_E4 330</span>
<span style="color: #007020;">#define NOTE_F4 349</span>
<span style="color: #007020;">#define NOTE_FS4 370</span>
<span style="color: #007020;">#define NOTE_G4 392</span>
<span style="color: #007020;">#define NOTE_GS4 415</span>
<span style="color: #007020;">#define NOTE_A4 440</span>
<span style="color: #007020;">#define NOTE_AS4 466</span>
<span style="color: #007020;">#define NOTE_B4 494</span>
<span style="color: #007020;">#define NOTE_C5 523</span>
<span style="color: #007020;">#define NOTE_CS5 554</span>
<span style="color: #007020;">#define NOTE_D5 587</span>
<span style="color: #007020;">#define NOTE_DS5 622</span>
<span style="color: #007020;">#define NOTE_E5 659</span>
<span style="color: #007020;">#define NOTE_F5 698</span>
<span style="color: #007020;">#define NOTE_FS5 740</span>
<span style="color: #007020;">#define NOTE_G5 784</span>
<span style="color: #007020;">#define NOTE_GS5 831</span>
<span style="color: #007020;">#define NOTE_A5 880</span>
<span style="color: #007020;">#define NOTE_AS5 932</span>
<span style="color: #007020;">#define NOTE_B5 988</span>
<span style="color: #007020;">#define NOTE_C6 1047</span>
<span style="color: #007020;">#define NOTE_CS6 1109</span>
<span style="color: #007020;">#define NOTE_D6 1175</span>
<span style="color: #007020;">#define NOTE_DS6 1245</span>
<span style="color: #007020;">#define NOTE_E6 1319</span>
<span style="color: #007020;">#define NOTE_F6 1397</span>
<span style="color: #007020;">#define NOTE_FS6 1480</span>
<span style="color: #007020;">#define NOTE_G6 1568</span>
<span style="color: #007020;">#define NOTE_GS6 1661</span>
<span style="color: #007020;">#define NOTE_A6 1760</span>
<span style="color: #007020;">#define NOTE_AS6 1865</span>
<span style="color: #007020;">#define NOTE_B6 1976</span>
<span style="color: #007020;">#define NOTE_C7 2093</span>
<span style="color: #007020;">#define NOTE_CS7 2217</span>
<span style="color: #007020;">#define NOTE_D7 2349</span>
<span style="color: #007020;">#define NOTE_DS7 2489</span>
<span style="color: #007020;">#define NOTE_E7 2637</span>
<span style="color: #007020;">#define NOTE_F7 2794</span>
<span style="color: #007020;">#define NOTE_FS7 2960</span>
<span style="color: #007020;">#define NOTE_G7 3136</span>
<span style="color: #007020;">#define NOTE_GS7 3322</span>
<span style="color: #007020;">#define NOTE_A7 3520</span>
<span style="color: #007020;">#define NOTE_AS7 3729</span>
<span style="color: #007020;">#define NOTE_B7 3951</span>
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Constants</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Notes array (from Brett Hagman code)</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> notes[] <span style="color: #666666;">=</span> { <span style="color: #40a070;">0</span>,
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};
<span style="color: #60a0b0; font-style: italic;">// Number of songs that can be played</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> N_SONGS <span style="color: #666666;">=</span> <span style="color: #40a070;">7</span>;
<span style="color: #60a0b0; font-style: italic;">// Char used to indicate the end of the song</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">char</span> END_CHAR <span style="color: #666666;">=</span> <span style="color: #4070a0;">'E'</span>;
<span style="color: #60a0b0; font-style: italic;">// String used to indicate the song id is not in the allowed range</span>
<span style="color: #007020; font-weight: bold;">const</span> String SONG_NOT_FOUND <span style="color: #666666;">=</span> <span style="color: #4070a0;">"Song not found"</span>;
<span style="color: #60a0b0; font-style: italic;">// Char used to indicate that the song title has been completely sent</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">char</span> END_SONG_TITLE_CHAR <span style="color: #666666;">=</span> <span style="color: #4070a0;">'\n'</span>;
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Global variables</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Array of songs to select them easily</span>
<span style="color: #902000;">char</span><span style="color: #666666;">*</span> songArray[N_SONGS];
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Helper functions</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Tells if a char is representing a digit or not</span>
boolean <span style="color: #06287e;">isDigit</span>(<span style="color: #902000;">char</span> c)
{
<span style="color: #007020; font-weight: bold;">return</span> (c <span style="color: #666666;">>=</span> <span style="color: #4070a0;">'0'</span> <span style="color: #666666;">&&</span> c <span style="color: #666666;"><=</span> <span style="color: #4070a0;">'9'</span>);
}
<span style="color: #60a0b0; font-style: italic;">// Play the specified song (from Brett Hagman code)</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">play_rtttl</span>(<span style="color: #902000;">char</span> <span style="color: #666666;">*</span>p)
{
<span style="color: #60a0b0; font-style: italic;">// Absolutely no error checking in here</span>
byte default_dur <span style="color: #666666;">=</span> <span style="color: #40a070;">4</span>;
byte default_oct <span style="color: #666666;">=</span> <span style="color: #40a070;">6</span>;
<span style="color: #902000;">int</span> bpm <span style="color: #666666;">=</span> <span style="color: #40a070;">63</span>;
<span style="color: #902000;">int</span> num;
<span style="color: #902000;">long</span> wholenote;
<span style="color: #902000;">long</span> duration;
byte note;
byte scale;
<span style="color: #60a0b0; font-style: italic;">// format: d=N,o=N,b=NNN:</span>
<span style="color: #60a0b0; font-style: italic;">// find the start (skip name, etc)</span>
<span style="color: #007020; font-weight: bold;">while</span>(<span style="color: #666666;">*</span>p <span style="color: #666666;">!=</span> <span style="color: #4070a0;">':'</span>) p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// ignore name</span>
p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip ':'</span>
<span style="color: #60a0b0; font-style: italic;">// get default duration</span>
<span style="color: #007020; font-weight: bold;">if</span>(<span style="color: #666666;">*</span>p <span style="color: #666666;">==</span> <span style="color: #4070a0;">'d'</span>)
{
p<span style="color: #666666;">++</span>; p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip "d="</span>
num <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #007020; font-weight: bold;">while</span>(isDigit(<span style="color: #666666;">*</span>p))
{
num <span style="color: #666666;">=</span> (num <span style="color: #666666;">*</span> <span style="color: #40a070;">10</span>) <span style="color: #666666;">+</span> (<span style="color: #666666;">*</span>p<span style="color: #666666;">++</span> <span style="color: #666666;">-</span> <span style="color: #4070a0;">'0'</span>);
}
<span style="color: #007020; font-weight: bold;">if</span>(num <span style="color: #666666;">></span> <span style="color: #40a070;">0</span>) default_dur <span style="color: #666666;">=</span> num;
p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip comma</span>
}
<span style="color: #60a0b0; font-style: italic;">//Serial.print("ddur: "); Serial.println(default_dur, 10);</span>
<span style="color: #60a0b0; font-style: italic;">// get default octave</span>
<span style="color: #007020; font-weight: bold;">if</span>(<span style="color: #666666;">*</span>p <span style="color: #666666;">==</span> <span style="color: #4070a0;">'o'</span>)
{
p<span style="color: #666666;">++</span>; p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip "o="</span>
num <span style="color: #666666;">=</span> <span style="color: #666666;">*</span>p<span style="color: #666666;">++</span> <span style="color: #666666;">-</span> <span style="color: #4070a0;">'0'</span>;
<span style="color: #007020; font-weight: bold;">if</span>(num <span style="color: #666666;">>=</span> <span style="color: #40a070;">3</span> <span style="color: #666666;">&&</span> num <span style="color: #666666;"><=</span><span style="color: #40a070;">7</span>) default_oct <span style="color: #666666;">=</span> num;
p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip comma</span>
}
<span style="color: #60a0b0; font-style: italic;">//Serial.print("doct: "); Serial.println(default_oct, 10);</span>
<span style="color: #60a0b0; font-style: italic;">// get BPM</span>
<span style="color: #007020; font-weight: bold;">if</span>(<span style="color: #666666;">*</span>p <span style="color: #666666;">==</span> <span style="color: #4070a0;">'b'</span>)
{
p<span style="color: #666666;">++</span>; p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip "b="</span>
num <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #007020; font-weight: bold;">while</span>(isDigit(<span style="color: #666666;">*</span>p))
{
num <span style="color: #666666;">=</span> (num <span style="color: #666666;">*</span> <span style="color: #40a070;">10</span>) <span style="color: #666666;">+</span> (<span style="color: #666666;">*</span>p<span style="color: #666666;">++</span> <span style="color: #666666;">-</span> <span style="color: #4070a0;">'0'</span>);
}
bpm <span style="color: #666666;">=</span> num;
p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip colon</span>
}
<span style="color: #60a0b0; font-style: italic;">//Serial.print("bpm: "); Serial.println(bpm, 10);</span>
<span style="color: #60a0b0; font-style: italic;">// BPM usually expresses the number of quarter notes per minute</span>
wholenote <span style="color: #666666;">=</span> (<span style="color: #40a070;">60</span> <span style="color: #666666;">*</span> <span style="color: #40a070;">1000L</span> <span style="color: #666666;">/</span> bpm) <span style="color: #666666;">*</span> <span style="color: #40a070;">4</span>; <span style="color: #60a0b0; font-style: italic;">// this is the time for whole note (in milliseconds)</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.print("wn: "); Serial.println(wholenote, 10);</span>
<span style="color: #60a0b0; font-style: italic;">// now begin note loop</span>
<span style="color: #007020; font-weight: bold;">while</span>(<span style="color: #666666;">*</span>p)
{
<span style="color: #60a0b0; font-style: italic;">// first, get note duration, if available</span>
num <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #007020; font-weight: bold;">while</span>(isDigit(<span style="color: #666666;">*</span>p))
{
num <span style="color: #666666;">=</span> (num <span style="color: #666666;">*</span> <span style="color: #40a070;">10</span>) <span style="color: #666666;">+</span> (<span style="color: #666666;">*</span>p<span style="color: #666666;">++</span> <span style="color: #666666;">-</span> <span style="color: #4070a0;">'0'</span>);
}
<span style="color: #007020; font-weight: bold;">if</span>(num) duration <span style="color: #666666;">=</span> wholenote <span style="color: #666666;">/</span> num;
<span style="color: #007020; font-weight: bold;">else</span> duration <span style="color: #666666;">=</span> wholenote <span style="color: #666666;">/</span> default_dur; <span style="color: #60a0b0; font-style: italic;">// we will need to check if we are a dotted note after</span>
<span style="color: #60a0b0; font-style: italic;">// now get the note</span>
note <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #007020; font-weight: bold;">switch</span>(<span style="color: #666666;">*</span>p)
{
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'c'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">1</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'d'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">3</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'e'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">5</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'f'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">6</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'g'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">8</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'a'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">10</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'b'</span>:
note <span style="color: #666666;">=</span> <span style="color: #40a070;">12</span>;
<span style="color: #007020; font-weight: bold;">break</span>;
<span style="color: #007020; font-weight: bold;">case</span> <span style="color: #4070a0;">'p'</span>:
<span style="color: #002070; font-weight: bold;">default:</span>
note <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
}
p<span style="color: #666666;">++</span>;
<span style="color: #60a0b0; font-style: italic;">// now, get optional '#' sharp</span>
<span style="color: #007020; font-weight: bold;">if</span>(<span style="color: #666666;">*</span>p <span style="color: #666666;">==</span> <span style="color: #4070a0;">'#'</span>)
{
note<span style="color: #666666;">++</span>;
p<span style="color: #666666;">++</span>;
}
<span style="color: #60a0b0; font-style: italic;">// now, get optional '.' dotted note</span>
<span style="color: #007020; font-weight: bold;">if</span>(<span style="color: #666666;">*</span>p <span style="color: #666666;">==</span> <span style="color: #4070a0;">'.'</span>)
{
duration <span style="color: #666666;">+=</span> duration<span style="color: #666666;">/</span><span style="color: #40a070;">2</span>;
p<span style="color: #666666;">++</span>;
}
<span style="color: #60a0b0; font-style: italic;">// now, get scale</span>
<span style="color: #007020; font-weight: bold;">if</span>(isDigit(<span style="color: #666666;">*</span>p))
{
scale <span style="color: #666666;">=</span> <span style="color: #666666;">*</span>p <span style="color: #666666;">-</span> <span style="color: #4070a0;">'0'</span>;
p<span style="color: #666666;">++</span>;
}
<span style="color: #007020; font-weight: bold;">else</span>
{
scale <span style="color: #666666;">=</span> default_oct;
}
scale <span style="color: #666666;">+=</span> OCTAVE_OFFSET;
<span style="color: #007020; font-weight: bold;">if</span> (<span style="color: #666666;">*</span>p <span style="color: #666666;">==</span> <span style="color: #4070a0;">','</span>)
p<span style="color: #666666;">++</span>; <span style="color: #60a0b0; font-style: italic;">// skip comma for next note (or we may be at the end)</span>
<span style="color: #60a0b0; font-style: italic;">// From line 248 to 256</span>
<span style="color: #60a0b0; font-style: italic;">// Added to code so end of the song signal is transmitted</span>
<span style="color: #60a0b0; font-style: italic;">// It is a very dirty way to send the END_CHAR but it was</span>
<span style="color: #60a0b0; font-style: italic;">// impossible to make it work by sending just the END_CHAR</span>
<span style="color: #902000;">char</span> songPlaying <span style="color: #666666;">=</span> <span style="color: #666666;">*</span>p;
<span style="color: #007020; font-weight: bold;">if</span> (songPlaying)
{
Serial.print(<span style="color: #4070a0;">" "</span>);
}
<span style="color: #007020; font-weight: bold;">else</span>
{
Serial.print(END_CHAR);
}
<span style="color: #60a0b0; font-style: italic;">// now play the note</span>
<span style="color: #007020; font-weight: bold;">if</span>(note)
{
<span style="color: #60a0b0; font-style: italic;">//Serial.print("Playing: ");</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.print(scale, 10); Serial.print(' ');</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.print(note, 10); Serial.print(" (");</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.print(notes[(scale - 4) * 12 + note], 10);</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.print(") ");</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.println(duration, 10);</span>
tone(TONE_PIN, notes[(scale <span style="color: #666666;">-</span> <span style="color: #40a070;">4</span>) <span style="color: #666666;">*</span> <span style="color: #40a070;">12</span> <span style="color: #666666;">+</span> note]);
delay(duration);
noTone(TONE_PIN);
}
<span style="color: #007020; font-weight: bold;">else</span>
{
<span style="color: #60a0b0; font-style: italic;">//Serial.print("Pausing: ");</span>
<span style="color: #60a0b0; font-style: italic;">//Serial.println(duration, 10);</span>
delay(duration);
}
}
}
<span style="color: #60a0b0; font-style: italic;">// Returns the name of a song given an input of the type</span>
<span style="color: #60a0b0; font-style: italic;">// "Name Song : Notes Features"</span>
String <span style="color: #06287e;">getSongName</span>(<span style="color: #902000;">char</span><span style="color: #666666;">*</span> p)
{
String songName <span style="color: #666666;">=</span> <span style="color: #4070a0;">""</span>;
<span style="color: #007020; font-weight: bold;">while</span> (<span style="color: #666666;">*</span>p <span style="color: #666666;">!=</span> <span style="color: #4070a0;">':'</span>)
{
songName <span style="color: #666666;">+=</span> <span style="color: #666666;">*</span>p;
<span style="color: #666666;">++</span>p;
}
<span style="color: #007020; font-weight: bold;">return</span> songName;
}
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Main functions</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>(<span style="color: #902000;">void</span>)
{
Serial.begin(<span style="color: #40a070;">9600</span>);
<span style="color: #60a0b0; font-style: italic;">// Put all desired songs in the array of songs</span>
songArray[<span style="color: #40a070;">0</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6"</span>;
songArray[<span style="color: #40a070;">1</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"Indiana:d=4,o=5,b=250:e,8p,8f,8g,8p,1c6,8p.,d,8p,8e,1f,p.,g,8p,8a,8b,8p,1f6,p,a,8p,8b,2c6,2d6,2e6,e,8p,8f,8g,8p,1c6,p,d6,8p,8e6,1f.6,g,8p,8g,e.6,8p,d6,8p,8g,e.6,8p,d6,8p,8g,f.6,8p,e6,8p,8d6,2c6"</span>;
songArray[<span style="color: #40a070;">2</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6,8e,c6,8e,2c.6,8c6,8d6,8d#6,8e6,8c6,8d6,e6,8b,d6,2c6,p,8d,8d#,8e,c6,8e,c6,8e,2c.6,8p,8a,8g,8f#,8a,8c6,e6,8d6,8c6,8a,2d6"</span>;
songArray[<span style="color: #40a070;">3</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"Looney:d=4,o=5,b=140:32p,c6,8f6,8e6,8d6,8c6,a.,8c6,8f6,8e6,8d6,8d#6,e.6,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6,8g,8a#,8a,8f"</span>;
songArray[<span style="color: #40a070;">4</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"StarWars:d=4,o=5,b=45:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6"</span>;
songArray[<span style="color: #40a070;">5</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"GoodBad:d=4,o=5,b=56:32p,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,d#,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,c#6,32a#,32d#6,32a#,32d#6,8a#.,16f#.,32f.,32d#.,c#,32a#,32d#6,32a#,32d#6,8a#.,16g#.,d#"</span>;
songArray[<span style="color: #40a070;">6</span>] <span style="color: #666666;">=</span> <span style="color: #4070a0;">"MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d"</span>;
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>(<span style="color: #902000;">void</span>)
{
<span style="color: #007020; font-weight: bold;">if</span> (Serial.available() <span style="color: #666666;">></span> <span style="color: #40a070;">0</span>)
{
<span style="color: #60a0b0; font-style: italic;">// Read the songNumber sent by the other node</span>
<span style="color: #902000;">int</span> songNumber <span style="color: #666666;">=</span> Serial.parseInt();
<span style="color: #60a0b0; font-style: italic;">// Check that the songNumber is inside the range of allowed values</span>
<span style="color: #007020; font-weight: bold;">if</span> (songNumber <span style="color: #666666;">>=</span> <span style="color: #40a070;">0</span> <span style="color: #666666;">&&</span> songNumber <span style="color: #666666;"><</span> N_SONGS)
{
<span style="color: #60a0b0; font-style: italic;">// Print song title</span>
Serial.print(getSongName(songArray[songNumber]) <span style="color: #666666;">+</span> END_SONG_TITLE_CHAR);
delay(<span style="color: #40a070;">300</span>);
<span style="color: #60a0b0; font-style: italic;">// Play the song</span>
play_rtttl(songArray[songNumber]);
}
<span style="color: #007020; font-weight: bold;">else</span>
{
<span style="color: #60a0b0; font-style: italic;">// Send error message to the other node</span>
Serial.print(SONG_NOT_FOUND <span style="color: #666666;">+</span> END_SONG_TITLE_CHAR);
delay(<span style="color: #40a070;">300</span>);
}
}
}
</pre>
</td></tr>
</tbody></table>
</div>
<h2>
<span style="line-height: 100%;">References</span></h2>
</div>
</div>
<div>
[1] Elements list for the system build. Retrieved from <a href="http://www.handsonwsn.org/guide.pdf" target="_blank">WSN course guide</a> on 25/04/2014.<br />
[2] Image "8.1 Wireless doorbell: switch layout". Retrieved from <a href="http://www.handsonwsn.org/guide.pdf" target="_blank">WSN Course Guide</a> on 25/04/2014.<br />
[3] Image "8.2 Wireless doorbell: buzzer layout". Retrieved from <a href="http://www.handsonwsn.org/guide.pdf" target="_blank">WSN Course Guide</a> on 25/04/2014.<br />
[4] Arduino Forums - Need help with a melody (<a href="http://forum.arduino.cc/index.php?topic=991.0" target="_blank">link</a>). Retrieved on 25/04/2014.<br />
[5] Arduino Star Wars Song for Piezo (<a href="https://gist.github.com/nickjamespdx/4736535" target="_blank">link</a>). Retrieved on 25/04/2014.</div>
Daniel Fureloshttp://www.blogger.com/profile/18259717346490561724noreply@blogger.com1tag:blogger.com,1999:blog-3219653273674708900.post-86774068886325335582014-04-10T23:21:00.005+02:002014-04-11T16:49:45.692+02:00Practice - Chatting with XBee<h2>
Description</h2>
<div>
<div style="text-align: justify;">
In this lab we have experimented with the XBee devices and how they can complement with Arduino. Firstly, we set a chat between two computers using the XBee's without Arduino; secondly, as an extra activity, we decided to establish a chat such as the messages sent from one computer through an XBee, were received by a second XBee connected to the Arduino. Moreover, these messages were shown through a LCD screen.</div>
</div>
<div>
<br /></div>
<div>
We used the following components:</div>
<div>
<ul>
<li>2 Digi XBee.</li>
<li>2 Xbee Explorer.</li>
<li>2 Type-A USB to mini-USB cables.</li>
<li>2 Computers.</li>
<li>1 Arduino Uno.</li>
<li>1 (or 2) Breadboard.</li>
<li>1 Display.</li>
<li>Several wires to interconnect the breadboard and Arduino.</li>
</ul>
The needed sofware to complete the task is the following:<br />
<ul>
<li><a href="http://ftp1.digi.com/support/utilities/40003026_A.exe" target="_blank">XCTU Software</a></li>
</ul>
<ul>
<li><a href="http://www.ftdichip.com/Drivers/CDM/CDM%20v2.10.00%20WHQL%20Certified.exe" target="_blank">VCP drivers for FTDI</a></li>
</ul>
<ul>
<li><a href="https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads" target="_blank">Liquid Crystal Library (for LCD screen)</a></li>
</ul>
</div>
<div>
<h2>
Part 1: Simple Chat</h2>
</div>
<div>
<span style="text-align: justify;">The first step consists in achieving communication between two machines connected to the following set:</span><br />
<span style="text-align: justify;"><br /></span>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOG9iMy_UNFtfSI_45AYdeq7GNLE1DKQKLETDrEGG9X7OT4GWiuYTpQpZdhibxyeqxcUfL7zgNxb3SEWX7fumlYssnLcjOKfJdTXAatfcWqOOZU92A6t9mos698f2NXk1l5L1PLgIOZz4/s1600/montaje+p2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOG9iMy_UNFtfSI_45AYdeq7GNLE1DKQKLETDrEGG9X7OT4GWiuYTpQpZdhibxyeqxcUfL7zgNxb3SEWX7fumlYssnLcjOKfJdTXAatfcWqOOZU92A6t9mos698f2NXk1l5L1PLgIOZz4/s1600/montaje+p2.png" height="198" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 1: Xbee Module</td></tr>
</tbody></table>
<div style="text-align: justify;">
<br />
We used the software XCTU to configure the devices and to set the PAN ID. We can also implement some stuff like changing device’s name, applying security, changing transmission power, etc.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
After connecting the XBee modules to the computer, we must set a coordinator and a router as follows:<br />
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_5c9MOv7-CVYnk8HbQRmT0zV8fjnPEt_t7aJp_Xc5ujDLmrLYx4Cc_28vP2Na4_bOa0kkAZwhYbkDvpVndlHyGeRnyFZnIPbxMvpHbGs_VfCzmkHvBbl5GQK8dyWDTkLL5k_Q0xaaEyg/s1600/1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_5c9MOv7-CVYnk8HbQRmT0zV8fjnPEt_t7aJp_Xc5ujDLmrLYx4Cc_28vP2Na4_bOa0kkAZwhYbkDvpVndlHyGeRnyFZnIPbxMvpHbGs_VfCzmkHvBbl5GQK8dyWDTkLL5k_Q0xaaEyg/s1600/1.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 2: Coordinator setting</td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
After setting one of the XBee module as a coordinator, we should see this on the XCTU screen:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil_-aZwWGpHyDHGjqq7bL7wilWt1h8u5sajn-9b8IS36QuqrCNTjqgFbcZcsDtGuWK7p_0Zw2wlhLHFYRm21edg3j_KhnF7xklaHSL_1mSleNVu9a_BulwzlLp3oE5-tqnWtfJFBWLuZ4/s1600/2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEil_-aZwWGpHyDHGjqq7bL7wilWt1h8u5sajn-9b8IS36QuqrCNTjqgFbcZcsDtGuWK7p_0Zw2wlhLHFYRm21edg3j_KhnF7xklaHSL_1mSleNVu9a_BulwzlLp3oE5-tqnWtfJFBWLuZ4/s1600/2.png" height="62" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 3: Coordinator setting</td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
We do the same for setting the other XBee module as router:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqCCTNn9P3vItgn3MXyAicBPpe5cvtNxHr_7AuYFWuDrIN_QbM_sUuU1GFbEBm5CbQQ80tcJoQpG-uqjFf2FyaonHWvgI6CJ3v0AOIYGRN00AWLAjk9qbZk14qGOutlksQP17Ruyy2vJE/s1600/3.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqCCTNn9P3vItgn3MXyAicBPpe5cvtNxHr_7AuYFWuDrIN_QbM_sUuU1GFbEBm5CbQQ80tcJoQpG-uqjFf2FyaonHWvgI6CJ3v0AOIYGRN00AWLAjk9qbZk14qGOutlksQP17Ruyy2vJE/s1600/3.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 4: Router setting</td></tr>
</tbody></table>
<br />
<div style="text-align: justify;">
We check that the change has been done successfully:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGIhZnQbj7JaJlqByueLajQoA_add7UZBIkDY66ilrGxCfpYTPymjJUQftLfXrOFqHJwD23-kh4iPGKtMc54hc9Em21N-LDoIQBsJwmzbJszJrwKxwIHqD0VFJiVkJcFmk0PmYKsjb2qk/s1600/4.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGIhZnQbj7JaJlqByueLajQoA_add7UZBIkDY66ilrGxCfpYTPymjJUQftLfXrOFqHJwD23-kh4iPGKtMc54hc9Em21N-LDoIQBsJwmzbJszJrwKxwIHqD0VFJiVkJcFmk0PmYKsjb2qk/s1600/4.png" height="60" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 5: Router checking</td></tr>
</tbody></table>
<span style="font-family: 'Times New Roman', serif; font-size: 16px; text-align: justify;"><br /></span></div>
<div>
<div style="text-align: justify;">
Once the configuration has been done and the PAN ID is the same for both devices, we can run the “discover nodes in the same network” functionality:</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjncL259OP1PQPhHtQTr9TG5U9pFkYm7FpN9-V2bKc3Dvw-mV9_srjLrk6dQ2-DhMYJ0yvULjJ8Tt9ZwRGXNGZTGgeTgHpuujwOsDiUgDMRPYliH9vB6Dq2qIBGtj_2aK2TvKIpGicQmis/s1600/5.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjncL259OP1PQPhHtQTr9TG5U9pFkYm7FpN9-V2bKc3Dvw-mV9_srjLrk6dQ2-DhMYJ0yvULjJ8Tt9ZwRGXNGZTGgeTgHpuujwOsDiUgDMRPYliH9vB6Dq2qIBGtj_2aK2TvKIpGicQmis/s1600/5.png" height="62" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 6: Device discovered</td></tr>
</tbody></table>
</div>
<div>
<br />
<div style="text-align: justify;">
The last step for chatting is switching to the consoles working mode and opening the serial connection with the radio module. The figure below shows a conversation maintained between two ZigBee modules connected to different computers.</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZrkv5kpLGUe5dbQkf93jQdAPr6zGSLSHKQkVl4TCL4vzzFySufxUtsfDGnxGRt751tczJbduz9Fc5DNx2zIpAHM2ZE-7mxkC1RM4ykI9yPNknVTb4BDMF_mpiebDTADQNslkloLyLl-M/s1600/6.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZrkv5kpLGUe5dbQkf93jQdAPr6zGSLSHKQkVl4TCL4vzzFySufxUtsfDGnxGRt751tczJbduz9Fc5DNx2zIpAHM2ZE-7mxkC1RM4ykI9yPNknVTb4BDMF_mpiebDTADQNslkloLyLl-M/s1600/6.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 7: Chatting</td></tr>
</tbody></table>
<div style="text-align: justify;">
For more specific details, see the YouTube video attached at the end of the post.</div>
<h2>
Part 2: Chat display</h2>
<div>
<div class="MsoNormal" style="margin-bottom: 0cm; text-align: justify;">
In this second part we are going to show how we can display the information transmitted between two XBee in a screen connected to Arduino. To do that, we need to understand how the XBee Explorer works:<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheGZsCUPKe36xbhrfwZbQ3pu6uVYmJwACrM5r3pYMKhS5lCu_gkrGIawuVglylI7I-FaX2IEjtGdH7jmWwvmkHPfm0B_JV8-21xOrFuwNbmvqW2ezIwnxSJ8WjWUh3fhd40kd1zS9JFlM/s1600/explorer.png" imageanchor="1" style="font-family: 'Times New Roman', serif; font-size: 16px; margin-left: auto; margin-right: auto; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheGZsCUPKe36xbhrfwZbQ3pu6uVYmJwACrM5r3pYMKhS5lCu_gkrGIawuVglylI7I-FaX2IEjtGdH7jmWwvmkHPfm0B_JV8-21xOrFuwNbmvqW2ezIwnxSJ8WjWUh3fhd40kd1zS9JFlM/s1600/explorer.png" height="301" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 8: XBee Explorer</td></tr>
</tbody></table>
</div>
<div class="MsoNormal" style="margin-bottom: 0cm; text-align: justify;">
The previous figure shows the pins we must connect to the Arduino in order to be able to display the information sent by the other XBee module. We note that the input voltage must be 3.3V instead of 5V to avoid potential problems (however, we can use the 5V pin placed at the upper-right side). The XBee Explorer is not only important to connect the XBee to the computer, but also to put it in the breadboard so as connections can be set (later we will show the figure).<br />
<br />
The connection between the XBee modules must be implemented as shown on part 1. In this case, however, we have set that one of the XBee's acts as a coordinator and the other one as an end-device (it could be used as a router too).<br />
<br />
One of the main aspects we have to take into account when establishing the connection is that if we send broadcast messages, these messages will probably arrived much more delayed than in the case were we have specified the addresses. Moreover, it is very useful to observe that until RSSI leds in each XBee Explorer are not high, messages will not be sent (so be patient).<br />
<br />
Once connectivity between the two XBee modules has been established, we can implement the part where we display the information in a screen. The image below also shows the connections done for our purpose and we can observe that we have the same type of pins as for ZigBee Explorer: Voltage input (5V), Ground, TX and RX.<br />
<h2>
Circuit schema</h2>
<div>
The image below shows the circuit. We must say that is not exactly the schema we followed because there are some pieces that are not included in the software (Fritzing): XBee Explorer (which is not the same as the one put in the image) and an I2C module we used for the LCD screen (see figure 15). At the link [5] you will find information on how to use I2C module.</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1grZG9LrYBBVq1WzRZB2U0SCJMTnLB0up5xEFt7OhUTU9z_Lm5uDq8-aFXV0JWwTnSDWqAVK26jQnnu8dwp-IQv3ypoYmtNKznsEOrghf0QF7RH82GkerIE-_yXBtq-6vT8cZ3h2nvU4/s1600/p2_circuit.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1grZG9LrYBBVq1WzRZB2U0SCJMTnLB0up5xEFt7OhUTU9z_Lm5uDq8-aFXV0JWwTnSDWqAVK26jQnnu8dwp-IQv3ypoYmtNKznsEOrghf0QF7RH82GkerIE-_yXBtq-6vT8cZ3h2nvU4/s1600/p2_circuit.jpg" height="251" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 9: Physical Schema</td></tr>
</tbody></table>
</div>
<div class="MsoNormal" style="margin-bottom: 0cm; text-align: justify;">
<h2 style="text-align: start;">
Circuit photographs</h2>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvWVZLXYju3IS6Uk3EMIBGDUjlRZkSIkJdcesbzPBLwFXAm9EwsI8d71Rk3fwYVdzWdLWUswXL0ZW6o-WCOUDzQhykzgmAOBC0Woh0GvX2iOchWXbgwC0Dr8kPwuvWiIkgEq5cNdaizXw/s1600/IMG_20140410_204350.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvWVZLXYju3IS6Uk3EMIBGDUjlRZkSIkJdcesbzPBLwFXAm9EwsI8d71Rk3fwYVdzWdLWUswXL0ZW6o-WCOUDzQhykzgmAOBC0Woh0GvX2iOchWXbgwC0Dr8kPwuvWiIkgEq5cNdaizXw/s1600/IMG_20140410_204350.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 10: General image of the circuit</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJuaLcpDF3s0BUWF1NhfpFHjeYLy54WAl9Ckr1KU5rZ5pYjFoBD6qyrYGF-lPYdi4huEesciBMPgdvKMmCqGBE1VqfOdhkDJwQgM79lihkNAWqGCEI82pUMIaghU1YaBdWqsDIReK1YTc/s1600/IMG_20140410_204423.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJuaLcpDF3s0BUWF1NhfpFHjeYLy54WAl9Ckr1KU5rZ5pYjFoBD6qyrYGF-lPYdi4huEesciBMPgdvKMmCqGBE1VqfOdhkDJwQgM79lihkNAWqGCEI82pUMIaghU1YaBdWqsDIReK1YTc/s1600/IMG_20140410_204423.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 11: General image of the circuit</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid4OhjZWViDrV45pLNs1ycElJ0qoJyvBgYCBqWIz1wvkKMyEujRk_l-lTNMFhWW1mWkY-LINdsKCnUjVZVLNDGsFWwQ9QM7XlT06Eyy3m6JH2lFWEpsRZUENjwTwcAUI9VucpPN46XIgw/s1600/IMG_20140410_204504.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid4OhjZWViDrV45pLNs1ycElJ0qoJyvBgYCBqWIz1wvkKMyEujRk_l-lTNMFhWW1mWkY-LINdsKCnUjVZVLNDGsFWwQ9QM7XlT06Eyy3m6JH2lFWEpsRZUENjwTwcAUI9VucpPN46XIgw/s1600/IMG_20140410_204504.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 12: Zoom in the Arduino board</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtyTM0IRNPc-Ji0wMoEssPBNNZpUgeuY4ab2r2cfThA2RQ2KKWfm4fngc4Krlmr2VHMb0_REw2hJScJ_hepLKZHIDQcFlr1fnCaeNfc5UvpZx84h7Wm9u0Q3KLUAEJwAu9OCTKut7HrI0/s1600/IMG_20140410_204517.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtyTM0IRNPc-Ji0wMoEssPBNNZpUgeuY4ab2r2cfThA2RQ2KKWfm4fngc4Krlmr2VHMb0_REw2hJScJ_hepLKZHIDQcFlr1fnCaeNfc5UvpZx84h7Wm9u0Q3KLUAEJwAu9OCTKut7HrI0/s1600/IMG_20140410_204517.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 13: Zoom in the XBee (receiver) module part</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLcJcYEYxiqIl7YbLeiBPIb_2EtLjCi6vAx17cwuV_2lyxd7j4tfcdumsWH2rTM95Sk1uTbwi0QQrZ_QhlzkAQ4NEbXlb_XYgHQiKuXvdglpFDUp5bW1JJSjYcP2_FHQHqaxwXj55Zoig/s1600/IMG_20140410_204549.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLcJcYEYxiqIl7YbLeiBPIb_2EtLjCi6vAx17cwuV_2lyxd7j4tfcdumsWH2rTM95Sk1uTbwi0QQrZ_QhlzkAQ4NEbXlb_XYgHQiKuXvdglpFDUp5bW1JJSjYcP2_FHQHqaxwXj55Zoig/s1600/IMG_20140410_204549.jpg" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 14: Zoom in the LCD screen part</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitgOw_TVvptB1upUqWGDihR3a0cNMEaZfneaU8G5BiOM9h53gfs59IeOPzU_umAqdxlozRb-E6pf4yjWhXc9Q4pRFFGqTR0SGitc7sSZrvK2BQ7obawgTtr8CBC05C_WSPVWkqNN9n7Ew/s1600/IMG_20140410_231212.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitgOw_TVvptB1upUqWGDihR3a0cNMEaZfneaU8G5BiOM9h53gfs59IeOPzU_umAqdxlozRb-E6pf4yjWhXc9Q4pRFFGqTR0SGitc7sSZrvK2BQ7obawgTtr8CBC05C_WSPVWkqNN9n7Ew/s1600/IMG_20140410_231212.jpg" height="320" width="240" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Figure 15: The I2C module</td></tr>
</tbody></table>
<h2 style="text-align: start;">
Video</h2>
<div style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/bXW44xJnPFE?rel=0" width="560"></iframe>
</div>
<div>
<br /></div>
</div>
<div class="MsoNormal" style="margin-bottom: 0cm; text-align: justify;">
</div>
</div>
<h2>
Code</h2>
<div style="text-align: justify;">
Now we can see two versions that correspond to the second part explained before. In both cases some characters written from the XBee connected from a computer are received in another XBee and shown through the display. However, there are some differences that are explained in the header comments of the code.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
You can download the code from the <a href="http://getting-into-arduino.blogspot.com.es/p/code.html" target="_blank">code section</a>.</div>
<h3>
</h3>
<h3>
Version 1</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Chatting with Xbee - Version 1</span>
<span style="color: #60a0b0; font-style: italic;"> * In this version an Xbee connected to XCTU software sends a</span>
<span style="color: #60a0b0; font-style: italic;"> * message finished by an INTRO (like in a real chat). Because</span>
<span style="color: #60a0b0; font-style: italic;"> * of display length restrictions, just those characters that</span>
<span style="color: #60a0b0; font-style: italic;"> * fit in the screen are displayed.</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #007020;">#include <LiquidCrystal_I2C.h></span>
<span style="color: #007020;">#include <SoftwareSerial.h></span>
<span style="color: #007020;">#include <Wire.h> </span>
<span style="color: #60a0b0; font-style: italic;">// Set the LCD address to 0x27 for a 20 chars 4 line display</span>
<span style="color: #60a0b0; font-style: italic;">// Set the pins on the I2C chip used for LCD connections:</span>
<span style="color: #60a0b0; font-style: italic;">// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol</span></pre>
<pre style="line-height: 125%; margin: 0;"><pre><span style="color: #60a0b0; font-style: italic;">// Set the LCD I2C address</span></pre>
LiquidCrystal_I2C <span style="color: #06287e;">lcd</span>(<span style="color: #40a070;">0x27</span>, <span style="color: #40a070;">2</span>, <span style="color: #40a070;">1</span>, <span style="color: #40a070;">0</span>, <span style="color: #40a070;">4</span>, <span style="color: #40a070;">5</span>, <span style="color: #40a070;">6</span>, <span style="color: #40a070;">7</span>, <span style="color: #40a070;">3</span>, POSITIVE);
SoftwareSerial <span style="color: #06287e;">xbee</span>(<span style="color: #40a070;">0</span>, <span style="color: #40a070;">1</span>); <span style="color: #60a0b0; font-style: italic;">// Pin: RX, TX</span>
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Constants</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Number of flashes before starting to show the characters</span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">// received</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> N_FLASHES <span style="color: #666666;">=</span> <span style="color: #40a070;">3</span>;
<span style="color: #60a0b0; font-style: italic;">// Number of rows in the display</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> N_ROWS <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #60a0b0; font-style: italic;">// Number of characters in each row</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> MAX_CHARS_PER_ROW <span style="color: #666666;">=</span> <span style="color: #40a070;">16</span>;
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Global variables</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// String that will be formed from the chars sent by the other xbee</span>
String input <span style="color: #666666;">=</span> <span style="color: #4070a0;">""</span>;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
<span style="color: #60a0b0; font-style: italic;">// Initialize the lcd for 16 chars 2 lines, turn on backlight</span>
lcd.begin(MAX_CHARS_PER_ROW, N_ROWS);
<span style="color: #60a0b0; font-style: italic;">// Quick 3 flashes of backlight </span>
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> N_FLASHES; <span style="color: #666666;">++</span>i)
{
lcd.backlight();
delay(<span style="color: #40a070;">250</span>);
lcd.noBacklight();
delay(<span style="color: #40a070;">250</span>);
}
lcd.backlight(); <span style="color: #60a0b0; font-style: italic;">// finish with backlight on </span>
<span style="color: #60a0b0; font-style: italic;">// set the data rate for the SoftwareSerial port</span>
xbee.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
<span style="color: #60a0b0; font-style: italic;">// If the xbee is receiving something...</span>
<span style="color: #007020; font-weight: bold;">if</span> (xbee.available())
{
<span style="color: #60a0b0; font-style: italic;">// Read the character received from the other xbee</span>
<span style="color: #902000;">char</span> c <span style="color: #666666;">=</span> xbee.read();
<span style="color: #60a0b0; font-style: italic;">// If INTRO key is pressed, print the string in the display</span>
<span style="color: #60a0b0; font-style: italic;">// Otherwise, keep reading it</span>
<span style="color: #007020; font-weight: bold;">if</span> (c <span style="color: #666666;">==</span> <span style="color: #40a070;">13</span> <span style="color: #666666;">||</span> c <span style="color: #666666;">==</span> <span style="color: #40a070;">10</span>)
{
printStringInLCD();
input <span style="color: #666666;">=</span> <span style="color: #4070a0;">""</span>;
}
<span style="color: #007020; font-weight: bold;">else</span>
{
input <span style="color: #666666;">+=</span> c;
}
}
}
<span style="color: #60a0b0; font-style: italic;">// Updates the LCD parameters (row, position in row) depending on</span>
<span style="color: #60a0b0; font-style: italic;">// the current position</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">printStringInLCD</span>()
{
<span style="color: #60a0b0; font-style: italic;">// Size restriction of the display</span>
<span style="color: #902000;">int</span> length <span style="color: #666666;">=</span> min(input.length(), MAX_CHARS_PER_ROW <span style="color: #666666;">*</span> N_ROWS);
<span style="color: #902000;">int</span> row <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #902000;">int</span> pos <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #60a0b0; font-style: italic;">// Important to clear the display before writting new text</span>
lcd.clear();
<span style="color: #60a0b0; font-style: italic;">// Print char by char in order to take advantage from all </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> // the display</span>. <span style="color: #60a0b0; font-style: italic;">The string can also be divided in two parts</span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> // and print them separately</span>
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> length; <span style="color: #666666;">++</span>i)
{
pos <span style="color: #666666;">=</span> i <span style="color: #666666;">%</span> MAX_CHARS_PER_ROW;
<span style="color: #007020; font-weight: bold;">if</span> (i <span style="color: #666666;">==</span> MAX_CHARS_PER_ROW)
{
<span style="color: #666666;">++</span>row;
}
lcd.setCursor(pos, row);
lcd.print(input.charAt(i));
}
}
</pre>
</td></tr>
</tbody></table>
</div>
<h3>
</h3>
<h3>
Version 2</h3>
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Chatting with Xbee - Version 2</span>
<span style="color: #60a0b0; font-style: italic;"> * In this version an Xbee connected to XCTU software sends an </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> * infinite</span> <span style="color: #60a0b0; font-style: italic;">message to the Xbee connected to the Arduino.</span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> * The message is displayed</span> <span style="color: #60a0b0; font-style: italic;">character by character while the </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> * message is written in XCTU. If the</span> <span style="color: #60a0b0; font-style: italic;">message is filled with </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> * characters, then the screen is cleared.</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #007020;">#include <LiquidCrystal_I2C.h></span>
<span style="color: #007020;">#include <SoftwareSerial.h></span>
<span style="color: #007020;">#include <Wire.h> </span>
<span style="color: #60a0b0; font-style: italic;">// Set the LCD address to 0x27 for a 20 chars 4 line display</span>
<span style="color: #60a0b0; font-style: italic;">// Set the pins on the I2C chip used for LCD connections:</span>
<span style="color: #60a0b0; font-style: italic;">// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol</span></pre>
<pre style="line-height: 125%; margin: 0;"><pre><span style="color: #60a0b0; font-style: italic;">// Set the LCD I2C address</span></pre>
</pre>
<pre style="line-height: 125%; margin: 0;">LiquidCrystal_I2C <span style="color: #06287e;">lcd</span>(<span style="color: #40a070;">0x27</span>, <span style="color: #40a070;">2</span>, <span style="color: #40a070;">1</span>, <span style="color: #40a070;">0</span>, <span style="color: #40a070;">4</span>, <span style="color: #40a070;">5</span>, <span style="color: #40a070;">6</span>, <span style="color: #40a070;">7</span>, <span style="color: #40a070;">3</span>, POSITIVE);</pre>
<pre style="line-height: 125%; margin: 0;">SoftwareSerial <span style="color: #06287e;">xbee</span>(<span style="color: #40a070;">0</span>, <span style="color: #40a070;">1</span>); <span style="color: #60a0b0; font-style: italic;">// Pin: RX, TX</span>
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Constants</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Number of flashes before starting to show the characters </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">// received</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> N_FLASHES <span style="color: #666666;">=</span> <span style="color: #40a070;">3</span>;
<span style="color: #60a0b0; font-style: italic;">// Number of rows in the display</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> N_ROWS <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #60a0b0; font-style: italic;">// Number of characters in each row</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> MAX_CHARS_PER_ROW <span style="color: #666666;">=</span> <span style="color: #40a070;">16</span>;
<span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Global variables</span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #902000;">int</span> currentRow <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #902000;">int</span> nextRow <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #902000;">int</span> currentPos <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #902000;">int</span> nextPos <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
<span style="color: #60a0b0; font-style: italic;">// Initialize the lcd for 16 chars 2 lines, turn on backlight</span>
lcd.begin(MAX_CHARS_PER_ROW, N_ROWS);
<span style="color: #60a0b0; font-style: italic;">// Quick 3 flashes of backlight </span>
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> N_FLASHES; <span style="color: #666666;">++</span>i)
{
lcd.backlight();
delay(<span style="color: #40a070;">250</span>);
lcd.noBacklight();
delay(<span style="color: #40a070;">250</span>);
}
lcd.backlight(); <span style="color: #60a0b0; font-style: italic;">// finish with backlight on </span>
<span style="color: #60a0b0; font-style: italic;">// set the data rate for the SoftwareSerial port</span>
xbee.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
<span style="color: #60a0b0; font-style: italic;">// If the xbee is receiving something...</span>
<span style="color: #007020; font-weight: bold;">if</span> (xbee.available())
{
updateLCDParams();
<span style="color: #60a0b0; font-style: italic;">// Set the cursor position in the display </span>
lcd.setCursor(currentPos, currentRow);
<span style="color: #60a0b0; font-style: italic;">// Read the character received from the other xbee</span>
<span style="color: #902000;">char</span> c <span style="color: #666666;">=</span> xbee.read();
<span style="color: #60a0b0; font-style: italic;">// If INTRO key is pressed...</span>
<span style="color: #007020; font-weight: bold;">if</span> (c <span style="color: #666666;">==</span> <span style="color: #40a070;">13</span> <span style="color: #666666;">||</span> c <span style="color: #666666;">==</span> <span style="color: #40a070;">10</span>)
{
nextRow <span style="color: #666666;">=</span> (currentRow<span style="color: #666666;">+</span><span style="color: #40a070;">1</span>) <span style="color: #666666;">%</span> N_ROWS;
nextPos <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
}
<span style="color: #007020; font-weight: bold;">else</span>
{
<span style="color: #60a0b0; font-style: italic;">// Print char throught the display</span>
lcd.print(c);
<span style="color: #60a0b0; font-style: italic;">// If we reach the end of the current row...</span>
<span style="color: #007020; font-weight: bold;">if</span> (currentPos <span style="color: #666666;">==</span> MAX_CHARS_PER_ROW<span style="color: #666666;">-</span><span style="color: #40a070;">1</span>)
{
nextRow <span style="color: #666666;">=</span> (currentRow<span style="color: #666666;">+</span><span style="color: #40a070;">1</span>) <span style="color: #666666;">%</span> N_ROWS;
nextPos <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
}
<span style="color: #007020; font-weight: bold;">else</span>
{
nextRow <span style="color: #666666;">=</span> currentRow;
nextPos <span style="color: #666666;">=</span> (currentPos<span style="color: #666666;">+</span><span style="color: #40a070;">1</span>) <span style="color: #666666;">%</span> MAX_CHARS_PER_ROW;
}
}
}
}
<span style="color: #60a0b0; font-style: italic;">// Updates the LCD parameters (row, position in row) depending on</span>
<span style="color: #60a0b0; font-style: italic;">// the current position</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">updateLCDParams</span>()
{
currentRow <span style="color: #666666;">=</span> nextRow;
currentPos <span style="color: #666666;">=</span> nextPos;
<span style="color: #007020; font-weight: bold;">if</span> (currentRow <span style="color: #666666;">==</span> <span style="color: #40a070;">0</span> <span style="color: #666666;">&&</span> currentPos <span style="color: #666666;">==</span> <span style="color: #40a070;">0</span>)
{
lcd.clear();
}
}</pre>
</td></tr>
</tbody></table>
</div>
<h2>
References</h2>
</div>
<div>
<div style="text-align: left;">
[1] Lab 4.1 Wireless messaging Moodle: <a href="http://youtu.be/qz6KgNB339M" target="_blank"><span id="goog_1892051718"></span>http://youtu.be/qz6KgNB339M<span id="goog_1892051719"></span></a></div>
<div style="text-align: left;">
[2] How to Connect an Arduino to an Xbee Radio: <a href="http://answers.oreilly.com/topic/2458-how-to-connect-an-arduino-to-an-xbee-radio/">http://answers.oreilly.com/topic/2458-how-to-connect-an-arduino-to-an-xbee-radio/</a></div>
<div style="text-align: left;">
[3] Arduino and XBee Communication: <a href="http://cs.smith.edu/dftwiki/index.php/Tutorial:_Arduino_and_XBee_Communication">http://cs.smith.edu/dftwiki/index.php/Tutorial:_Arduino_and_XBee_Communication</a></div>
<div style="text-align: left;">
[4] Let's make XBee talk: <a href="http://www.norwegiancreations.com/2013/10/arduino-tutorial-1-lets-make-xbee-talk/">http://www.norwegiancreations.com/2013/10/arduino-tutorial-1-lets-make-xbee-talk/</a><br />
[5] LCD with I2C Module: <a href="http://www.handsonwsn.org/forum/index.php?PHPSESSID=796aba16711e172869e84cead5facd52&topic=5.msg5#new">http://www.handsonwsn.org/forum/index.php?PHPSESSID=796aba16711e172869e84cead5facd52&topic=5.msg5#new</a></div>
</div>
Daniel Fureloshttp://www.blogger.com/profile/18259717346490561724noreply@blogger.com0tag:blogger.com,1999:blog-3219653273674708900.post-27060094963127828772014-04-03T22:04:00.000+02:002014-04-09T19:41:46.178+02:00Practice - Blinking LED<div style="text-align: justify;">
<h2>
Description</h2>
In this practice we had to deal with LEDs (Light Emitting Diode) in order to perform a useful system; concretely, a program was developed to turn on and turn off the LEDs after a certain amount of time (moreover, we could do the same using switches/buttons). This task formed the minimum requirements to start experimenting with more complex ideas.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
One of the main issues we found consisted in deciding how to put the button in the breadboard to turn on and turn off the LEDs. As you will see later in this entry, we have seen that when the button is switched on, the terminals on the same side become connected; otherwise, they are not connected. This fact can be observed on the following image, where terminals A and C are directly connected (as B and D). When the button is switched on, the current flows through all the nodes.<br />
<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOPBHK74XjrtHK3hPHkGMOA1RqwAV90sqlwnNCemY_tfZ2hpQmlhUXHLC8A8f5kX6-29_hXXkIGFaCuWkC1Ts1SXZMMmRf_8AlLdahyphenhyphenCGyXMNixpOeV9AVPR7h2IW_qZhWBy7uFzG7ib4/s1600/pushbutton_legs_final.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOPBHK74XjrtHK3hPHkGMOA1RqwAV90sqlwnNCemY_tfZ2hpQmlhUXHLC8A8f5kX6-29_hXXkIGFaCuWkC1Ts1SXZMMmRf_8AlLdahyphenhyphenCGyXMNixpOeV9AVPR7h2IW_qZhWBy7uFzG7ib4/s1600/pushbutton_legs_final.jpg" height="223" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><span style="font-size: x-small;">Image 1: Button functionality [1]</span></td></tr>
</tbody></table>
</div>
<div style="text-align: center;">
<br /></div>
<div style="text-align: justify;">
The additional work we did consisted in creating a traffic light using three different LED: red, yellow and green. We thought it was a good way to show how LEDs blink and moreover it has an important application in everyday life, specially for those zones where people's concurrency is low.<br />
<br />
This traffic light remains always in red until someone pushes a button. Then some actions are done in order to indicate that it is going to change its state (to green) soon. After that, the green light is on for a while and the same actions than before (later specified clearly) are done to indicate that it is going to change to red again. To summarize, it is not an automatic traffic light: in order to pass through the street, we have to ask for permission first.<br />
<br />
It works as follows:</div>
<div style="text-align: justify;">
<ol>
<li>The traffic lights remain in red if the button is not pushed.</li>
<li>When the button is pushed:</li>
<ol>
<li>Keep the red light on for 2 more seconds (for security reasons before crossing the street).</li>
<li>Turn on/off alternatively the red and the yellow lights to indicate that the colour is going to change soon. This process is repeated five times and each light remains 200 ms on and 200 ms off for each iteration.</li>
<li>Turn off the red and the yellow lights; turn on the green light for 3 seconds.</li>
<li>Turn on/off alternatively the green and the yellow lights to indicate that the colour is going to change soon. This process in the same way as in step 2.</li>
<li>Turn off the green and the yellow lights; turn on the red light indefinitely (return to the first step).</li>
</ol>
</ol>
<div>
<h2>
Circuit schema</h2>
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi32LDfJHKc9YfgHNpJWm-s4CFQY3SLK6IzekbOmi4rhdNDhK-7vO0Lx7UTaD2Ojj87n4ky7tu3D1afnq4fdfxnf8SduhUe0uAiCe5LP3CXou14VpHLkJwjmTyVWhgbtjCtim_1mAn432k/s1600/diagram_traffic_light.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi32LDfJHKc9YfgHNpJWm-s4CFQY3SLK6IzekbOmi4rhdNDhK-7vO0Lx7UTaD2Ojj87n4ky7tu3D1afnq4fdfxnf8SduhUe0uAiCe5LP3CXou14VpHLkJwjmTyVWhgbtjCtim_1mAn432k/s1600/diagram_traffic_light.png" height="265" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Image 2: Traffic lights circuit schema</td></tr>
</tbody></table>
<h2>
Circuit photograph</h2>
<div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLlUobG8xjURRvPC2HSu6a2yiI0ZK-JoOsnwSDBz9YRWrVXzikLUqJpeeNxYBqG68-HqhPmZcwrD-lSqBzaEr2SO4ydlJ0G7rE78D2f8n58wPdZ7UY2F49ZQK-bfiYjdKPiXtEFDky3A8/s1600/circuit_traffic_lights.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLlUobG8xjURRvPC2HSu6a2yiI0ZK-JoOsnwSDBz9YRWrVXzikLUqJpeeNxYBqG68-HqhPmZcwrD-lSqBzaEr2SO4ydlJ0G7rE78D2f8n58wPdZ7UY2F49ZQK-bfiYjdKPiXtEFDky3A8/s1600/circuit_traffic_lights.jpg" height="300" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Image 3: Traffic lights photograph</td></tr>
</tbody></table>
<h2>
</h2>
<h2 style="text-align: left;">
Video</h2>
<h2 style="text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube-nocookie.com/embed/-cX-DhkFGqM?rel=0" width="560"></iframe> </h2>
<h2>
</h2>
<h2>
Code</h2>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f0f0; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">/**</span>
<span style="color: #60a0b0; font-style: italic;"> * Traffic lights</span>
<span style="color: #60a0b0; font-style: italic;"> * Represents the traffic lights system we find in cities.</span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> * It is a variation in</span> <span style="color: #60a0b0; font-style: italic;">which the permission for a green </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;"> * light is asked through a button. </span>
<span style="color: #60a0b0; font-style: italic;"> */</span>
<span style="color: #60a0b0; font-style: italic;">// Button and traffic lights ports</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> BUTTON <span style="color: #666666;">=</span> <span style="color: #40a070;">2</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> ledR <span style="color: #666666;">=</span> <span style="color: #40a070;">13</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> ledY <span style="color: #666666;">=</span> <span style="color: #40a070;">12</span>;
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> ledG <span style="color: #666666;">=</span> <span style="color: #40a070;">8</span>;
<span style="color: #60a0b0; font-style: italic;">// Time waited before starting to change from red to turn </span></pre>
<pre style="line-height: 125%; margin: 0;"><span style="color: #60a0b0; font-style: italic;">// on alternatively red and yellow</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> waitForPass <span style="color: #666666;">=</span> <span style="color: #40a070;">5000</span>;
<span style="color: #60a0b0; font-style: italic;">// Time we keep the green light on</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> timeGreen <span style="color: #666666;">=</span> <span style="color: #40a070;">10000</span>;
<span style="color: #60a0b0; font-style: italic;">// Time we keep a light on when alternates with another one</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> alternTurnTime <span style="color: #666666;">=</span> <span style="color: #40a070;">200</span>;
<span style="color: #60a0b0; font-style: italic;">// Number of times that two lights are alternatively turned on/off</span>
<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> Nblinks <span style="color: #666666;">=</span> <span style="color: #40a070;">14</span>;
<span style="color: #60a0b0; font-style: italic;">// Indicates whether the button is pushed (1) or not (0)</span>
<span style="color: #902000;">int</span> val <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>;
<span style="color: #60a0b0; font-style: italic;">// the setup routine runs once when you press reset:</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">setup</span>()
{
<span style="color: #60a0b0; font-style: italic;">// initialize the digital pin as an output.</span>
pinMode(ledR, OUTPUT);
pinMode(ledY, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(BUTTON, INPUT);
Serial.begin(<span style="color: #40a070;">9600</span>);
}
<span style="color: #60a0b0; font-style: italic;">// the loop routine runs over and over again forever:</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">loop</span>()
{
<span style="color: #60a0b0; font-style: italic;">// Read the value of the button (to see if it is pushed or not)</span>
val <span style="color: #666666;">=</span> digitalRead(BUTTON);
delay(<span style="color: #40a070;">10</span>);
<span style="color: #60a0b0; font-style: italic;">// Print the value for debugging reasons</span>
Serial.println(val);
<span style="color: #60a0b0; font-style: italic;">// Turn the red light on</span>
digitalWrite(ledR, HIGH);
<span style="color: #60a0b0; font-style: italic;">// If the red button is pushed, make the transition to green.</span>
<span style="color: #60a0b0; font-style: italic;">// Then return to red again.</span>
<span style="color: #007020; font-weight: bold;">if</span> (val <span style="color: #666666;">==</span> HIGH)
{
<span style="color: #60a0b0; font-style: italic;">// Keep the red light on for 'waitForPass' time</span>
<span style="color: #60a0b0; font-style: italic;">// Then turn this light off</span>
delay(waitForPass);
digitalWrite(ledR, LOW);
<span style="color: #60a0b0; font-style: italic;">// Turn on alternatively both yellow and red lights</span>
turnLightsAlternatively(ledY, ledR);
<span style="color: #60a0b0; font-style: italic;">// Turn on the green light for a specified amount of time</span>
digitalWrite(ledG, HIGH);
delay(timeGreen);
digitalWrite(ledG, LOW);
<span style="color: #60a0b0; font-style: italic;">// Turn on alternatively both yellow and green lights</span>
<span style="color: #60a0b0; font-style: italic;">// When finished, note that red light will be turned on</span>
<span style="color: #60a0b0; font-style: italic;">// automatically when loop function restarts.</span>
turnLightsAlternatively(ledY, ledG);
}
}
<span style="color: #60a0b0; font-style: italic;">// Function to turn two lights alternatively</span>
<span style="color: #902000;">void</span> <span style="color: #06287e;">turnLightsAlternatively</span>(<span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> light1, <span style="color: #007020; font-weight: bold;">const</span> <span style="color: #902000;">int</span> light2)
{
<span style="color: #007020; font-weight: bold;">for</span> (<span style="color: #902000;">int</span> i <span style="color: #666666;">=</span> <span style="color: #40a070;">0</span>; i <span style="color: #666666;"><</span> Nblinks; <span style="color: #666666;">++</span>i)
{
digitalWrite(light1, HIGH);
delay(alternTurnTime);
digitalWrite(light1, LOW);
digitalWrite(light2, HIGH);
delay(alternTurnTime);
digitalWrite(light2, LOW);
}
}
</pre>
</td></tr>
</tbody></table>
</div>
<br />
Download the code <a href="https://www.dropbox.com/s/9ddaveqio3v1vxq/TrafficLightsApp.zip">here</a>.<br />
<h2>
References</h2>
[1] Picture of the functionality of the button, retrieved on March, 3th from:<br />
<a href="http://www.varesano.net/blog/fabio">http://www.varesano.net/blog/fabio</a></div>
</div>
Daniel Fureloshttp://www.blogger.com/profile/18259717346490561724noreply@blogger.com0