-1

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();
}
4
  • Note that analogRead(3) is a very poor entropy source. Timing the button press with micros() would be way better. – Edgar Bonet Mar 10 '20 at 21:21
  • Why is it a bad source? From the arduino reference for the random() function: "If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin." Edit: Nvm I understand why it's bad. – BobaJFET Mar 10 '20 at 21:37
  • your code handles a button press, but it does not do anything with the button release ... do not code for a button pressed state because that state lasts as long as the button is held ... code for transition between button released and button pressed because the transition happens only once and can only repeat if you release the button – jsotola Mar 11 '20 at 2:17
  • @jsotola Yes I am currently reading on how to do that. I would love to be able to have the function finish it's code completely before going back to the main loop. Since writing this post, the code now has a for loop which is meant to display 29 random numbers back to back, then display the 30th number on the display. I can't seem to get it. What type of example should I look for? – BobaJFET Mar 11 '20 at 4:02
1

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.

1

The problem is that your rollD20 function is only called when the button is pressed. So once you release the button, it never goes back to that routine to finish. You need to separate the calling of the function from the detecting of the button.

bool rollingDice = false;

void loop() {


if(digitalRead(A2) == HIGH){
  rollingDice = true;
}
if(rollingDice){
  rollD20();
}
else{
  sevseg.setChars("");
  sevseg.refreshDisplay();
}

And then in your rollD20 method you need to set that back to false when it is time to stop:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.