Hold push button to switch several outputs

Hello,

I was wondering if the next thing could ben done.
I have a situation where I want to be able to switch situations on 3 outputs with 1 single push button.

I have and arduino connected to 2 leds (a red one and a green one) and I have another output.

The situation I want to create is when the arduino is powered the red LED is on, the green LED is off and the 3rd output is low.
When holding the button for three seconds, I want the red led to switch off, the green LED to switch on and the 3rd output to go high.

When I press the button for 3 seconds again I want it to to go back to it's original situation again.

So I want it cycle between

Power on state:
Red LED on
Green LED off
3rd output low

Press for 3 seconds
Red LED off
Green LED on
3rd output high

Press for 3 seconds
switch it back, and so on and so on.

I have a situation when I press the button it already does this.
But I want the situation to be like I have to hold the button for 3 seconds.

How can I achieve this ?

Nicely explained project.
It’s a great starter for a new user.

To start out, I’d suggest working through the built-in tutorials in the Arduino IDE

Try to understand one thing at a time, then stitch them together.

While plugging through the lessons, play with the miliis() function for your timing. delay)) is useful but only in the simplest situations when you understand how it works,

Come back to this same thread to ask more questions about the same project.

Thank you so much!!

I have it figured out now!

/*
  j3r03n85's Famciom/NES clone 50/60Hz Video mode switcher for TA-02NP PPU's

  In this sketch I use an arduino to switch PIN 16 of my TA-02NP clone NES PPU from 50 to 60Hz
  using the reset button.
  The reset button still acts as a normal reset button on the console, but when you hold it for
  a brief moment you'll see the power LED changing color and the PPU will be switched from
  50 to 60Hz.
  When holding the reset button again for the same amount of time will switch it back to the mode
  it was before.
  The main reason I used an arduino is that I didn't want to drill a hole in my case for a
  physical switch.
  
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 8;  // the number of the pushbutton pin
const int REDled = 11;    // the number of the RED LED pin
const int GREENled = 12;    // the number of the GREEN LED pin
const int VIDMODE = 3;    // the number of the Video output mode


// Variables will change:
int REDledState = HIGH;        // the current state of the output pin
int GREENledState = LOW;
int VIDMODEState = LOW;
int buttonState;            // the current reading from the input pin
int lastButtonState = LOW;  // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 1000;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(REDled, OUTPUT);
  pinMode(GREENled, OUTPUT);
  pinMode(VIDMODE, OUTPUT);

  // set initial LED state
  digitalWrite(REDled, REDledState);
  digitalWrite(GREENled, GREENledState);
  digitalWrite(VIDMODE, VIDMODEState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        REDledState = !REDledState;
        GREENledState = !GREENledState;
        VIDMODEState = !VIDMODEState;
      }
    }
  }

  // set the LED:
  digitalWrite(REDled, REDledState);
  digitalWrite(GREENled, GREENledState);
  digitalWrite(VIDMODE, VIDMODEState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

The project I am working on is a famicom clone which has a CPU that can switch between 50 and 60hz video mode by having one pin of the PPU tied to either GND (50Hz) or +5V (60Hz)

Normally it would be easier to just lift that pin and put a toggle switch on it, but I don't want to drill a hole in this case.
So I was thinking to put the arduino in as the middle man and using the already existing reset button and replacing the power LED with a 2 color LED.
So when powering on it it will be in the original state (50Hz) and the LED will be RED, and when holding the reset button for 3 seconds, it will switch to 60Hz and my led will be green.
If I want to swtich back to 50Hz, all I have to do is hold that reset button for 3 seconds again.

Now all I have to do is check how much current that one of the ppu draws.

[EDIT] - the pin uses less than 4mA so I can hook it up to an I/O pin on the arduino safely

Skip the Arduino, put a latching magnetic reed switch on the inside of the case, and use a magnet to switch it from outside.

will that achieve my goal?
Holing a button for an x amount of time, change the color of the LED so I know the video mode changes?

If that's what you really need, probably not. But, you stated that the reason you weren't using a switch is because you didn't want to drill a hole in the case, so I gave you an option. YMMV.

Ah I see !

Gonna try this code on my real arduino now.
It works in my simulator.

Tell us about your simulator. Pst a link to it!

Our do you mean your simulation running on some simulator we may know about?

If so, woyncha post a link to your simulation?

a7

Yeah I used an online simulator to check my code.

you can find it here:

I am familiar with that one. Post a link to your simulation.

TIA

a7

in the sim I use a yellow led on the video output pin to see how it responds

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