Problem with counting how long I hold a button – Defusable Airsoft Bomb Prop

I’ve made a defusable bomb prop (with my Arduino UNO) for playing airsoft, but I’ve come across something I think is a bug. Sometimes (not so often) when I’m holding down the red button that is supposed to arm the bomb and begin the timer, the Arduino just counts wrong for how long I’ve held the button (see the attached picture “Problem counting.png” for an overview over the serial monitor). After holding the red button for 5 seconds, it’s supposed to arm the bomb. However, when this bug appears, it arms the bomb a completely random time after I’ve held the button for 5 seconds. This is very bad when it comes to what this is going to be used for; a competetive game that is all about how much time you have. Sometimes it takes 5 seconds to defuse/disarm the bomb, but sometimes it takes 10 seconds, which is very unfair for the different players/teams.

I have no idea where this problem is located, so I’ll just attach all of my code and a picture on the circuit (“airsoftBomb3.4.ino” and “Circuit.png”).

Contents:
Arduino Uno, Real Time Clock (DS3231), red push button, blue push button, buzzer (I think it is electromechanical, since it can’t create any tones), red LED (I call it “timerLED” in the code), RGB LED (I call it “statusLED” in the code).

I would also appreciate whatever feedback you can come up with and how I can make my project better.

Remember that I am kind of new when it comes to programming and using Arduino, so you might need to be more clear than usual when telling me something.

Thanks in advance,
Arduotto

Problem counting.png

airsoftBomb3.4.ino (9.4 KB)

Hi,

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Ops circuit;


Instead of using Vin to supply the components on the protoboard, use 5V.
Do you have a DMM?

Tom... :slight_smile:

TomGeorge:
Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Hi Tom, and thank you for taking your time!

I read that posts before posting, but couldn't make it happen because I had way too much text/code.

TomGeorge:
Instead of using Vin to supply the components on the protoboard, use 5V.
Do you have a DMM?

Sure thing, I'll change it to 5V! But I'm just curious, what's the reason to do this?

Also, I think I have a DMM laying around somewhere, so I'll seek for it soon!

Hi,

Vin is connected to the DC socket on the UNO, it only has any power on it when you use the DC socket, this has to be 7V to 12V as it feeds the onboard 5V and 3v3 regulators.

As you are using the USB to power the project, Vin will have some stray voltage lower than 5V, so will not be supplying 5V to the protoboard.
Basically Vin pin is a Power Input pin.

Tom... :slight_smile:

TomGeorge:
As you are using the USB to power the project, Vin will have some stray voltage lower than 5V, so will not be supplying 5V to the protoboard.

Oh, sorry for the confusion with the picture. I'm using the DC socket and a 9V battery to power my project. So if I'm understanding this right, I can still use Vin?

lost my previous post

i don't see a problem. looks like your code is working as expected. don't see an issue with voltages

your figure may have leaned resulting in a momentary break in the switch contact

suggest you detect a button press and capture the time, msec. if msec is set, and millis()-msec > holdtime , perform some action, reset msec.

if the button is release, reset msec.

you may want to add a short delay after reading the butting and perhaps read again to verify any change

gcjr:
your figure may have leaned resulting in a momentary break in the switch contact

Thank you for tuning in @gcjr! This was a good hypothesis!

gcjr:
you may want to add a short delay after reading the butting and perhaps read again to verify any change

How would I do this in the best way possible?

arduotto:
Oh, sorry for the confusion with the picture. I'm using the DC socket and a 9V battery to power my project. So if I'm understanding this right, I can still use Vin?

No No. No!!! The Vin pin is connected almost directly to the 9V of your battery. There is actually a forward biased diode between the two so it will measure something between 8.4V and 8.6V which is too high to apply to arduino inputs or the RTC module. As Tom said use the +5V pin not the Vin pin.

Ian

IanCrowe:
No No. No!!! The Vin pin is connected almost directly to the 9V of your battery. There is actually a forward biased diode between the two so it will measure something between 8.4V and 8.6V which is too high to apply to arduino inputs or the RTC module. As Tom said use the +5V pin not the Vin pin.

The more you know! :sweat_smile: :astonished: Once again, thank all of you for taking your time to give a newbie all of this great feedback!

arduotto:
Oh, sorry for the confusion with the picture. I'm using the DC socket and a 9V battery to power my project. So if I'm understanding this right, I can still use Vin?

Good reason for a hand drawn picture of a project with all the components and labels rather than stick with the unlabeled and inadequate fritzy picture.
Has moving to 5V pin made any difference?
Tom.... :slight_smile:

TomGeorge:
Has moving to 5V pin made any difference?

Not what I can tell... :neutral_face: It still sometimes have a problem to detect the amount of seconds the buttons are held.