if pin 1 is high in 3seconds led will turn on else ignore .(if statement .help.)

this is the situation, press the switch in more than 3 seconds before the led turn on unless it wont.

my idea didnt work.

if ( button, HIGH) {
delay(500);
x + 1;
if ( button, HIGH) {
delay(500);
x + 1;
if (button, HIGH) {
delay(500);
x + 1;
if ( x == 3) {

digitalwrite( "leds turns on")
x = 0;
} } } }
else {

x=0;

}

just give me the idea.

if ( button, HIGH) {

It is highly unlikely that the comma operator is doing what you think it is doing. You can't just invent a "command" and expect it to work.

There is a function to read the state of a digital pin. You should figure out what that digitalRead() function is, and how to use it.

Oh, and adding 1 to x and discarding the result is an exercise in futility.

it should be something along this line

if (Switch==HIGH)
{
 x++;
 if (x==3)
 {
   digitalWrite(LED,HIGH);
   x=0;
 }
}

ash901226:
it should be something along this line

if (Switch==HIGH)

{
x++;
if (x==3)
{
   digitalWrite(LED,HIGH);
   x=0;
}

thank you very much with this sir. ill try this style.
but i think if you press the switch 3 times it will light up. (not holding the switch) . though ill just try it if it works..
}

Your code:

if ( button, HIGH) {

Should be :

if(digitalRead(button) == HIGH) {

Your code:

x + 1;

Should be:

x = x + 1;

Your code layout makes it very difficult what your control structure is.

Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.

PeterH:
Your code:

if ( button, HIGH) {

Should be :

if(digitalRead(button) == HIGH) {

Your code:

x + 1;

Should be:

x = x + 1;

Your code layout makes it very difficult what your control structure is.

Please adopt the habit of putting each { and } on separate lines, with matching pair indented by the same amount, and with the code between them indented one extra level. It makes it much, much easier for us (and you) to understand the structure of the code and also to spot where the structure is not what you intended.

pls give me the answer coz my program didnt work. i repeat, there is no problem with the correct code. i only did that for representation (i know its wrong)

this is a very simple problem, though no one can answer even me.

the solution for my answer, i think…

check if the switch is high
delay (100)
check if the switch still high
delay (100)
check if the switch still high
delay (100)
check if the switch still high
turn on the led

but how can i transfer this to a code or something?.

if no one can help me then
ill keep trying to solve this problem.

there is no problem with the correct code.

Then post that code.

ash901226:
it should be something along this line

if (Switch==HIGH)

{
x++;
if (x==3)
{
  digitalWrite(LED,HIGH);
  x=0;
}

}

woohooooooo. yes! it works! for all the answer out their. this is the only one made me smile :grin:,
very good sir, thank you very much!. you only use looping but its the same with mine. what exactly is the one second as a delay? is it delay(1000)?
. you really help me sir. thank you very much.

Moderator edit: gibberish removed

PaulS:

there is no problem with the correct code.

Then post that code.

const int buttonPin = 2;     
const int ledPin =  11;
int x = 0; 
int buttonState = 0;
void setup() {
  
  pinMode(ledPin, OUTPUT);      
  
  pinMode(buttonPin, INPUT);     
}

void loop(){
  
  buttonState = digitalRead(buttonPin);

 
  if (buttonState == HIGH) { 
  x++;    
  
    delay(1000);// turn LED on:  
    
   if (x==3) {
          
    digitalWrite(ledPin, HIGH);
    x=0;
    
    }
  }
  
  
  else {
   
    digitalWrite(ledPin, LOW); 
  }
}

Does that code do what you want ?

What happens if the user presses the button for 2.5 seconds then releases it ?
What will the value of x be then ?

What value will x have after the user now presses the button for 1 second ?

Im sorry jaylisto, The program that i gave to you does not do what you need it to do. I thought you need to press the switch 3 time b4 the led will light up. so its does not do acording to what you want.
btw i want to say its not good that you anger all those post a reply for your question. those people have help me learn alot.

const int buttonPin = 2;     
const int ledPin =  11;

int buttonState = LOW;
int  lastReading = LOW;
long onTime = 0;

void setup() { 
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}

void loop(){ 
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastReading == LOW) {
    onTime = millis();
  }

//held
  if (buttonState == HIGH && lastReading == HIGH) {
    if ((millis() - onTime) > 3000 ) { //(current time - first pressed time) must be greater than 3000 (3 seconds)
       digitalWrite(ledPin, HIGH);
       lastReading = LOW; //reset state
    }
  
  else {
    digitalWrite(ledPin, LOW); 
    }
  }
  lastReading = buttonState; //write button to lastreading for compare
}

I can not try this myself right now, but it should work.

Of course, there are always people who will write code for others despite any attitude problems.

I’ve removed a lot of invective and insults from this thread.
OP, if this continues, this thread goes and your account risks going with it.

@Arrch
I wanted to see if he would insult me too afterwards, If he did, I would've removed my post and leave him with nothing. I can tolerate rude people to an extent, as I do everyday as a tech support technician. But if I try to help them and they are still ungreatful, then they get blocked.

I didn't get insulted by him yet, so I guess it worked.

UKHeliBob:
Does that code do what you want ?

What happens if the user presses the button for 2.5 seconds then releases it ?
What will the value of x be then ?

What value will x have after the user now presses the button for 1 second ?

ow. wow, thank you very much. i didnt recognize it. i tried it, it looks solved but i realized that you are correct.
i think i need to add x = 0 at the else statement. thank you for the post.

HazardsMind:

const int buttonPin = 2;     

const int ledPin =  11;

int buttonState = LOW;
int  lastReading = LOW;
long onTime = 0;

void setup() {
  pinMode(ledPin, OUTPUT);     
  pinMode(buttonPin, INPUT);   
}

void loop(){
  buttonState = digitalRead(buttonPin);

if (buttonState == HIGH && lastReading == LOW) {
    onTime = millis();
  }

//held
  if (buttonState == HIGH && lastReading == HIGH) {
    if ((millis() - onTime) > 3000 ) { //(current time - first pressed time) must be greater than 3000 (3 seconds)
      digitalWrite(ledPin, HIGH);
      lastReading = LOW; //reset state
    }
 
  else {
    digitalWrite(ledPin, LOW);
    }
  }
  lastReading = buttonState; //write button to lastreading for compare
}




I can not try this myself right now, but it should work.

brilliant! wow, i never know this kind of style. in school i never use this kind of style. you compared it in the last value of the variable.
very good sir. i will try this one. i will bow my head for you and say thanks.
and! what do you mean by the code millis();?

by the way since you are a genius,i have an out of topic question for you.
is it possible to multi quote? so that i can reply to all who posted in my topic easily?. thanks genius man.

Multi quote? Not that im aware of. Usually you just write "@To all who helped", or something like that.

Once you write the new post, the people who did help, will read it.

HazardsMind:
@Arrch
I wanted to see if he would insult me too afterwards, If he did, I would've removed my post and leave him with nothing. I can tolerate rude people to an extent, as I do everyday as a tech support technician. But if I try to help them and they are still ungreatful, then they get blocked.

You're better than I am at it. I have the philosophy that if you insult anyone trying to help, not just me, I'm not helping until they are apologized too, but that wouldn't work too well for tech support I would imagine.