Rebounce Function Issue

Hi all! I have a question regarding the placement of the placement of the rebounce() function. I have some code that will print one random number per press of a button but I want it to output a number every say 500ms as long as the button is held down. I have looked at the bounce library page and can’t find much about how rebounce() works. Any help would be appreciated greatly. Thanks -ookid

#include <Bounce.h>

int rand1; // variable for random number
int lastbutton = 1; //state of button, was it pressed last loop?
Bounce buttonpress =  Bounce ( 12, 20 ); // initialize debouncing

void setup() {
  pinMode(12, INPUT);
  digitalWrite (12, HIGH); // using internal pull up resistor
  Serial.begin(9600);
}

void loop() {
  rand1 = random (1, 7); // generate random number and store it
  buttonpress.update ( ); // update button state
  int i =  buttonpress.read(); // store state as i
  if (i == 0) {
    if (lastbutton == 1) { //prevents multiple outputs per button press
      Serial.print (rand1);
      lastbutton = 0; prevents multiple outputs per button press
    }
  }
  else {
    lastbutton = 1; // prevents multiple outputs per button press
  }
}

Why do you think you need to use the rebounce() method?

You have explicitly added code, involving lastbutton, that ensures that only one output occurs per button press. If you want to output more than once per button press, use millis() to record when the last output occurred. Set that value to 0 whenever the button is released.

When the button is pressed, output a value if the last time is 0 or if now minus last time is greater than or equal whatever interval you want. Set the last time whenever you output a value.

I think the rebounce example from the bounce library does what you want.

If you just swap the LED toggling code for your random number printing.

#include <Bounce.h>

// As long as the button is held down, the LED will blink
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button

#define BUTTON 2
#define LED 13

// A variable to store the current LED state
int ledState = LOW;

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 ); 

void setup() {
  pinMode(BUTTON,INPUT);
  pinMode(LED,OUTPUT);
}

void loop() {
  
 // Update and monitor a change of input
  if ( bouncer.update() ) {
    
    // Get the state of the button
   int value = bouncer.read();
 
   // Toggle the LED if the button is held
   if ( value == HIGH) {
     // Make the button retrigger in 500 milliseconds
     bouncer.rebounce(500);
     if ( ledState == LOW ) {
       ledState = HIGH;
     } else {
       ledState = LOW;
     }
   } else {
     ledState = LOW;
   }
 
    digitalWrite(LED, ledState );
    
  }
 
 
 
}

Well, I figured using rebounce() would easier to implement but I coded out your method and I think I like it better. Thanks for the replies, here is my resulting code for those interested :smiley:

#include <Bounce.h>

int rand1;
Bounce buttonpress =  Bounce ( 12, 20 );
unsigned long time;
int duration;

void setup() {
  pinMode(12, INPUT);
  digitalWrite (12, HIGH);
  Serial.begin(9600);
}

void loop() {
  rand1 = random (1, 7);
  time = millis ();
  buttonpress.update ( );
  int i =  buttonpress.read();
  if (i == 0) {
    int val = (time - duration);
    if (duration == 0 || val >= 500)  {
      Serial.print (rand1);
      duration = time;
    }
    
  }
  if (i == 1) {
    duration = 0;
  }
}