Making Things Talk by Tom Igoe

Making Things Talk by Tom Igoe

Author:Tom Igoe
Language: eng
Format: mobi, epub
Tags: COMPUTERS / Hardware / General
Publisher: Make
Published: 2011-09-08T04:00:00+00:00


Next, add some code to add a time stamp. First, add a global variable to set the text line height.

int lineHeight = 14; // a variable to set the line height

Add a method to the end of the program, drawReadings(). This will display the date, time, voltage reading, and received signal strength.

Call this method from a few different places in the program; first, at the end of the setup() method:

// show the readings text: drawReadings(0,0);

Next, to draw the latest readings, call it at the end of the parseData() method:

// draw a line on the graph, // and the readings: drawGraph(average); drawReadings(average, signalStrength);

void drawReadings(int thisReading, int thisSignalStrength) { // set up an array to get the names of the months // from their numeric values: String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // format the date string: String date = day() + " " + months[month() −1] + " " + year() ; // format the time string // all digits are number-formatted as two digits: String time = nf(hour(), 2) + ":" + nf(minute(), 2) + ":" + nf(second(), 2); // calculate the voltage from the reading: float voltage = thisReading * 3.3 / 1024; // choose a position for the text: int xPos = 20; int yPos = 20; // erase the previous readings: noStroke(); fill(0); rect(xPos,yPos, 180, 80); // change the fill color for the text: fill(#4F9FE1); // print the readings: text(date, xPos, yPos + lineHeight); text(time, xPos, yPos + (2 * lineHeight)); text("Voltage: " + voltage + "V", xPos, yPos + (3 * lineHeight)); text("Signal Strength: -" + thisSignalStrength + " dBm", xPos, yPos + (4 * lineHeight)); }

That’s the whole program. When it’s running, it should look like Figure 7-16.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.