Led button control

I need to control the led using pushbuttons. For example., After pressing the button 5 times, the led should blink 5 times.. I tried a lot.. But its not working.. Is this possible...? can anyone help me with the code..?

Welcome to the forum

Please post the full sketch of your best attempt

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

The following program is working as... when it detects the button, led is blinking for 4 times.. but what I need is I have to give the times of led blinking as the button input.... ex.. If I press the button 3 times , led should blink 3 times... Is it possible using pushbutton??

//To blink the led 4 times
const int led=13;
int button =5;
void setup() {
  pinMode(led,OUTPUT);
  pinMode(button,INPUT);
 }
void loop() {
int bstate=digitalRead(button);
if(bstate==HIGH)
{
 for(int i=0;i<4;i++)
 {
 digitalWrite(led,HIGH);
 delay(200);
 digitalWrite(led,LOW);
 delay(200);
}}
}

Yes

You need to detect when the button becomes pressed rather than when it is pressed and count how many times it becomes pressed

See the StateChangeDetection example in the IDE

How will the sketch know when to stop counting button presses and start blinking the LED ?

1 Like

You don't need an Arduino for that. Connect a button, an led and a resistor (~220R) in series from 5V to ground. You will find that if you press the button 3 times, the led will flash 3 times.

1 Like

Yeah! I understood that.. but my doubt is.. can we make the led blink for 5 times after pressing the button 5 times...? If I press it for 10 times., then after that, the led should continuously blink for 10 times. Sorry if My English is wrong..

yes it is.
But first you need to define some program requirements.
a) you want to count button presses. That's easy: See the IDE Example "State change detection" how to identify each button press and how to count
b) But when do you want to start "blinking"?
for example, how long after the last "counter press" do you want to start blinking
c) what should happen after the last blink? back to button count?
d) what should happen if you press the button during the blinking?

When you write down your requirements use as short sentences as possible.
End your sentences with points.
Because you need to translate each sentence in a line of code. Clear and short requirments result in a simple working code.

Yes

Something has been lost in translation I think because if the LED is going to blink continuously then it will not be obvious that it is blinking 10 times. You will need a gap between each set of blinks like this

  • blink the LED the required number of times
  • wait a bit
  • repeat

Do you want the user to be able to change the number of blinks once the sequence has started ?
As asked before, how will the sketch know when to stop counting button presses and start blinking the LED ?

Yes.... How can we set that?

Which of my questions are you asking about ?

A word of warning. In order to do either or both of the things I asked about then the sketch must not use delay() because that stops anything else happening at the same time such as how long the user has taken to press the button or whether the user has pressed the button to start a new count entry whilst the LEDs are blinking

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

consider

#undef MyHW
#ifdef MyHW
const byte PinBut = A1;
#else
const byte PinBut = 5;
#endif

const byte PinLed =13;

byte butState;
int  butCnt;

#define Timeout 500
#define Period  500

enum { Off = HIGH, On = LOW };

unsigned long msecLst;

// -----------------------------------------------------------------------------
void loop () {
    unsigned long msec = millis ();
    if (0 < butCnt && (msec - msecLst) > Timeout)  {
        Serial.println ("Timeout");
        for ( ; 0 < butCnt; butCnt--)  {
            Serial.println (" Period");
            digitalWrite (PinLed, On);
            delay (Period);
            digitalWrite (PinLed, Off);
            delay (Period);
        }
    }

    byte but = digitalRead (PinBut);
    if (butState != but) {
        butState = but;
        msecLst  = msec;

        delay (10);     // debounce

        if (LOW == but)  {
            butCnt++;
            Serial.println (butCnt);
        }
    }
}

void setup () {
    Serial.begin (9600);

    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);

    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}
1 Like

Thankyou! This code is working... But I need to learn the functions of every line since I didn't understand how it is working.... because, I am new to Arduino. I will do that.. anyway thankyou so much...

Read the comments in the code. Oh, wait...

But there is no explanation comments for all the lines

That's exactly my point, hence the "Oh, wait", as in "Oh wait, there are no comments"....

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.