128 5. ANALOG TO DIGITAL CONVERSION (ADC)
}
else
{
PORTD = 0xAA;
}
}
//*************************************************************************
//*************************************************************************
5.8.3 ADC RAIN GAGE USING THE ARDUINO DEVELOPMENT
ENVIRONMENT—REVISITED
If you carefully compare the two implementations of the rain gage indicator provided in the two pre-
vious examples, you will note that some activities are easier to perform in the Arduino Development
Environment while others are easier to accomplish in C. Is it possible to mix the two techniques for
a more efficient sketch? The answer is “yes!”
In this example, we revise the earlier Arduino Development Environment sketch using por-
tions of the original sketch with the ATmega328 control algorithm code to significantly shorten
the program. In particular, we will address PORTD directly in the sketch to shorten up the code
when the LEDs are illuminated. Recall in Chapter 1, we provided the open source schematic of
the Arduino UNO R3 processing board (Figure 1.8). Careful study of the schematic reveals that
DIGITAL pins 7 to 0 are connected to PORTD of the Arduino UNO R3 processing board. This
allows direct configuration of PORTD using C language constructs.
//*************************************************************************
#define trim_pot 0 //analog input pin
int trim_pot_reading; //declare variable for trim pot
void setup()
{
DDRD = 0xFF; //Set PORTD (DIGITAL pins 7 to 0)
} //as output
void loop()
{
//read analog output from trim pot
trim_pot_reading = analogRead(trim_pot);
if(trim_pot_reading < 128)
{

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.