Help whit code for a little project

Hi Guys
Can anyone help out creating a simple code
I have 1 input and 2 outputs
when input goes high i whant first output to go high
when the first output goes low i whant second output to go high for 2 seconds

any help will be much appreciated

what have you tried?

if you don't want to learn, this seems a great ask for the Jobs and Paid Consultancy forum, make sure you mention your budget.

Your post was MOVED to its current location as it is more suitable.

We should be able to help you with your sketch.

void setup() {
pinMode(5, INPUT); // sets the digital pin 5 as input
pinMode(8, OUTPUT); // sets the digital pin 8 as output
pinMode(9, OUTPUT); // sets the digital pin 9 as output
digitalWrite(8, HIGH); // sets the digital pin 8 on
digitalWrite (9, HIGH);
delay(2000); // waits for 2 seconds
}

void loop() {
if (digitalRead(8) == HIGH) { // check if input 9 is made
digitalWrite(9, LOW); // sets the digital output pin 8 off
} else {
digitalWrite(8, LOW); // sets the digital output pin 8 off
delay(2000); // waits for 2 seconds
digitalWrite(8, HIGH); // sets the digital output pin 8 on
delay(2000); // waits for 2 seconds
} if (digitalRead(5) == HIGH) { // check if input 2 is made
digitalWrite(8, HIGH); // sets the digital output pin 8 on
} else {
digitalWrite(8, LOW); // sets the digital output pin 8 off
}
}

thats as far as i got

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

What does the posted code do? How is that different from what you want?

  • if the input is a pushbutton switch, they are conventionally connected between ground and a pin configured with an internal pull-up resistor, INPUT_PULLUP, which means pressing the button makes the input LOW

you don't say what causes the first output to go LOW -- when the input goes LOW? if so

  • need to recognize the change in state of the input and recognize when it becomes low.
  • the output can be set HIGH and use delay to set it LOW after 2 sec

Here you go champ. Knock yourself out:
(Tested here: sketch.ino - Wokwi Arduino Simulator)

#define BTN_PIN 5
#define LED_ONE 9
#define LED_TWO 8

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP); // sets the digital pin 5 as input
  pinMode(LED_ONE, OUTPUT); // sets the digital pin 8 as output
  pinMode(LED_TWO, OUTPUT); // sets the digital pin 9 as output
  Serial.begin(9600);
}

void loop() {
  static byte lastState;
  static unsigned long debounceCapture = 0;
  static unsigned long timer = 0;
  byte currentState = digitalRead(BTN_PIN);
    
  digitalWrite(LED_ONE, !currentState);
    
  if (lastState != currentState) {
    lastState = currentState;

    if (currentState == LOW)
      debounceCapture = millis();
    else if (millis() - debounceCapture > 25) {
      digitalWrite(LED_TWO, HIGH);
      timer = millis();
    }
  }

  if (millis() - timer > 2000)
    digitalWrite(LED_TWO, LOW);
}

Thanks ! st3v3n92

You're welcome.
Are you looking to learn Arduino/C++ ? If you need any parts of the code explaining just ask.

will do..gone use it whit a proximity sensor..the ideea was when sensor picks you up to do nothing and when you step out of range to open a relay for 2 seconds

much appreciated your help

Nice! What does the relay then activate?

its based on a set of traffic lights
when you press a button to cross sensor sends a demand in controller when you step out of range spose to cancel the demand

what about?

#define BTN_PIN A1
#define LED_ONE 13
#define LED_TWO 12

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP); // sets the digital pin 5 as input
  pinMode(LED_ONE, OUTPUT); // sets the digital pin 8 as output
  pinMode(LED_TWO, OUTPUT); // sets the digital pin 9 as output
  Serial.begin(9600);
}

void loop() {
    digitalWrite (LED_ONE, digitalRead (BTN_PIN));

    static byte lastState = HIGH;
    if (lastState != digitalRead (LED_ONE))  {
        lastState = ! lastState;

        if (HIGH == lastState)  {
            digitalWrite (LED_TWO, HIGH);
            delay (2000);
            digitalWrite (LED_TWO, LOW);
        }
    }
}

Then led one becomes unresponsive to button presses while led two is doing its thing. Not an issue, unless you want them to behave independent of each other. Up to Mr.OP

Which I suppose if the IRL use case is a traffic light, it might be preferred not to force the two second wait and cancellation, if someone new has pushed the cross button.

Just throwing thoughts around.

hi st3v3n92 is to much to ask for a connection diagram

If you click on the wokwi link I posted above you can see how it's wired in the simulation program I used

whent by that and am getting first led on all the time..is it 1k resistors?