Hardware Reference
In-Depth Information
Serial.print(f);
Serial.print("F.");
Processing needs to parse the Celsius and Fahrenheit temperature data. By
replacing the spaces and carriage returns with commas and periods, you can
easily look for these delimiting characters and use them to parse the data.
Next, you need to add the shift register code from the previous chapter, and
map the LED levels appropriately to the temperature range that you care about.
If you a need a refresher on the shift register code that you previously wrote,
take another look at Listing 7-3; much of the code from that program will be
reused here, with a few small tweaks. To begin, change the total number of
light variables from nine to eight. With this change, you always leave one LED
on as an indication that the system is working (the 0 value is eliminated from
the array). You need to accommodate for that change in the variable value map-
ping, and you need to map a range of temperatures to LED states. Check out the
complete code sample in Listing 8-2 to see how that is accomplished. I chose to
make my range from 24 ° C to 31 ° C (75 ° F to 88 ° F), but you can choose any range.
Listing 8-2: I 2 C Temperature Sensors Code with Shift Register LEDs and Serial
Communication—temp_unit.ino
//Reads temp from I2C temperature sensor
//Show it on the LED bar graph, and show it in Processing
//Include Wire I2C library
#include <Wire.h>
const int SER =8; //Serial Output to Shift Register
const int LATCH =9; //Shift Register Latch Pin
const int CLK =10; //Shift Register Clock Pin
int temp_address = 72;
//Possible LED settings
int vals[8] = {1,3,7,15,31,63,127,255};
void setup()
{
//Instantiate serial communication at 9600 bps
Serial.begin(9600);
//Create a Wire Object
Wire.begin();
//Set shift register pins as outputs
pinMode(SER, OUTPUT);
pinMode(LATCH, OUTPUT);
Search WWH ::




Custom Search