Unusual use of a switch

I would like to use a switch with an arduino nano in such a way that when the switch is thrown into the closed position a pin on the nano only receives a half second High signal then returns to LOW even though the switch remains closed. Would someone be kind enough to provide me with the wiring and code? Thank you in advance for all the kind folks on this forum! P.S. I do not want to use a button because the user pushing the button could hold it longer than half a second, thus defeating my time requirement.

This isn't a free code writing service. You can post in the jobs section and offer to pay if you want someone else to do it for you. Otherwise you get started and people here will help you along the way.

I don't understand. The switch would also stay closed longer than half a second.

I think we have an X-Y problem here. Please explain what you actually want to do. What's really going on?

That requires some circuitry between the switch and the Arduino. However, the Arduino can be programmed to produce a single pulse on some output, beginning when the switch is closed.

If the person opened and closed the switch again within the half second period what would you want to happen?

1 Like

Forgive me, I was not asking for code and wiring to launch a space shuttle model! I was under the impression this forum had people willing to help a total greenhorn with a very simple solution in order to begin understanding how arduino programing a wiring works. I did not realize it was a place for EXPERTS such as yourself to condemn us for asking. As a poor dumb peasant, I will refrain from any future posts your exalted highness!

What you are asking for is pretty much covered by introductory tutorials and examples. If you do those, use the ideas they give you to attempt to do it yourself then get stuck you can show the folk here your best effort and ask about whatever problems you encounter, then you will most likely get help. As it stands you are just asking for someone to do it for you without making any effort to try yourself, which generally gets a poor response.

1 Like

This article by a forum regular (@cedarlakeinstruments ) will give you a good place to start.
One-Shot

3 Likes

That’s exactly why we’re here.
Tell us how you’d like to approach the problem, what it does, and we’ll help you LEARN.
We’ll ask you a bunch more questions, and you’ll have to write and draw some - as will we.

2 Likes

You've been posting here (and presumably working with Arduinos) for 2-1/2 years.

I'm sorry. I did not realize that I was here just to serve up code to you any time you are too lazy to use google. Man, you should ask for your money back from me. Matter of fact, here it is. All none of it.

1 Like

What you ask is not possible of a simple switch. See post #3

1 Like

I asked chatGPT your question


Response:

Certainly! You can achieve this by using a switch and programming the Arduino Nano to generate a short pulse when the switch is closed. Here’s a simple wiring and code example:

Wiring:

  1. Connect one side of the switch to a digital pin on the Arduino Nano (e.g., D2).
  2. Connect the other side of the switch to the ground (GND) pin on the Arduino Nano.
  3. Optionally, you can use a pull-up resistor by connecting a resistor (e.g., 10kΩ) between the digital pin and the 5V pin on the Arduino Nano.
  4. Additionally, there is a triggerPin defined, representing the pin to which you want to send the short pulse when the switch is closed. if you want to drive an ignition device using triggerPin D3, connect the triggerPin (e.g., D3) to the ignition device using an appropriate interface like a MOSFET, relay, or optocoupler.

Code:

const int switchPin = 2;  // Replace with the actual pin you used
const int triggerPin = 3;  // Replace with the actual pin you used (e.g., D3)
const unsigned long pulseDuration = 500;  // Pulse duration in milliseconds (0.5 seconds)

void setup() {
  pinMode(switchPin, INPUT_PULLUP);  // INPUT_PULLUP enables the internal pull-up resistor
  pinMode(triggerPin, OUTPUT); // Set the trigger pin as an output for different triggering mechanisms
}

void loop() {
  int switchState = digitalRead(switchPin);

  if (switchState == LOW) {
    // Switch is closed, generate a short pulse
    digitalWrite(triggerPin, HIGH);
    delay(pulseDuration);
    digitalWrite(triggerPin, LOW);
    while(digitalRead(switchPin) == LOW) ; // wait for release
    delay(50); // debounce
  }

  // Add any other code you need to run during each loop iteration here
}

Explanation:

• The switch is connected to a digital pin (switchPin) configured as an input with a pull-up resistor to ensure a stable signal.
• In the setup() function, triggerPin is set as an output to enable it to send the pulse signal. You can adapt the triggerPin for different triggering mechanisms, such as MOSFET, relay, or optocoupler.
• In the loop() function, it continuously monitors the state of the switch.
• If the switch is closed (LOW), it momentarily sets the triggerPin to HIGH for the specified pulse duration (0.5 seconds) and then brings it back to LOW.

This code ensures that even if the switch is held in the closed position, the signal on the triggerPin will only be high for half a second. Adjust the switchPin and triggerPin based on the actual pin connections in your setup.

I hope this provides the information you’re looking for!

1 Like

If it works great, if not chatGPT will try to confuse help you without complaining…and without guidance on security risks with your ignition device…

If you want our help, we usually ask in exchange that you show commitment and willingness to learn…

3 Likes

ChatGPT doesn't know about INPUT_PULLUP? LOL!

You weren't asking.

You were begging and condescending.

Don't you think that a switch can also be kept in a certain position for an extended time and hence defeating your timing requirement?

That would require external hardware (a one-shot as mentioned by @EmilyJane in post #10). The alternative is to implement it in software based on a state change detection; you detect when an input goes from HIGH to LOW (or vice versa) and react on that; it's basically what the code in post #16 by @J-M-L does.

1 Like

i only tinker rarely. although i have been on here that long i have only ever done a couple of projects.

thanks goes to chatGPT...

be extra careful if you are playing with an ignition device - proper grounding, ensuring there is no risks of false triggering etc...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.