EasyManuals Logo

Arduino uno User Manual

Arduino uno
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #114 background imageLoading...
Page #114 background image
-
function updateUI(temperature, distance) {
-
document.getElementById("temperature").innerText = temperature;
-
for (var i = 1; i < 9; i++) {
30
var index = "d" + i;
-
if (distance <= lights[index][0])
-
document.getElementById(index).style.color = lights[index][1];
-
else
-
document.getElementById(index).style.color = "white";
35
}
-
}
-
-
arduino.connect();
-
To read the sensor data from the Arduino, we use the
SerialDevice
class weve
defined in Writing a SerialDevice Class, on page 274. We create a new instance
named
arduino
in the first line. Make sure youre using the right serial port
path.
Then we define an
onConnect
handler that prints a message to the browsers
JavaScript console as soon as the application has connected to an Arduino.
In principle, you dont need the
onConnect
handler. In this case, its mostly
useful for debugging purposes.
Things get more interesting in the
onReadLine
handler. In line 9, we split the
data weve received from the Arduino. We make sure that weve actually
received two values. In this case we turn both values into numbers using
parseInt
, and we also divide them by 100 because the Arduino sends values
that have been multiplied by 100 before. In line 11, we use a popular Java-
Script trick to round the temperature value to one decimal digit. After weve
turned both the distance and the temperature into proper numbers, we pass
them to
updateUI
.
updateUI
sets the new temperature value first in line 29. To do this, it looks up
the HTML element having the ID
temperature
using the
getElementById
function.
Then it sets the elements
innerText
property to the current temperature.
Updating the artificial LED display is a bit more complex, but not too difficult.
Weve defined a data structure named
lights
that maps the IDs of our displays
<span>
elements to arrays having two elements each. For example, it maps
the ID
d1
to an array containing the values 35.0 and orange. That means
that the color of the element having the ID
d1
will be set to orange when the
distance to the next object is less than or equal 35.0 centimeters.
Using the
lights
data structure, its easy to implement the LED display. In line
30, we start a loop iterating over all LEDs. We determine the current LEDs
Chapter 5. Sensing the World Around Us 96
report erratum discuss
www.it-ebooks.info

Table of Contents

Other manuals for Arduino uno

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino uno and is the answer not in the manual?

Arduino uno Specifications

General IconGeneral
Form factorArduino
CertificationRoHS, FC, CE
Processor model-
Processor frequency- MHz
Microcontroller modelATmega328
Microcontroller frequency16 MHz
DC input voltage7-12 V
Operating voltage5 V
DC current per I/O pin40 mA
Flash memory0.032 MB
Maximum internal memory- GB
SRAM (Static Random Access Memory)2 KB
EEPROM (Electrically Erasable Programmable Read-Only Memory)1 KB
Wi-FiNo
Number of analog I/O pins6
Number of digital I/O pins14
Weight and Dimensions IconWeight and Dimensions
Board dimensions53.4 x 68.6 mm

Related product manuals