user61969: I'm coding an electronic dice roller for a project. At the press of a push button (A2), the code must display a random number to the two digit seven segment display for 20 seconds, then display blank. 
Currently, my code will display a random number every t seconds while the button is continuously held. I would like the user to be able to press the button and release to let the code do it's thing. 
What type of examples should I look at to achieve this? I'm quite a beginner at arduino code. 
    #include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment object

int displayPeriod = 1000; //button press delay
unsigned long time_now = 0;

long randNumber;

void setup() {

  pinMode(A2,INPUT);
 randomSeed(analogRead(3));

  byte numDigits = 2;
  byte digitPins[] = {2, 3};
  byte segmentPins[] = {4,5,6,7,8,9,10};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected. Then, you only need to specify 7 segmentPins[]

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);

}

void loop() {


if(digitalRead(A2) == HIGH){

  rollD20();

}

else{

  sevseg.setChars("");
  sevseg.refreshDisplay();
}







}

void rollD20(){

if(millis() >= time_now + displayPeriod){
        time_now += displayPeriod;
        time_now = millis();
        randNumber = random(21); 
        }
        sevseg.setNumber(randNumber);
  sevseg.refreshDisplay();
  }

void testInput(){
sevseg.setNumber(0);
sevseg.refreshDisplay();
}


user63711: I would recommend to not reinvent the wheel, and proper button management may not be so trivial. I would suggest this library to address your problem.

61969: <p>I'm coding an electronic dice roller for a project. At the press of a push button (A2), the code must display a random number to the two digit seven segment display for 20 seconds, then display blank. </p>

<p>Currently, my code will display a random number every t seconds while the button is continuously held. I would like the user to be able to press the button and release to let the code do it's thing. </p>

<p>What type of examples should I look at to achieve this? I'm quite a beginner at arduino code. </p>

<pre><code>    #include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment object

int displayPeriod = 1000; //button press delay
unsigned long time_now = 0;

long randNumber;

void setup() {

  pinMode(A2,INPUT);
 randomSeed(analogRead(3));

  byte numDigits = 2;
  byte digitPins[] = {2, 3};
  byte segmentPins[] = {4,5,6,7,8,9,10};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected. Then, you only need to specify 7 segmentPins[]

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);

}

void loop() {


if(digitalRead(A2) == HIGH){

  rollD20();

}

else{

  sevseg.setChars("");
  sevseg.refreshDisplay();
}







}

void rollD20(){

if(millis() &gt;= time_now + displayPeriod){
        time_now += displayPeriod;
        time_now = millis();
        randNumber = random(21); 
        }
        sevseg.setNumber(randNumber);
  sevseg.refreshDisplay();
  }

void testInput(){
sevseg.setNumber(0);
sevseg.refreshDisplay();
}
</code></pre>
 63711: <p>I would recommend to not reinvent the wheel, and proper button management may not be so trivial. I would suggest <a href="https://github.com/JChristensen/JC_Button" rel="nofollow noreferrer">this library</a> to address your problem.</p>
