Hardware Reference
In-Depth Information
The setup() method sets the
states of the pins, initializes serial,
and blinks an LED, as usual.
8
void setup() {
// configure serial communications:
Serial.begin(115200);
// configure output pins:
pinMode (analogLed, OUTPUT);
// blink the TX LED indicating that
// the main program's about to start:
blink(analogLed, 3);
}
8
The main loop listens for incoming
serial data and checks to see whether
the button's been pushed. If so, it
connects or disconnects as appropri-
ate. If the radios are connected, it
reads the analog input and sends it out
if there's been a significant change.
void loop() {
// read incoming serial and parse it:
handleSerial();
// check to see if the pushbutton's pressed:
boolean buttonPushed = buttonRead(connectButton);
// if the button's just pressed:
if (buttonPushed) {
// if the client's connected, disconnect:
if (connected) {
BTDisconnect();
} // if the client's disconnected, try to connect:
else {
BTConnect();
}
}
// if connected, take sensor readings:
if (connected) {
// note the current time in milliseconds:
long currentTime = millis();
// if enough time has passed since the last reading:
if (currentTime - lastReadingTime > debounceInterval) {
// read the analog sensor, divide by 4 to get a 0-255 range:
int sensorValue = analogRead(A0)/4;
// if there's a significant difference between the
// current sensor reading and the last, send it out:
if (abs(sensorValue - lastSensorReading) > threshold) {
Serial.println(sensorValue, DEC);
}
// update the last reading time
// and last sensor reading:
lastReadingTime = currentTime;
lastSensorReading = sensorValue;
}
}
}
 
Search WWH ::




Custom Search