I need a little help / guidance with running a function off a button press.


i have button wired stated in button tutorial.
the button in question push close.
i want able press button , run function ramped led flash takes 2 seconds run.

the code stands work if hold button down.

i appreciate if point me in right direction working.

please bear in mind i`m quite new this, code may bad or incorrectly formatted.  ive tried best looking presentable.

many thanks,

andy

code: [select]

const int button1pin = 16;
int flashled = 5;

int button1state = low;
long lastflashdelay=0;

void setup()
{
  pinmode(flashled, output);     
  pinmode(button1pin, input);     
}

void flash()
{
  long flashinterval=3200;
  unsigned long currentmillis = millis();
  if(currentmillis - lastflashdelay > flashinterval)
  {
      analogwrite(flashled, 2);
  }
  if(currentmillis - lastflashdelay > flashinterval+200)
  {
      analogwrite(flashled, 5);
  } 
 
  if(currentmillis - lastflashdelay > flashinterval+400)
  {
      analogwrite(flashled, 8);
  } 
 
  if(currentmillis - lastflashdelay > flashinterval+600)
  {
      analogwrite(flashled, 11);
  } 
 
  if(currentmillis - lastflashdelay > flashinterval+800)
  {
      analogwrite(flashled, 14);
  }
 
  if(currentmillis - lastflashdelay > flashinterval+1000)
  {
      analogwrite(flashled,17);
  } 
 
  if(currentmillis - lastflashdelay > flashinterval+1200)
  {
      analogwrite(flashled, 20);
  } 
 
   if(currentmillis - lastflashdelay > flashinterval+1400)
  {
      analogwrite(flashled, 23);
  } 
 
  if(currentmillis - lastflashdelay > flashinterval+2000)
  {
      analogwrite(flashled, 255);
  }
 
  if(currentmillis - lastflashdelay > flashinterval+2080)
  {
      analogwrite(flashled, 64);
  }
 
  if(currentmillis - lastflashdelay > (flashinterval+2150))
  {
    lastflashdelay = currentmillis;
    analogwrite(flashled, 0);
  }
}


void loop()
{
button1state = digitalread(button1pin);
    if (button1state == high)   
    {
            flash();
    }
}

the current implementation consists of flash() function call repeatedly make led fade , down.

at moment, call flash() while input high release button flashing stop, leaving led in whatever state in when released button.

if want have led complete flash cycle , start next cycle if/when button held down again, achieve taking stuff digitalread/button1state out of loop() , call flash() unconditionally. @ end of flash() detect (flashinterval+2150) has elapsed, instead of setting lastflashdelay = currentmillis; start next flash cycle unconditionally if digitalread() indicated button held down.


Arduino Forum > Using Arduino > Programming Questions > I need a little help / guidance with running a function off a button press.


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support