Post comments on this article and find any associated files and/or downloads at
www.nutsvolts.com/index.php?/magazine/article/january2015_Smiley.
calibration variable values for the potentiometer angle. We
recorded the ADC value when the pot was pointed at
zero, and recorded it again when pointed to 180. Based
on those readings, we would then map the ADC values to
angles between 0 and 180 degrees, and then store these
values in volatile memory ('evaporates' when power is
removed). Each time we re-powered the Arduino, we had
to calibrate it again.
Conversions between these data types then become
some relatively simple computing operations.
To keep things even simpler, we will use a special
library (storeEEPROM) that automatically stores and
retrieves 16-bit ints and 32-bit long ints in EEPROM. You
can find this library in the a101_ch12_supplemental.zip
file at the article link.
We somewhat alleviated that problem in Chapter 9
when we learned to use the Arduino with a battery that
continued to provide power to the volatile memory after
the USB connection was removed, but batteries get
unplugged or eventually run out of juice. So, how do we
get the Arduino to remember things when the power goes
off? We use the EEPROM (Electrically Erasable
If you want to see how these algorithms work, you
can look at the original source code for each function.
The functions in that library are:
void storeIntEEPROM(int myAddress, int myInt)
int retrieveIntEEPROM (int myAddress)
void storeLongEEPROM (int myAddress,
long int myInt)
long int retrieveLongEEPROM (int myAddress)
Programmable Read Only Memory) which is a form of
non-volatile memory (doesn't 'evaporate' when the power
is removed) that remembers the data until it is
reprogrammed with new data. The Arduino UNO has
1,024 bytes of EEPROM. This memory is incredibly simple
to use, plus we are provided with a library of functions
that let us write or read bytes to addresses.
By now, you should be able to read each function
description above and see what it does and how it is
used. For instance, the first function stores a 16-bit int,
myInt at the EEPROM location myAddress. The second
function will retrieve a 16-bit int from the myAddress
location. We will install and test this library in Lab 2.
Keeping a Timestamp for Data Sensing
Charting Arduino Data on a PC
Well that's great, if you want to store eight-bit byte
variables (numbers between 0 and 255). Unfortunately,
there is a slight complication if you want to store other
types of data that we've been using. The values from the
ADC are 10-bit and may be from 0 to 1023. However, we
store these values in 16-bit integers. As we saw in Chapter
9, Unix timestamps are 32-bit long integer values. We
need an algorithm for converting these data types into
bytes for storage and then retrieving those bytes and
restoring the original data type. Since the ADC values are
stored in 16-bit ints, we can convert them into two eight-bit bytes for storage. For the time value, we can convert
and store 32-bit long integers as four eight-bit bytes.
Now that we know how to store raw data in an
EEPROM, let's look at how we may show that data. We
might write a program that reads four sensors once per
second, and stores a data byte for each sensor in the
EEPROM in blocks of four bytes each second. When we
have finished reading that data after say, a minute, we may
then send that data to a PC for interpretation. This sort of
data is shown here with raw comma separated data:
0,152,252,89,2,165,249,77,5,177,244,66,10,188,239
,55,15,199,232,45,22,209,225,36,29,218,216,27,38,
227,206,20,48,234,196,13,58,241,185,8,69,246,173,
4,81,250,161,1,93,253,149,0,105,254,136,0,118,254
,123,1,131,253,111,3,143,251,98,6,156,248,86,11,1
68,243,74,17,180,237,63,24,191,230,52,32,202,222,
42,41,212,213,33,51,221,203,25,61,229,193,18,73,2
36,181,12,84,242,170,7,97,247,158,3,109,251,145,1
,122,253,132,0,134,254,120,0,147,254,107,1,160,25
3,94,4,172,250,82,8,183,246,71,13,195,241,59,19,2
05,235,49,26,215,228,39,34,224,220,31,44,231,210,
23,54,238,200,16,64,244,190,10,76,248,178,6,88,25
2,166,2,100,254,154,0,113,254,141,0,125,254,129,0
,138,252,116,2,151,249,103,5,163,245,91,9,175,240
,79,14,187,233,67,21,198,225,56,28,208,217,46,37,
217,207,37,47,226,197,28,57,233,186,21,68,240,175
,14,79,245,163,9,91,249,150,5,104,252,138,2,
When we list that data, we see that it doesn't make
much sense to us. What is actually going on with these
four sensors? We can put this data in a spreadsheet and
have a chart drawn for the data showing the output of
each of the four sensors as shown in Figure 1. Now, the
data makes sense to us; we are seeing data that represents
four sine waves.
■ FIGURE 1: Raw data shown in graph.
We will learn how to do this in Lab 6 using the free
Google Chart browser application.
54 January 2015