Store › Forums › Digit Shield › Bugs/Problems › Stopwatch
- This topic has 5 replies, 2 voices, and was last updated 8 years, 8 months ago by
Michael.
-
AuthorPosts
-
December 20, 2015 at 4:00 am #795
jwax
MemberHope 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);
}
}December 20, 2015 at 2:04 pm #2371Michael
KeymasterHow 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) {
...
December 20, 2015 at 2:30 pm #2355jwax
MemberPin 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!
December 20, 2015 at 2:42 pm #2378Michael
KeymasterYou 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)
December 20, 2015 at 11:15 pm #2379jwax
MemberThe 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!
JohnDecember 21, 2015 at 9:04 pm #2380Michael
KeymasterI guess I’m not sure what the problem could be, so maybe you could try inverting the logic and see if that changes anything.
-
AuthorPosts
- You must be logged in to reply to this topic.