button

hi all, can you help me,,? I wanted to pin 13 LED blink after pressing the button (1) for 4 times,
and led pin13 blink after pressing the button (2) for 3 seconnd

 State change detection (edge detection)

Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON.  This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on
and on to off.

The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
  most Arduino boards)

created  27 Sep 2005
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ButtonStateChange

*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 10;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
 // initialize the button pin as a input:
 pinMode(buttonPin, INPUT);
 // initialize the LED as an output:
 pinMode(ledPin, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
}


void loop() {
 // read the pushbutton input pin:
 buttonState = digitalRead(buttonPin);

 // compare the buttonState to its previous state
 if (buttonState != lastButtonState) {
   // if the state has changed, increment the counter
   if (buttonState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     buttonPushCounter++;
     Serial.println("on");
     Serial.print("number of button pushes:  ");
     Serial.println(buttonPushCounter);
   } else {
     // if the current state is LOW then the button
     // wend from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,
 //for next time through the loop
 lastButtonState = buttonState;


 // turns on the LED every four button pushes by
 // checking the modulo of the button push counter.
 // the modulo function gives you the remainder of
 // the division of two numbers:
 if (buttonPushCounter % 3 == 1) {     
   digitalWrite(ledPin, HIGH);
     delay(100);
         digitalWrite(ledPin, LOW);     
 } else {
   digitalWrite(ledPin, LOW);


       
 }

}

/*

I wanted to pin 13 LED blink after pressing the button (1) for 4 times,
and led pin13 blink after pressing the button (2) for 3 seconds

So what have you tried ?

Do not consider using the delay() function when blinking the LED as it will slow down reading inputs. Look at the BlinkWithoutDelay example.

The code in this link shows how to require a button to be pressed for a period of time

IMHO it would be easier to do

if (buttonPushCounter == 4)

rather than

if (buttonPushCounter % 3 == 1)

Have a look at Planning and Implementing a Program - what you are trying to do will be much easier if you organize the code into small single-purpose functions. And don't use delay() if you want a responsive program.

And please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

...R

In general

if (buttonPushCounter % 3 == 1) {   

Should be written

if (buttonPushCounter % 4 == 0) {

To really check every 4 ticks. But You have a challenge at start thought because buttonPushCounter is 0 and thus its modulo by anything is 0.

Your first case will work because 4 modulo 3 is 1 But then because you don't reset buttonPushCounter, when it gets to 7 the modulo will be 1 so you trigger after 3 clicks, then at 10...

Also another good reason fo resetting it is that it will grow past 32768 and turn negative if you use the program long enough

Your serial.print will be super long compared the the debounce you have in there. Be aware that at 9600 bauds that can cause weird behaviors if you rely on quick action.

I am still learning phase arduino programming, so I'm still confused, does anyone want to give examples of coding I want?

previous,, thanks and sorry if my english is not good,. Greetings from Indonesia

The point for learning is trying - read the feedback above and offer modification of your code

fathur:
does anyone want to give examples of coding I want?

What is wrong with the example I gave you in Reply #2 ?

...R

//Robin2

the lights can not be turned again after a 4x push,, must first press the reset button

I change like this, how to stop the blink, I want to just 1x blink

  if (buttonPushCounter  ==4  ) {
    digitalWrite(ledPin, HIGH);
        delay(100); 
    digitalWrite(ledPin, LOW);
      delay(100); 
  }else {
    digitalWrite(ledPin, LOW);

    
    }
  }

fathur:
//Robin2

the lights can not be turned again after a 4x push,, must first press the reset button

You need to post the complete program that you wrote taking account of the code in the link I gave you. The link was not intended to meet your complete requirement - I thought I should leave a little work for you. :slight_smile:

...R

fathur:
I change like this, how to stop the blink, I want to just 1x blink
[/code]

Just push the button again :wink:

Seriously though, just keep a counter to count the number of flashes. When you reach the number of flashes (5 in below example to differentiate from the 4 pushes on the button), reset whatever needs to be reset.

void loop()
{
  static byte flashcounter = 0;   // <------ ADD THIS at the beginning from loop()

  // existing code
  ...
  ...

  // your code to flash the led; modified

  if (buttonPushCounter == 4)
  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);

    // count number of flashes
    flashcounter++;               // <------ ADD THIS and the following if block
    if(flashcounter >= 5)
    {
      // reset flash counter
      flashcounter = 0;
      // reset button push counter
      buttonPushCounter = 0;
    }
  }
  else 
  {
    digitalWrite(ledPin, LOW);
  }
}

And as Robin said; in future please post the complete code.