Hardware Reference
In-Depth Information
Note that the button and text
positions are all relative to the
screenWidth and screenHeight
variables. This is so that they stay in
the same relation to each other and
the screen if the device is rotated.
8
Continued from opposite page.
// try to connect to Bluetooth:
connectionState = connect();
readButton = new Button(screenWidth/2 - 100, 2*screenHeight/3,
200, 60, buttonColor, buttonHighlightColor, "Get Reading");
sendButton = new Button(screenWidth/2 - 100, 2*screenHeight/3 + 80,
200, 60, buttonColor, buttonHighlightColor, "Send Reading");
}
8
The draw() method starts by
drawing the text strings and buttons.
Then it checks to see whether the
sensor reading interval has passed, or
whether the updateNow variable has
been set (when you click the Read Now
button, this variable is set to true).
If either of these is true, the sketch
takes a reading via Bluetooth using
the getData() method, and adds it to a
string of readings taken since the last
time it updated the server.
void draw() {
// display data onscreen:
background(bgColor);
fill(textColor);
textAlign(LEFT);
text(connectionState, 10, screenHeight/4);
text(getTime(), 10, screenHeight/4 + 60);
text("latest reading (volts): " + thisReading, 10, screenHeight/4 + 90);
text("Server updated at:\n" + lastSendTime, 10, screenHeight/4 + 120);
// draw the buttons:
readButton.display();
sendButton.display();
if (sendNow) {
textAlign(LEFT);
text("sending to server, please wait...", 10, screenHeight/4 - 60);
}
Next, it checks whether the send
interval or sendNow has been set by a
click of the Send Now button. If either
of these is true, it sends the current
string of readings to the server using
the sendData() method.
// if the update interval has passed,
// or updateNow is true, update automatically:
if (abs(second() - lastRead) >= readInterval || updateNow) {
thisReading = getData();
// if you got a valid reading, add a timestamp:
if (thisReading != null) {
currentReadings += getTime() +"," + thisReading;
// take note of when you last updated:
lastRead = second();
// you've updated, no need to do it again until prompted:
updateNow = false;
}
}
// if the send interval has passed,
// or sendNow is true, update automatically:
if (abs(minute() - lastSend) >= sendInterval || sendNow ) {
sendData(currentReadings);
// get the time two ways:
ยป
 
Search WWH ::




Custom Search