Debouncing Button Code

I want to use a DC Motor and PWM it down and up by the button based on if the button was pressed before or not, the code is below but i’m having trouble getting the button to behave, I know I have to debounce it, but i’m not sure how or where to put it in my code. I’m fairly new to Arduino, so apologies if the code is not formatted right. Thanks!

[quote]

[color=#CC6600]const[/color] [color=#CC6600]int[/color] button = 13;
[color=#CC6600]const[/color] [color=#CC6600]int[/color] motor = 11;

[color=#CC6600]boolean[/color] lastButton = [color=#006699]LOW[/color];

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()

{
 [color=#CC6600]pinMode[/color] (button, [color=#006699]INPUT[/color]);
 [color=#CC6600]pinMode[/color] (motor, [color=#006699]OUTPUT[/color]);
 [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()

{
 [color=#CC6600]int[/color] buttonval = [color=#CC6600]digitalRead[/color](button);
 [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](buttonval);
 
 [color=#CC6600]if[/color] (buttonval == [color=#006699]HIGH[/color] && lastButton == [color=#006699]LOW[/color] )
 { 
   [color=#CC6600]for[/color]([color=#CC6600]int[/color] i = 0; i<255;i=i+5)
  {
    [color=#CC6600]analogWrite[/color](motor,i);
    [color=#CC6600]delay[/color](40);
  }

  lastButton = [color=#006699]HIGH[/color];
}
 

 
 [color=#CC6600]else[/color] [color=#CC6600]if[/color] (buttonval == [color=#006699]HIGH[/color] && lastButton == [color=#006699]HIGH[/color])
 {
   [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](lastButton);
   [color=#CC6600]for[/color]([color=#CC6600]int[/color] i = 255; i>=0; i=i-5)
  {
    [color=#CC6600]analogWrite[/color](motor,i);
    [color=#CC6600]delay[/color](40);
  }
 
 }
}

[/quote]

Modify your post. Highlight & copy your code, then paste it in between the code tags. Don't use the "copy for forum" feature, the code becomes unreadable with all the color & italics tags.

Sorry about that, here:

const int button = 13;
const int motor = 11;

boolean lastButton = LOW;

void setup()

{
 pinMode (button, INPUT);
 pinMode (motor, OUTPUT);
 Serial.begin(9600);
}

void loop()

{
 int buttonval = digitalRead(button);
 Serial.println(buttonval);
 
 if (buttonval == HIGH && lastButton == LOW )
 { 
   for(int i = 0; i<255;i=i+5)
  {
    analogWrite(motor,i);
    delay(40);
  }

  lastButton = HIGH;
}
 

 
 else if (buttonval == HIGH && lastButton == HIGH)
 {
   Serial.println(lastButton);
   for(int i = 255; i>=0; i=i-5)
  {
    analogWrite(motor,i);
    delay(40);
  }
 
 }
}

Simplest to use code already written in a form that is very easy to use - i.e., a library that does the hard work for you:

https://github.com/majenkotech/DebouncedInput

I read a C listing in the last couple of days from a link posted around here on debouncing topic. It is not arduino ready and needs to be integrated with a fixed timer interrupt say from the MStimer2 library or equivalent.

It’s well commented and explains the advantages of using a integration method rather then the usually simple timed sampling delay we normally seen used on arduino sketches and libraries. Would be nice to see it converted to a general purpose library if someone feels up to the challenge.

Lefty

/******************************************************************************
debounce.c
written by Kenneth A. Kuhn
version 1.00

This is an algorithm that debounces or removes random or spurious
transistions of a digital signal read as an input by a computer.  This is
particularly applicable when the input is from a mechanical contact.  An
integrator is used to perform a time hysterisis so that the signal must
persistantly be in a logical state (0 or 1) in order for the output to change
to that state.  Random transitions of the input will not affect the output
except in the rare case where statistical clustering is longer than the
specified integration time.

The following example illustrates how this algorithm works.  The sequence 
labeled, real signal, represents the real intended signal with no noise.  The 
sequence labeled, corrupted, has significant random transitions added to the 
real signal.  The sequence labled, integrator, represents the algorithm 
integrator which is constrained to be between 0 and 3.  The sequence labeled, 
output, only makes a transition when the integrator reaches either 0 or 3.  
Note that the output signal lags the input signal by the integration time but 
is free of spurious transitions.
 
real signal 0000111111110000000111111100000000011111111110000000000111111100000
corrupted   0100111011011001000011011010001001011100101111000100010111011100010
integrator  0100123233233212100012123232101001012321212333210100010123233321010
output      0000001111111111100000001111100000000111111111110000000001111111000

I have been using this algorithm for years and I show it here as a code
fragment in C.  The algorithm has been around for many years but does not seem
to be widely known.  Once in a rare while it is published in a tech note.  It 
is notable that the algorithm uses integration as opposed to edge logic 
(differentiation).  It is the integration that makes this algorithm so robust 
in the presence of noise.
******************************************************************************/

/* The following parameters tune the algorithm to fit the particular
application.  The example numbers are for a case where a computer samples a
mechanical contact 10 times a second and a half-second integration time is
used to remove bounce.  Note: DEBOUNCE_TIME is in seconds and SAMPLE_FREQUENCY
is in Hertz */

#define DEBOUNCE_TIME		0.3
#define SAMPLE_FREQUENCY	10
#define MAXIMUM			(DEBOUNCE_TIME * SAMPLE_FREQUENCY)

/* These are the variables used */
unsigned int input;       /* 0 or 1 depending on the input signal */
unsigned int integrator;  /* Will range from 0 to the specified MAXIMUM */
unsigned int output;      /* Cleaned-up version of the input signal */


/* Step 1: Update the integrator based on the input signal.  Note that the 
integrator follows the input, decreasing or increasing towards the limits as 
determined by the input state (0 or 1). */

  if (input == 0)
    {
    if (integrator > 0)
      integrator--;
    }
  else if (integrator < MAXIMUM)
    integrator++;

/* Step 2: Update the output state based on the integrator.  Note that the
output will only change states if the integrator has reached a limit, either
0 or MAXIMUM. */

  if (integrator == 0)
    output = 0;
  else if (integrator >= MAXIMUM)
    {
    output = 1;
    integrator = MAXIMUM;  /* defensive code if integrator got corrupted */
    }

/********************************************************* End of debounce.c */

Thanks! I've found a library to use called the Bounce Library, I've integrated it, and it works!