sentryGun53 / Project-Sentry-Gun

This is an open-scource code project from Rudolph Labs. See the website for more information. Initiated by Bob Rudolph (sentryGun53) projectSentryGun@rudolphLabs.com, sentryGun53@gmail.com
http://psg.rudolphlabs.com/
209 stars 107 forks source link

Timing issue in the Arduino code #6

Open ricks03 opened 4 years ago

ricks03 commented 4 years ago

I'm trying to make some modifications to the code to let me limit how often the PSG can fire, and then some modifications for very specifically how long the (electronic) trigger will hold, and I believe I've found a code/timing issue.

Arduino uses millis() for timing, to determine how long between events.

However, the PSG Arduino Fire function uses a raw counter (fireTimer++) to calculate how long the trigger has been held down. this same counter is used to determine how often to increment the ammo.

Took me a while to figure out, because triggerTravelMillis is named like it's associated with usage of the millis() function. But it's not. Instead it's compared to values of the fireTimer counter.

This will make the code VERY sensitive to the hardware it's running on, as the code is using number of loops through the code, not time, to determine what to do.

It also means if you're trying to get the timing exactly right for how long the trigger is held down (as I need to, because the electronic system I'm using needs ~ 150 ms of the trigger held down to fire correctly) it's a lot harder.

Here's the code I'm referencing. fireTimer should really be using millis() time. Instead, it's just reset to 0 and starts over: if(selector == 1) { fireTimer++; if(fireTimer >=0 && fireTimer <= triggerTravelMillis) { digitalWrite(electricTriggerPin, HIGH); trigger.write(triggerServo_SqueezedPosition); digitalWrite(firingIndicatorLEDPin, HIGH); } if(fireTimer > triggerTravelMillis && fireTimer < 1.5*triggerTravelMillis) { digitalWrite(electricTriggerPin, LOW); trigger.write(triggerServo_HomePosition); digitalWrite(firingIndicatorLEDPin, LOW); } if(fireTimer >= 1.5*triggerTravelMillis) { fireTimer = 0; if(useAmmoCounter) { shotCounter++; // increment the shot counter } }