ButtonStateDetection

Hi brother, using the ButtonStateChange sketch, i need to turn ON the led 5 second.So,any one can help me .

//ButtonStateChange with led turn ON 5 second only//
const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);}
void loop() {
buttonState = digitalRead(buttonPin);
delay(100);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);}
else {Serial.println("off"); }}
lastButtonState = buttonState;
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {digitalWrite(ledPin, LOW);}}.

Hi, sorry, I’m not your brother or any other relation, but I will show you how to make your code readable. Use Tools–>Auto Format, then paste the code into your post using code tags </> so it looks like this:

//ButtonStateChange with led turn ON 5 second only//

const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  delay(100);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    else {
      Serial.println("off");
    }
  }
  lastButtonState = buttonState;
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

Your code does not seem to do what you seem to be saying you want it to do. It appears to count the number of times the button is pressed, and turn the led on or off every 4th press. Why is it doing this and how do you want to combine this with the led on for 5 seconds?

Paul

digitalWrite(ledPin,HIGH);
delay(5000);
digitalWrite(ledPin,LOW);

I forgot this word.

No, what you clearly forgot was to use Auto Format (it’s under “Tools” or Control-T) and to post your full code properly as was explained to you.

Go and read the instructions, then go back and modify each of your posts (use the “More → Modify” option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, the code you post could well be garbled and is certainly anything but easy to read.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

Until you reply and demonstrate that you understand these points that have been made so far, there really is little point working through the other problems with the code, so I will leave it at that for the present.

zainimechanical:
digitalWrite(ledPin,HIGH);
delay(5000);
digitalWrite(ledPin,LOW);

I forgot this word.

I don't understand what you are saying.

So you want LED to bright for a 5 seconds while you have pressed the button? Right?

void setup() {  
  pinMode(1, OUTPUT);          //LED connected to pin 1 & output
  pinMode(3, INPUT);           //BUTTON connected to pin 3 & input
  }

void loop() {
  if (digitalRead(3) == HIGH) {             //if you hold BUTTON pressed
  digitalWrite (1, HIGH);                   //LED will bright
  delay (5000);                             //for 5 seconds
  digitalWrite (1, LOW);                    //then LED will turn off
  delay (5000);                             //and wait 5 seconds and loop will start again 
  }                            
  else {} 
}

...so thats when you hold button...but if you want that, when you press button, but not hold it, loop will run. Then you will need to record time in millis() when is button high and record again when is button low and then you can do with if command: if (recWhenButtonIsHigh-recWhenButoonIsLow != 0) {/then led bright for 5 seconds/} . Also you will need to use while (button = LOW/HIGH); , before if command, and you record in millis() with command; unsigned long recWhenButtonIsHigh = millis(); P.S. != means different.

-LUCO19