Hardware Reference
In-Depth Information
8
The blink() method is the same as
it was in the earlier XBee example.
void blink(int thisPin, int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(thisPin, HIGH);
delay(200);
digitalWrite(thisPin, LOW);
delay(200);
}
}
The buttonRead() method will look
familiar, too,—it's the same one from
the pong clients in Chapter 5.
8
// this method reads the button to see if it's just changed
// from low to high, and debounces the button in case of
// electrical noise:
boolean buttonRead(int thisButton) {
boolean result = false;
// temporary state of the button:
int currentState = digitalRead(thisButton);
// final state of the button:
int buttonState = lastButtonState;
// get the current time to time the debounce interval:
long lastDebounceTime = millis();
while ((millis() - lastDebounceTime) < debounceInterval) {
// read the state of the switch into a local variable:
currentState = digitalRead(thisButton);
// If the pushbutton changed due to noise:
if (currentState != buttonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = currentState;
}
// if the button's changed and it's high:
if(buttonState != lastButtonState && buttonState == HIGH) {
result = true;
}
// save the current state for next time:
lastButtonState = buttonState;
return result;
}
 
Search WWH ::




Custom Search