Hardware Reference
In-Depth Information
8
Now, add a new method,
drawGraph() , to the end of the
program.
void drawGraph(int thisValue) {
// draw the line:
stroke(#4F9FE1);
// map the given value to the height of the window:
float graphValue = map(thisValue, 0, 1023, 0, height);
// determine the line height for the graph:
float graphLineHeight = height - (graphValue);
// draw the line:
line(hPos, height, hPos, graphLineHeight);
// at the edge of the screen, go back to the beginning:
if (hPos >= width) {
hPos = 0;
//wipe the screen:
background(0);
}
else {
// increment the horizontal position to draw the next line:
hPos++;
}
}
Call this from the parseData() method,
replacing the println() statement that
prints out the average, like so:
// draw a line on the graph:
drawGraph(average);
Now when you run the program, it
should draw a graph of the sensor
readings, updating every time it gets
a new datagram.
8
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.
8
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"
};
Call this method from a few different
places in the program; first, at the end
of the setup() method:
// format the date string:
String date = day() + " " + months[month() -1] + " " + year() ;
// show the readings text:
drawReadings(0,0);
// format the time string
// all digits are number-formatted as two digits:
String time = nf(hour(), 2) + ":" + nf(minute(), 2) + ":" + nf(second(),
2);
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);
// calculate the voltage from the reading:
float voltage = thisReading * 3.3 / 1024;
// choose a position for the text:
int xPos = 20;
int yPos = 20;
ยป
 
Search WWH ::




Custom Search