Delay with stop

Hi,

Im just a beginner with the Arduino

I would like to make a script but i can't find how to make it.
-When Input 1 go HIGH> OUTPUT 2 has to go HIGH for 1 second en go LOW en stop.
-When Input 1 go LOW do nothing.
-When Input 1 goes HIGH again> OUTPUT 2 go HIGH for 1 Second en go LOW end stop.

I hope i did explain it clearly.

(it is just what i will, just a example, not the correct outputs en inputs).

I hope someone can help me.

What you have done until now? What is your main problem?

Start with

When Input 1 go HIGH> OUTPUT 2 has to go HIGH

Come back and post your code when you have got that working.

int LedPin13 = 13; // LED on pin 13
int InputPin = 2; //Switch on pin 2
int val = 0; // variable on pin_status

void setup() {
pinMode (LedPin13, OUTPUT); // pin 13 Output
pinMode (InputPin, INPUT); // pin 2 input
}

void loop () {
val = digitalRead(InputPin); // Read Value input
if (val == LOW) { //check of input is High
digitalWrite(LedPin13, LOW); // sets the LED on
delay(1000); // waits for a second
digitalWrite(LedPin13, HIGH); // sets the LED off
}
else digitalWrite(LedPin13, HIGH);

}

Buth when i use this it go only High en never goes OFF

And yes i know it is a sample to find on the internet, but i cant find something that not work like a loop as what i will.
Change the delay is not a option because is stops working for the whole script.

UKHeliBob:
Start with Come back and post your code when you have got that working.

int LedPin13 = 13; // LED on pin 13
int InputPin = 2; //Switch on pin 2
int val = 0; // variable on pin_status

void setup() {
pinMode (LedPin13, OUTPUT); // pin 13 Output
pinMode (InputPin, INPUT); // pin 2 input
}

void loop () {
val = digitalRead(InputPin); // Read Value input
if (val == LOW) { //check of input is High
digitalWrite(LedPin13, LOW); // sets the LED on
}
else digitalWrite(LedPin13, HIGH);

}

How do you have connected the button? It seems that you can't read the pin going low.

On this moment just with a breadboard, with a resist, en some wire, not a real button. But this like to work, when i open the swith the LED goes on en off when i close him.

With a simple script, what i posted on 12.52PM

So is working, right? But you don't know how to change things to do what you want? I'm sorry but I don't understand what is your problem. The first code that you post is almost there.

Yes i thought to, but when i use that code, But the problem is when i push open the switch the LED goes ON but don't go OFF. there is only a extra delay.

OK. Now I understand. The LED goes HIGH, but is very quick, then the button is read other time and as it is "pushed" the LED goes LOW again.

Maybe something like this, will work:

int LedPin13 =  13; // LED on pin 13
int InputPin = 2;  //Switch on pin 2
int val = 0; // variable on pin_status
int last_val;
void setup() {
  pinMode (LedPin13, OUTPUT); // pin 13 Output
  pinMode (InputPin, INPUT); // pin 2 input
}

void loop () {
  val = digitalRead(InputPin); // Read Value input
  if (val == LOW && val != last_val) { //check of input is High
  digitalWrite(LedPin13, LOW);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(LedPin13, HIGH);    // sets the LED off
  }
  else digitalWrite(LedPin13, HIGH);
 
 last_val = val;
  }

Thanks it is working, you saved my day

AndreHel:
On this moment just with a breadboard, with a resist, en some wire, not a real button. But this like to work, when i open the swith the LED goes on en off when i close him.

With a simple script, what i posted on 12.52PM

Here is your code with a few changes to the style, layout and variable declarations

const byte ledPin =  13;
const byte inputPin = 2;
byte inputState = 0;

void setup()
{
  pinMode (ledPin, OUTPUT);
  pinMode (inputPin, INPUT_PULLUP);
}

void loop ()
{
  inputState = digitalRead(inputPin);
  if (inputState == LOW)
  {
    digitalWrite(ledPin, HIGH);   // sets the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}

So, why the changes ?
INPUT_PULLUP activates the built in pullup resistors so you can remove the external ones. All you need to do is to take the input pin to GND to turn the switch on. Note that the test for the button being pressed now looks for LOW rather than HIGH.

All the variables are declared as byte rather than int to save memory. Not important here but a good habit to get into.

Variables whose value will not change are declared as const. This is a good way to stop them being changed later in the program by mistake.

Comments have been removed because the variable names and code make the actions self explanatory.

Braces have been used round every code block even if the code block is only one line and each brace is on its own line to make it more obvious where the code blocks start and end.

I suggest that you test this code and wiring and let us know if it works then we can advise on the next bit of coding.

const byte ledPin = 13; // LED on PIN 13 (Byte low MEM)
const byte inputPin = 2; // Input on PIN 2 (Byte low MEM)
byte inputState = 0; // MEM place ?
byte last_inputState; // MEM place ?

void setup()
{
pinMode (ledPin, OUTPUT); //make OUTPUT
pinMode (inputPin, INPUT_PULLUP); // make Input (without Resitor)
}

void loop ()
{
inputState = digitalRead(inputPin); // read value input
if (inputState == HIGH && inputState != last_inputState) //check of input is HIGH
{
digitalWrite(ledPin, LOW); // sets the LED OFF
delay(1000); // wait for 1 sec
digitalWrite(ledPin, HIGH); // sets the LED ON
}
else digitalWrite(ledPin, HIGH); // Sets the LED ON
last_inputState = inputState; // END of Loop

}

This is what i did make from both of the scripts.
It is more what i want, but you (UKHeliBob) saved some Memory of the Arduino board.
Maybe you can check it of mistakes from me of other stuff, because im a beginner.

HeliBob…
I suspect your code example could be simplified somewhat…

const byte ledPin =  13;
const byte inputPin = 2;


void setup()
{
  pinMode (ledPin, OUTPUT);
  pinMode (inputPin, INPUT_PULLUP);
}

void loop ()
{
    digitalWrite(ledPin, !digitalRead(inputPin));
}

HeliBob…
I suspect your code example could be simplified somewhat…

True, but I was using the original program as a basis for the changes and wanted to make several points about style, layout and variable declarations rather than propose a completely different solution.