Problem with push button for bingo machine (maybe debounce)

Hi,

I am new to arduino programming but I am trying to use this for a college project. My task is to produce a bingo machine, that randomly selects a number between 1 and 90 at the press of a button. When the device is turned on, it must start by displaying a 00.

I have spent some time trying to get my code working. I have successfully got the machine to generate a random 2 digit number. But found that when I saw the number on the serial monitor, after pressing the button, it would produce 3 or 4 numbers. I then researched switch debouncing, which I then followed some code I found online. The problem I have now, is that when I simulate my code, it constantly produces random numbers supper fast, as if the button is being constantly pressed.

I am hoping this will be a quick fix and I have made a silly mistake. Like I say I am new to programming.

Bellow is the code which I currently have

//include the libary code
#include<LiquidCrystal.h>

//initialize the libary with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//constants wont change.
//set the pin numbers
const int button1 = 7; //declare button1 (new number)
unsigned int memory[90]; //Declare storage array 1 - 90

//variables will change
unsigned int randomInt=0;//Declare and initialise variables
unsigned int memoryPointer=0; //points to memory storage element
int randomIntState = LOW; //The current state of the output
int buttonState; //The current reading from the button
int lastButtonState = LOW; //The previous reading from the button

unsigned long lastDebounceTime = 0; //The last time the button was pressed
unsigned long debounceDelay = 50; //The debounce time

void setup() {
Serial.begin(9600);// 9600 Baud
lcd.begin(16, 2); //Setting LCD screen size
pinMode(button1, INPUT); //Setting pin 7 as input
randomSeed(analogRead(0)); //makes random number different each time

lcd.print(“Bingo”); //Displays bingo on startup
lcd.setCursor(0, 1);//move cursor to second line
lcd.print(“00”); //Displays 00 on startup
} //End of setup

void loop(){
//read the state of the switch, declare as a variable
int reading = digitalRead(button1);
//check if the button has been pressed
//If the switch changed
if(reading != lastButtonState){
//reset debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay){
//if reading is true longer than debounce delay, reading is true
if(reading !=buttonState){
buttonState = reading;
//only produce random number if new button state is high
if (buttonState == HIGH){
randomIntState = !randomIntState;
}
}

//produce random number
while(memoryPointer<=90)
{
randomInt = random(1, 91);
memory[memoryPointer]=randomInt;
if(randomInt == memoryPointer)
{randomInt = random(1, 91);}
Serial.print(memory[memoryPointer]);
lcd.print(memory[memoryPointer]);
memoryPointer=memoryPointer+1;//increment the next memory location
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Number is:”);
lcd.setCursor(0,1);
lcd.print(randomInt);
Serial.print (randomInt);
}

lastButtonState = reading;
}
}

I hope someone can point me in the right direction please

Thank You

Given your usage scenario I’d be a bit more “brute force” and just wait a bit (20 ms or so) to get rid of the bouncing when you detect the button press.

Then you need to ensure that you wait until the button is released before capturing a new press on the button.

You don’t want to keep generating numbers while the button is still pressed (so get rid of that while)

careful on thisunsigned int memory[90]; //Declare storage array [color=red]0 - 89 not 1 - 90[/color]

You might want to build in some logic so that not the same number is presented twice…

Assuming your button is properly wired with an external pulldown resistor, the structure of the code could be something like this:

//include the libary code
#include<LiquidCrystal.h>

//initialize the libary with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//constants wont change.
//set the pin numbers
const byte button1 = 7; //declare button1 (new number)

//variables will change
int lastButtonState = LOW; //The previous reading from the button

void setup() {
  pinMode(button1, INPUT); //Setting pin 7 as input
  lcd.begin(16, 2); //Setting LCD screen size

  randomSeed(analogRead(A0)); //makes random number different each time

  lcd.setCursor(0, 0);//move cursor to start of first line
  lcd.print(F("Bingo")); //Displays bingo on startup
  delay(2000); // display for a short while
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("Number is:"));
  lcd.setCursor(0, 1);//move cursor to second line
  lcd.print(F("00")); //Displays 00 on startup
} //End of setup


void loop() {
  unsigned int randomInt;

  //read the state of the switch, declare as a variable
  int reading = digitalRead(button1);

  //check if the button has been pressed
  if ((reading == HIGH) && (reading != lastButtonState)) {
    // this is a new press

    // here you need to generate a new number randomly that has not been used before
    randomInt = 1; // <-- OF COURSE THIS IS TO BE CHANGED!

    // Once you have the number, display it
    lcd.setCursor(0, 1);
    lcd.print(randomInt);
    lcd.print("  "); // to erase what was there just in case
    delay(20); // poor man's debounce, OK here as nothing is time critical
  }
  lastButtonState = reading;
}

(would simplify hardware to use the builtin pullup and invest the button push detection logic in that case of course)

Please edit your post to have code tags. See How to sued this forum

And uhm,
[code]
pinMode(button1, INPUT); //Setting pin 7 as input
//[......]
if (buttonState == HIGH){

Is you're button connected to Vcc and a pin and does it have a pull down resistor?

Easiest way is to connect the button between GND and the pin and turn on the internal pull up resistors (pinMode(pin, INPUT_PULLUP). But note, a pressed button will then read LOW.

And if you want to make your life easy, grab a library for the button as well. You do it for the LCD and even for reading/setting the pins, why don't use a library to handle the debounce? I like to use Bounce2 for that.

PS Why is the button pin called "button*1"? If you want multiple => arrays. And you can help yourself by using clearer variable names. So "ButtonPin*"