Pages: [1] 2 3   Go Down
Author Topic: if pin 1 is high in 3seconds led will turn on else ignore .(if statement .help.)  (Read 1353 times)
0 Members and 1 Guest are viewing this topic.
isabela, philippines.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
if you dont go, you will never know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

my idea didnt work.
Code:

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.
Logged

jaylisto

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it should be something along this line

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

Logged

isabela, philippines.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
if you dont go, you will never know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it should be something along this line

Code:
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..
}

Logged

jaylisto

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code:

Code:
if ( button, HIGH) {

Should be :

Code:
if(digitalRead(button) == HIGH) {

Your code:

Code:
x + 1;
Should be:
Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

isabela, philippines.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
if you dont go, you will never know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code:

Code:
if ( button, HIGH) {

Should be :

Code:
if(digitalRead(button) == HIGH) {

Your code:

Code:
x + 1;
Should be:
Code:
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.
Logged

jaylisto

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
there is no problem with the correct code.
Then post that code.
Logged

isabela, philippines.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
if you dont go, you will never know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it should be something along this line

Code:
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  smiley-mr-green,
 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
« Last Edit: January 14, 2013, 01:36:36 pm by AWOL » Logged

jaylisto

isabela, philippines.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
if you dont go, you will never know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
there is no problem with the correct code.
Then post that code.

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);
  }
}
« Last Edit: January 14, 2013, 01:51:00 pm by AWOL » Logged

jaylisto

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3526
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2945
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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.
« Last Edit: January 14, 2013, 11:30:57 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 14, 2013, 01:50:06 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2 3   Go Up
Jump to: