Stopwatch

Store Forums Digit Shield Bugs/Problems Stopwatch

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #795
    jwax
    Member

    Hope to build a stopwatch which converts the time between two input pulses (5 volt, 2 msec pulses) to a velocity with some math in the program. The two pulses will be between 20 msecs and 100 msecs apart.
    The problem with my prototype is that it does not respond to the very short pulses (high speed).

    Also, the displays sometimes starts counting up without an input pulse. Could this be noise?

    Looking for either a fix for this code, or perhaps a totally new approach.
    First project, and new to coding, so I appreciate any help!
    John

    #include 
    #define BUTTON 8

    unsigned long start, stop;
    int speed;

    void setup() {

    pinMode(8, INPUT);
    digitalWrite(8, LOW);

    DigitShield.begin();
    DigitShield.setBlank(true);


    }

    void loop() {
    // wait until button press
    if (digitalRead(BUTTON) == HIGH){

    delay(200);
    start = millis();


    DigitShield.setBlank(false);


    while (true)
    {
    DigitShield.setValue((int)(millis()-start));
    if (digitalRead(BUTTON) == HIGH)
    {
    stop = millis();
    break;
    }
    }
    speed = 32.81*1000/(stop-start);
    DigitShield.setValue(speed);




    //turn off display after 4 seconds
    delay(4000);
    DigitShield.setBlank(true);

    }
    }
    #2371
    Michael
    Keymaster

    How do you have your input pin wired? It needs to be tied to ground to keep it low until you connect a 5V signal to it. If this pin is “floating”, then it will randomly read high or low.

    Usually when using an input pin with a button, you set the internal pullup resistor on the pin so that it reads HIGH at all times. Then your button connects the pin to ground. A button press then reads LOW.


    // set internal pullup resistor. Pin now reads HIGH normally.
    pinMode(8, INPUT_PULLUP);

    void loop() {
    // wait until button press
    if (digitalRead(BUTTON) == LOW) {
    ...
    #2355
    jwax
    Member

    Pin 8, the input pin, has a 56 Kohm resistor to ground. Perhaps this should be smaller?

    The trigger circuitry was configured to produce the +5 volt pulse. If need be, I could invert it, and rewrite the code as you suggest.

    Thanks Michael!

    #2378
    Michael
    Keymaster

    You might try a 10K resistor and see if that changes things. Or you can invert the logic as stated.

    Are you using a button to provide your input pulses? I guess I assumed you were. The problem with buttons is that they “bounce”. When you press it, the connection bounces a few times, and so some applications need to account for this with compensating logic. Bouncing is going to make it hard to actually measure short pulses.

    When I write logic that uses buttons as input, I usually add a short delay after reading the button to allow it to stabilize.

    delay(20)
    #2379
    jwax
    Member

    The pulses are coming from a LM555 timer working in Monostable mode to generate the uniform pulses. They look very clean on an oscilloscope.
    Would the Arduino “prefer” the negative pulse, and give better stability?
    I’ll be trying that next, along with making the trigger pulses even shorter, maybe < 1 msec.
    Appreciate your help, Michael!
    John

    #2380
    Michael
    Keymaster

    I guess I’m not sure what the problem could be, so maybe you could try inverting the logic and see if that changes anything.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.