// PhysDuino HT1632 stopwatch for short intervals (to 99.9 sec) // Based on http://playground.arduino.cc/Code/Stopwatch (Paul Badger 2008) // and https://github.com/gauravmm/HT1632-for-Arduino (display render part) #include #include #define ledPin 13 // LED connected to digital pin 13 #define buttonPin 4 // button on pin 4 int value = LOW; // previous value of the LED int buttonState; // variable to store button state int lastButtonState; // variable to store last button state int blinking; // condition for blinking - timer is timing long interval = 50; // blink interval - change to suit long previousMillis = 0; // variable to store last time LED was updated long startTime ; // start time for stop watch long elapsedTime ; // elapsed time for stop watch int fractional; // variable used to store fractional part of time int wd; String s; int x = 0; void setup () { Serial.begin(9600); HT1632.begin(8, 9, 10, 11); pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway digitalWrite(buttonPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground. HT1632.drawTarget(BUFFER_BOARD(1)); HT1632.drawText(" 0.0", 1, 0, FONT_16X8, FONT_16X8_WIDTH, FONT_16X8_HEIGHT, FONT_16X8_STEP_GLYPH); HT1632.render(); // Board 1's contents is updated. } void loop () { // check for button press buttonState = digitalRead(buttonPin); // read the button state and store if (buttonState == LOW && lastButtonState == HIGH && blinking == false){ // check for a high to low transition // if true then found a new button press while clock is not running - start the clock startTime = millis(); // store the start time blinking = true; // turn on blinking while timing delay(5); // short delay to debounce switch lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time } else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){ // check for a high to low transition // if true then found a new button press while clock is running - stop the clock and report elapsedTime = millis() - startTime; // store elapsed time blinking = false; // turn off blinking, all done timing lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time // routine to report elapsed time Serial.print( (int)(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print Serial.print("."); // print decimal point // use modulo operator to get fractional part of time fractional = (int)(elapsedTime % 1000L); // pad in leading zeros - wouldn't it be nice if // Arduino language had a flag for this? :) if (fractional == 0) Serial.print("000"); // add three zero's else if (fractional < 10) // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros Serial.print("00"); // add two zeros else if (fractional < 100) Serial.print("0"); // add one zero Serial.println(fractional); // print fractional part of time to the serial port } else{ lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time } // blink routine - blink the LED while timing // check to see if it's time to blink the LED; that is, the difference // between the current time and last time we blinked the LED is larger than // the interval at which we want to blink the LED. if ( (millis() - previousMillis > interval) ) { if (blinking == true){ float now = millis()-startTime; String s = String(trunc(now/1000),0); String ms = String(trunc((now/1000-trunc(now/1000))*10),1); // for 16x8 glyphs use *10, for smaller *100 in formula if (now < 10000) { // If the time is less than 10 sec (10.000 msec) // Select board 1 as the target of subsequent drawing/rendering operations. HT1632.drawTarget(BUFFER_BOARD(1)); String cas1 = s+"."+ms[0]; Serial.println(cas1); char m[32]; cas1.toCharArray(m,32); HT1632.drawText(m, 1, 0, FONT_16X8, FONT_16X8_WIDTH, FONT_16X8_HEIGHT, FONT_16X8_STEP_GLYPH); HT1632.render(); // Board 1's contents is updated. } if (now > 10000) { // If the time is greater than 10 sec (10.000 msec) HT1632.drawTarget(BUFFER_BOARD(1)); String cas1 = s+"."+ms[0]; char m[32]; cas1.toCharArray(m,32); HT1632.drawText(m, 1, 0, FONT_16X8, FONT_16X8_WIDTH, FONT_16X8_HEIGHT, FONT_16X8_STEP_GLYPH); HT1632.render(); // Board 1's contents is updated. } if (now > 99900) { // If the time is greater than 99.95 sec (95.95 msec) blinking = false; // stop the clock! } previousMillis = millis(); // remember the last time we blinked the LED // if the LED is off turn it on and vice-versa. if (value == LOW) value = HIGH; else value = LOW; digitalWrite(ledPin, value); } else{ digitalWrite(ledPin, LOW); // turn off LED when not blinking } } }