Very simple code or component issue which I'm missing

Hey,

So I’m looking to make a laser gun game and I’ve been able to create some code below to make a laser/ LED blink once when a button is depressed and kept depressed. However, the issue I’ve got is that the code or components are only letting the laser/ LED blink after the button has been released rather than on the button being pressed. Any help on sorting this issue would be greatly appreciated!

In the layout attached, I’ve had to use a Nano rather than the Pro Mini I’m actually using so please ignore the pin designations.

Cheers

Dan

const int buttonPin = 5;
const int ledPin = 13;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonStateLast = 0; // variable for recording last button state

void setup() 
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop()
{

// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// Determine when button pressed . . .
if (buttonState == HIGH and buttonState != buttonStateLast) {

// turn LED on:
digitalWrite(ledPin, HIGH);
delay(100); // wait for a 10th second
digitalWrite(ledPin, LOW);

}

buttonStateLast = buttonState;

}

That Fritzing is unreadable. Important lines are covered up. Please provide a usable schematic.

On the breadboard image you've posted it appears the (PB) switch is shorting ground to +5V.

Issue 66409 for this question: Do you have a pulldown resistor (about 10k) connected from pin 5 to GND?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a picture of your project so we can see your component layout/

Thanks.. Tom... :slight_smile:

Hey,

Thanks for the responses! and I’ve attached a new schematic where I think I’ve removed the short to the Push button? Along with that, there’s no tracks under the Arduino now. I’ve also added a resistor to connect pin 5 and GND. When testing online, its still having the same issue as before though.

To schedule an action when a button has been depressed for a period of time, you have to check
the time since the button was pressed, and maintain a state-machine for the thing.

danjr1:
Hey,

Thanks for the responses! and I’ve attached a new schematic where I think I’ve removed the short to the Push button?

Maybe not. Try it with the jumper I’ve marked* removed.

  • 1st attempt, I hope it works.

arduino forum 1.PNG

dougp:
Maybe not. Try it with the jumper I've marked* removed.

  • 1st attempt, I hope it works.

Just tried it and unfortunately it didn't work at all. My understanding is that a switch needs a complete circuit which then spurs off into the arduino to provide in the input.

Weirdly, this all did work before I tried using the MOSFET component and I copied the Nano circuit in the link below.

https://translate.googleusercontent.com/translate_c?depth=1&hl=en&ie=UTF8&nv=1&prev=_t&rurl=translate.google.co.uk&sl=auto&sp=nmt4&tl=en&u=https://www.heise.de/ct/projekte/machmit/processing/wiki/AufbauArduino&usg=ALkJrhhwNA265I57apW5tHu8FoSXxIq_iw

Solved it! my silly wiring around the push switch was the issue. Copied the example in the link exactly in regards to the push button and its working perfectly.

Thank you all for your help! and I'm sure I'll be back with more questions.

Cheers

Dan

Hey,

Knew I’d be back pretty quick! So the circuit and code all works now but as I want to add a recoil solenoid into the mix, I’ve breadboarded a motor in as a substitute for the time being (see the attached image).

The solenoid would be 24v so in the simulation it requires a different power supply. However, when I follow the same wiring principles as the rest of the system and the pin on the arduino outputting to both gates on the MOSFETs, the motor won’t spin? I’ve tried diodes across the motor terminals, resistors in various places, etc, etc but no luck.

As always, any help would be greatly appreciated!

The ground rail on the top of your breadboard is not connected to the ground rail on the bottom.

That layout needs some serious work.

PaulS:
The ground rail on the top of your breadboard is not connected to the ground rail on the bottom.

That layout needs some serious work.

Thanks! all works now. I'm just using tinkercad to test the waters. The tangible installation in the old PS4 light gun I'm using will be far worse.

Hi all,

I've now got the laser and audio working great but the solenoid is causing me an issue. I'm using the circuit linked below (where the motor is the solenoid) but when wired up as shown, it doesn't fire? Weirdly though it did activate after a while but the MOSFET was extremely hot but didn't deactivate as it should have done so I assume the MOSFET may have shorted?

I'm using the MOSFET, voltage booster and solenoid linked to below but I've yet to place a capacitor in the wiring.

As always, any help to get it firing would be greatly appreciated.

Cheers

Dan

Circuit layout: Tinkercad | From mind to design in minutes

Mosfet: https://goo.gl/jwACFD

Voltage booster: DC DC boost converter Constant Current Mobile Power supply 10A 250W LED Driver | eBay

Solenoid: https://goo.gl/7iy9TY

Hi,
That solenid.

DC 24V 10A 10mm Stroke 16kg Force Push Type Solenoid Electromagnet

10Amps....
How are you powering it?
Are you using protoboard?
The MOSFET should not have been the only thing getting hot.
Can you post a picture of your project please.
ATTACH it, DO NOT post it off forum like you did above.

You do not have the gnd of the solenoid supply connected to the gnd of the UNO, they should be connected so you can control the MOSFET.

Tom... :slight_smile:

I’m not seeing any flyback diode in the circuit diagram.

G

Hi Tom

Circuit attached that I designed but I had to improvise some of the components on tinkercad to test the code.

Basically a 3s lipo powers two voltage regulators - one for 3.3v for the arduino, audio and laser; the other larger voltage regulator that I linked to before powers the solenoid. The only connections between these two sides of the circuit are the solenoid MOSFET gate link to the arduino pin and the 0v link as shown in the circuit diagram. I have a suspicion that its something to do with the voltage booster maybe? I’m using veroboard for that side of the circuit, whilst using breadboards for the low voltage side. It’s maybe worth mentioning the whole time the circuit was wired up, the low side worked perfectly and still does.

Easy solution to this (assuming its not the wiring) is to use a 6s lipo and do away with the voltage booster but of course if I’ve made a stupid mistake, please just say!

Cheers

Dan

Zardof:
I'm not seeing any flyback diode in the circuit diagram.

G

That's true. Would that really stop the solenoid from firing though?

If you have the diodes the wrong way. Yes.
G

Zardof:
If you have the diodes the wrong way. Yes.
G

both cables on the solenoid are black so I've assumed it can be wired either way?