Author Topic: Button speed response Adruino vs. PIC  (Read 2591 times)

chicago_mike

Button speed response Adruino vs. PIC
« on: June 30, 2015, 07:31:50 PM »
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

  • more
  • Poster2
  • *****
  • Posts: 5577
  • Total likes: 753
  • Mike Parker (aka. Guitar Mike, Parks, etc)
Re: Button speed response Adruino vs. PIC
« Reply #1 on: June 30, 2015, 07:39:56 PM »
Can you post your code?
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

Re: Button speed response Adruino vs. PIC
« Reply #2 on: June 30, 2015, 07:53:10 PM »
Soon as I get to my tower. :)

chicago_mike

Re: Button speed response Adruino vs. PIC
« Reply #3 on: June 30, 2015, 08:05:19 PM »
#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

Re: Button speed response Adruino vs. PIC
« Reply #4 on: July 01, 2015, 01:53:12 AM »
@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

Re: Button speed response Adruino vs. PIC
« Reply #5 on: July 01, 2015, 05:46:58 AM »
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

  • more
  • Poster2
  • *****
  • Posts: 5577
  • Total likes: 753
  • Mike Parker (aka. Guitar Mike, Parks, etc)
Re: Button speed response Adruino vs. PIC
« Reply #6 on: July 01, 2015, 07:25:20 AM »
Looks like maybe both replies have info for you....what Slacker says, and then, if you get weird results, you might need to debounce....
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

Re: Button speed response Adruino vs. PIC
« Reply #7 on: July 01, 2015, 04:31:17 PM »
Thanks guys!! Info to chew on.  :)

GuitarPhil

Re: Button speed response Adruino vs. PIC
« Reply #8 on: July 05, 2015, 07:06:28 PM »
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.