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 1357 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Online Online
Faraday Member
**
Karma: 67
Posts: 2947
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

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.

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.
« Last Edit: January 15, 2013, 09:34:36 am by jaylisto » Logged

jaylisto

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

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.

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

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

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.

thank you for your help, now it works like charm.  i added x=0 in the else statement, so that when you releases the switch in 2.5 second it will reset the value of x to 0.


Code:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  11;
int x = 0; // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  x++;   
 
    delay(1000);
   
   if (x==3) {
         
    digitalWrite(ledPin, HIGH); // turn LED on: 
    x=0;
   
    }
  }
 
 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
   x=0;                              //to set the x as zero if its under 3 second, brilliant!
  }
}
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

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.

your code works!!! i already tried it.
but my code also works as i add x = 0 in the else statement. anyway thank you.
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

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.
wow, your code is amazing, since i realize in my arduino other programs are affected due to delay,
how did you do this thing? you did not use delay.

can you explain how did this thing works?
Logged

jaylisto

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

Have a look at, and more importantly play with, the blink without delay example in the IDE, without delay.
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.

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

You know the time that the button was pressed (onTime) copied from millis() when it happened.
Later there is a check whether the current time, millis(), minus onTime is greater than 3000.  If it is, then 3000 milliseconds (3 seconds) have passed and something needs to be done.  If not then don't hang around waiting, get on with something else and check the elapsed time again next time round the loop()

Does that help ?
Logged

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

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

You know the time that the button was pressed (onTime) copied from millis() when it happened.
Later there is a check whether the current time, millis(), minus onTime is greater than 3000.  If it is, then 3000 milliseconds (3 seconds) have passed and something needs to be done.  If not then don't hang around waiting, get on with something else and check the elapsed time again next time round the loop()

Does that help ?

the millis records the time between the button is pressed and released? right?

ok, if the value of the millis is passed to "ontime"

why did he minus the millis to the ontime?

it supposed to be ontime>3000

but the code works well. i wonder why.
Logged

jaylisto

NE PA
Offline Offline
Full Member
***
Karma: 5
Posts: 156
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

mills() is the time in milliseconds since the arduino powered up
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

Have a look at, and more importantly play with, the blink without delay example in the IDE, without delay.

ok from your code its hard to transfer what i have done,

i dont know if its better to make another topic or just post it here, i have just added a sensor.

if the analog pir sensor has an output and it trigers in 3 seconds just like the switch before. the led 13 must turned on.

this is the working code with delay.

Code:

// Uses a PIR sensor to detect movement, buzzes a buzzer


int ledPin = 13;                // choose the pin for the LED

int x = 0;
int inputPin = A0;               // choose the input pin (for PIR sensor)

            // we start, assuming no motion detected

int val = 0;                    // variable for reading the pin status


void setup() {

  pinMode(ledPin, OUTPUT);      // declare LED as output





}

void loop(){

  val = analogRead(inputPin);  // read input value

  if (val < 1) {            // check if the pir analog has trigerd (i just put one because the analog pir sensor produces
                                    morethan  1 ohms when it detects something),.
  x++;
 delay(1000);
if (x==3){
 

    digitalWrite(ledPin, HIGH);  // turn LED ON
     x=0;


  }
  else {

    digitalWrite(ledPin, LOW); // turn LED OFF

   x=0;

    delay(300);     



  }

}


   



this code worked, the light turns on when the sensor sens a person in 3 seconds.






without delay style

, implemented to your code.


Code:

const int pirbuttonPin = A0;     
const int ledPin =  13;


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

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

void loop(){
  buttonState = analogRead(pirbuttonPin);
 
 

  if (buttonState == true && lastReading == false) {
   
    onTime = millis();
   
  }

//held
  if (buttonState == true && lastReading == true) {
    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
}


it did not worked, what is wrong with my code? hope some one helps me.
« Last Edit: January 20, 2013, 09:47:02 pm by jaylisto » Logged

jaylisto

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

The reason it's not working is because analog gets values 0-255 and digital is 0 or 1. So as of right now your code is looking for a value of 255 to trigger. Your pin might not be putting out 255 but something like 168 or something. So you need to set a range to trigger the if statement to be true.

First you need to see what the value is, then set the IF statement to trigger at that value. It's the same in theory, but with a different setup. Instead of digital readings, it's analog.
Logged

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

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

The reason it's not working is because analog gets values 0-255 and digital is 0 or 1. So as of right now your code is looking for a value of 255 to trigger. Your pin might not be putting out 255 but something like 168 or something. So you need to set a range to trigger the if statement to be true.

First you need to see what the value is, then set the IF statement to trigger at that value. It's the same in theory, but with a different setup. Instead of digital readings, it's analog.

the upper code is working well, with the if statement greater than 1 . i have messured the analog sensor in digital tester as 0 = (no detection) and more than one ohms if detected.. so i did the line <1 ..... working as good. but when i use without delay method. wont work at all.
Logged

jaylisto

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