Button speed response Adruino vs. PIC

Started by chicago_mike, June 30, 2015, 07:31:50 PM

Previous topic - Next topic

chicago_mike

I guess what I am pondering out loud here, is this.

I have some simple code, that detects what button is pressed from an array.  However, I have to release the button before it reads the state.

It's Arduino. Which later on I will port the code over to PIC, my brother works for PIC so I can pick his brain at time. But I am wondering, if theres a cheat either for pic or arduino, that I don't have to wait for the button to be released for the aver to detect the button state change?

GibsonGM

  • SUPPORTER
MXR Dist +, TS9/808, Easyvibe, Big Muff Pi, Blues Breaker, Guv'nor.  MOSFace, MOS Boost,  BJT boosts - LPB-2, buffers, Phuncgnosis, FF, Orange Sunshine & others, Bazz Fuss, Tonemender, Little Gem, Orange Squeezer, Ruby Tuby, filters, octaves, trems...

chicago_mike


chicago_mike

#define LED_BUTTON_PIN_COUNT 8

byte g_led_pins[LED_BUTTON_PIN_COUNT] = { 22, 23, 24, 25, 26, 27, 28, 29};
byte g_button_pins[LED_BUTTON_PIN_COUNT] = { 40, 41, 42, 43, 44,45, 46, 47  };
bool g_button_state[LED_BUTTON_PIN_COUNT];
bool g_last_button_state[LED_BUTTON_PIN_COUNT];

void setup ()
{
 
 
   for( int i=0; i<LED_BUTTON_PIN_COUNT; i++ )
  {
    pinMode( g_button_pins, INPUT );
    pinMode( g_led_pins, OUTPUT );
    g_button_state = false;
    g_last_button_state = false;
  }
}

void loop()
{
  for( int i=0; i<LED_BUTTON_PIN_COUNT; i++ )
  {
    g_button_state = digitalRead( g_button_pins );

    // see if state changed, and new state is LOW
    if( g_button_state != g_last_button_state && g_button_state == LOW)
      {
      digitalWrite( g_led_pins, ! digitalRead (g_led_pins));  // <--- toggle pin
      delay (20);  // debounce
      }  // end of if state changed
    g_last_button_state = g_button_state;
  }  // end of for


 
       
}

gtudoran

@chicago_mike: that technique is called de-bouncing and is used to detect if a signal is a mechanical bounce from the switch or a true command signal, that is why is necessary to wait for the button to be released. You can use electronic de-bounce circuit with a Schmidt trigger but that would complicate the things a little bit.
So all in all, to have a reliable operation a de-bounce routine is needed.
Also there are many arduino library that can be used for detecting buttons and they use better ways to de-bounce, also if you are going to need a non blocking read of the buttons, you will need to use interrupts vectors to pause the program when a certain even occurs (in this particular case a push of a button) - in your case the program will be blocked until the button is released and the program will be in a delay loop as long as the button is pressed.

Regards,
Gabriel

slacker

I don't see anything there that means the state doesn't change until you release the button, the LED toggles when the button goes from high to low, so if you wire your buttons to be high when not pressed and low when pressed the LED will toggle as soon as you press the button. If your buttons are wired the other way round then the LED will toggle when the button is released.

GibsonGM

Looks like maybe both replies have info for you....what Slacker says, and then, if you get weird results, you might need to debounce....
  • SUPPORTER
MXR Dist +, TS9/808, Easyvibe, Big Muff Pi, Blues Breaker, Guv'nor.  MOSFace, MOS Boost,  BJT boosts - LPB-2, buffers, Phuncgnosis, FF, Orange Sunshine & others, Bazz Fuss, Tonemender, Little Gem, Orange Squeezer, Ruby Tuby, filters, octaves, trems...

chicago_mike


GuitarPhil

I found a great button library by Jack Christensen:

Extract from his ReadMe file:

ReadMe file for Arduino Button Library v1.0
https://github.com/JChristensen/Button
Jack Christensen Mar 2012


Does all your debouncing, can use active high or active low buttons, handles long presses and other neat stuff.

Phil.