Post comments on this article and find any associated files and/or downloads at
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:
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