Go Down

Topic: button control (Read 372 times) previous topic - next topic

farsajik

hi.my name is Rashad,I am new in Arduino.I use button library for debouncing.I use two button.I need when I press button1 led1 flashs 10 times.But when led1 flashs button2 doesnt work until flashing finishs.how to write program for two button work in the same time?thanks

Code: [Select]

#include <Button.h>

/*
  Wire like this:
  GND -----/ button ------ pin 12
*/
Button button1 = Button(11,BUTTON_PULLDOWN);
Button button2 = Button(9,BUTTON_PULLDOWN);

int led1=10;
int led2=12;

void setup()
{
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
}

void loop()
{
 
//***********************************
  if(button1.isPressed())
  {
  for (int i=0; i<10; i++)
    {
    digitalWrite(led1,HIGH);
    delay(1000);
    digitalWrite(led1,LOW);
    delay(1000);
    }
  }
//*************************************
if(button2.isPressed())
  {
    digitalWrite(led2,HIGH);
  }
  else
  {
    digitalWrite(led2,LOW);
  }

JimboZA

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

AWOL

I'm thinking of applying to have that reply to be given its own dedicated control   :D
"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.

billroy

It needs a number, for sure.  Single-digit, probably.

Thinking in terms of root-cause analysis, the real answer is to delete the Blink example, and rename BlinkWithoutDelay to Blink.

-br

JimboZA


rename BlinkWithoutDelay to Blink.



And Blink to BlinkWITHDelay8)
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

PaulS

Quote
the real answer is to delete the Blink example

I think BlinkTheStupidWay and BlinkTheSmartWay should be the names of the sketches.

AWOL

:)

OP, your problem is that when the processor is executing the "delay()"s in your sketch, it can't be doing anything else, like looking for button presses.

The trick is to remember that changing the state of a LED from on to off or vice versa, or looking at the state of a switch take very, very little time, but sitting in a "delay()" means you can't do anything else, so, the "delay()"s have to go!
"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.

PeterH


I'm thinking of applying to have that reply to be given its own dedicated control   :D


Damned fine idea! While we're on the subject, could I also suggest that any text pasted into the edit window should automatically be enclosed in code tags? My guess is that would reduce the moderator workload by a large factor.

Yes, I know this isn't the right place to suggest it ... :smiley-roll:
I only provide help via the forum - please do not contact me for private consultancy.

Go Up