/* Program 5-4: Timestamp the press of SW using micros() */ const int SW = 7; /* SW */ void setup() { pinMode(SW, INPUT); /* configure input pin for switch */ Serial.begin(9600); /* initialize Serial and set Baud rate */ } void loop() { static unsigned int lastTime = 0; /* last timestamp */ unsigned int thisTime; /* current timestamp */ unsigned int elapsedTime; /* time elapsed since last switch press */ static int before = HIGH; /* stored switch input state */ int now = digitalRead(SW); /* current switch input state */ if (before == HIGH && now == LOW) { /* switch pressed */ thisTime = micros(); /* take the timestamp */ elapsedTime = thisTime - lastTime; /* calculate time elapsed */ Serial.print(thisTime); /* display current timestamp */ Serial.print(", "); Serial.println(elapsedTime); /* and time elapsed */ lastTime = thisTime; /* update last timestamp */ } before = now; /* update stored switch state */ }