Registering Button Debounce

Good Afternoon all.

Ive created an project based on the Timer1. I do have a poblem where the code for some reason lag the processor and the response to the input 12 seems to take few good seconds to update the state. Unfortunetly i never worked with the PWM library therefore im not so sure if this is cause by my hardware or maybe someone else is able to spot an error in my code please?

MC-2100 Treadmill Motor Controller Interface
 Lathe Motor Controller via PWM
 Seven Segment Tachometer
 ON/OFF Toggle
 
 Joe Schoolcraft
 Brian Schoolcraft
 May 2013
 https://sonsofinvention.wordpress.com/
*/

#include  "TimerOne.h" //http://playground.arduino.cc/code/timer1
 
#define POT_READ A0 //Wiper of pot connected as voltage divider (Speed Command Input)
#define SPEED_SENSE 2 //connected to reed switch - magnet closes switch, pulls to ground (Spindle Speed Input - 2 pulses per rev)
#define PWM_OUT 9 //Connected to blue wire of MC2100 (50ms period PWM out)
#define PWM_CYCLE 50.0 //Output Signal PWM Period (50ms)
#define POT_DIF 4 //Change detection threshold on pot
#define MAX_DUTY 869 //Max Duty Cycle expected by MC-2100 (85% of 1023)
#define MIN_DUTY 0 //Min Duty Cycle expected by MC-2100 (0% of 1023)

 
 
int potTemp;
int potValue;
int lastPotValue;
int potCheck;
int speedLevel;
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW; 
const int ON_OFF = 13; //on off switch input
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50; 

 

byte onOffState = 0;

 

 
void setup()
{
  Serial.begin(9600);
 pinMode(POT_READ, INPUT);
 pinMode(PWM_OUT, OUTPUT);
 
 
pinMode(ON_OFF, INPUT_PULLUP); //Enable internal pullup resistor to simplify external circuit
 
 
Timer1.initialize(PWM_CYCLE*1000); //Set pin 9 and 10 period to 50 ms
 Timer1.pwm(PWM_OUT, 0); //Start PWM at 0% duty cycle
}
 
void loop()
{
  int reading = digitalRead(ON_OFF);
    if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
    if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        onOffState = !onOffState;
      }
    }
  }
  digitalWrite(ON_OFF, onOffState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
 //Read and condition pot value
 potTemp = analogRead(POT_READ);
 potCheck = abs(potTemp - potValue);
 if(potCheck >= POT_DIF) { //Only accept new value if it's far enough from the current accepted value
 potValue = potTemp;
 }
 
 speedLevel = map(potValue,0,1023,0,MAX_DUTY); //Convert Pot input to pwm level to send to MC-2100
 Serial.println(onOffState);

 if (onOffState == 0){ //Off
 Timer1.setPwmDuty(PWM_OUT, 0); //Shut down MC-2100
 }
 
if (onOffState == 1){ //ON
 Timer1.setPwmDuty(PWM_OUT, speedLevel); //Send speed command to MC-2100
 }
}

i just add a 10msec delay

but if if you're going to use a timer, it would prevent recognizing the next state change, not the reporting of the first

consider

    if ((millis() - lastDebounceTime) > debounceDelay) {
        int reading = digitalRead(ON_OFF);
        if (reading != buttonState) {
            buttonState = reading;
            if (buttonState == HIGH) {
                onOffState = !onOffState;
            }
        }
        lastDebounceTime = millis();
    }

Is the issue with the speed of response to the pwm change, or the state change between on/off?

Your code has a push button switch on pin13. What is on pin 12?
Because of the led, pin 13 is not a good choice for an input button.

Hi Guys

Thanks for your replay.

I`ve tried suggested option by @gcjr but result is the same.
I also moved the pin13 to pin 11 and the result is unfortunetly as per my description.

I think i see the problem.
I have latching switch, where the debonce is for the momentary switches right?
I noticed that i have to kind of switch it on and off twice for the state to change

Please clarify if the slow response is to the on off mode change or the pwn response to a pot change

@cattledog its to the on off change. Im just double checking what i did.

Looks also that i digitalRead and digitalWrite to the same pin...

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.