switch LED function after user stops interacting


hi, i've programmed interactive 'game' user controls 3 potentiometers try match color of led master led. user presses button check whether color right, , if is, master led chooses new color. i'd set timer when player has stopped guessing (pressing button) after 1 minute, of leds revert glowing animation. pressing button start game again.

i've done research on millis(); , using interval timer, i'm having trouble wrapping head around 'resetting' timer when button pressed.

here snippet of code working try , increment timer:

code: [select]

void loop() {

  unsigned long currentmillis = millis();

 if (state == 0)//idle animation{ 
 
  idlecolor();

if(state == 2) //initiate random color{
    r = random(0,255);
    g = random(0,255);
    b = random(0,255);
    simon.setpixelcolor(0, r,g,b);
    simon.show();
    state = 1;
}
 else if (state == 1)//mode user can pick colors potentiometers aka play mode{   
   matcher.setpixelcolor(0, redknob, greenknob, blueknob);
   matcher2.setpixelcolor(0, redknob, greenknob, blueknob);
   matcher3.setpixelcolor(0, redknob, greenknob, blueknob);//controls color of matcher
   
   matcher.show();
   matcher2.show();
   matcher3.show();
   
}
 if(state == 1)// long they're in play mode, initiate idle timer{
 if ((unsigned long)(currentmillis - previousmillis) >= interval){
  state = 0;
  previousmillis = millis();
 }
 }


 
 if(button == high && oldbutton == low) //statement check pushbutton triggered press, rather press-high release-low
 {
 
 
  if (state == 0)//if it's in glowing animation mode
  {
    state = 2;
  }
 


as stands, once press button, initiate play mode every 60 seconds passes reset idle animation regardless if user pressing button. i'm looking guidance how implement resetting 60 second timer every time button pressed! know can't go ahead , reset amount of time passed in millis(); i'm wondering of work arounds.

thanks!

it easier if format code nicely using tool in ide , post of it.

in principle, if want reset timer, prevent mode being forced idle in case, while user active: 

code: [select]
previousmillis = millis() ;

maybe choice of variable name not here , userlastactiveatms might better , refreshed on user activity (button pressing etc.). test regularly if timeout expires ie
code: [select]

millis() - userlastactiveatms > timeoutintervalms , act accordingly.




Arduino Forum > Using Arduino > Programming Questions > switch LED function after user stops interacting


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???