Go Down

Topic: if pin 1 is high in 3seconds led will turn on else ignore .(if statement .help.) (Read 1 time) previous topic - next topic


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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


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.


Code: [Select]

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();

  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.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID


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

Go Up