Button example - Help

I have been tring to understand the Button behaviour and wonder if i am right:

This is my class:

// Buttton Class
class Button extends IoExpander
{
// IO Pin assignments
pinButton = 0;
state = 0;

constructor(port, address, pinButton)
{
    server.log(format("constructor"));
    base.constructor(port, address);
    
    // enable pin as active
    setPin(pinButton, 0);
    
    // set button IRQ
    setPullUp(pinButton, 1);
    setIrqMask(pinButton, 1);
    setIrqEdges(pinButton, 1, 1);  // Set for rising and falling        
}

// Read button
function readState()
{
    state = getPin(pinButton);

    //server.log(format("debug %d", state));
    return state;
}

}

Then i initialize and read the button every sec.
If i press the button for more than a sec, i get chaotic behaviour. Like several 1 and 0s from the read.

How should i get process the button read output?

thanks
Patrick

If you’re polling the button, then you shouldn’t be enabling the interrupt on it (the “set button IRQ” bit).

You may be seeing switch bounce if you see some instability around either a press or a release; you need to wait until it settles, generally. Can you post all your code?

Thanks Hugo for the quick reply!

I am using the IoExpander class as a base for my Buttton class.

Then I initialise and use Button like this:

`
local button1 = Button(I2C_89, 0x3E, 1);
local button2 = Button(I2C_89, 0x3E, 2);
local state1 = 0;
local state2 = 0;

// Test
function change()
{
// Schedule the next change
imp.wakeup(0.5, change);

// Read the button
state1 = button1.readState();
state2 = button2.readState();

if (state1==1 || state2==1){
    server.log(format("state - button 1 is %d - button 2 is %d", state1, state2));
}

}

// Register with the server
imp.configure(“Test Button”, [], []);

change();
`

I have commented the IRQ code, and kept the line for the PullUp like this:
setPullUp(pinButton, 1);

This is what i get, if i output all read, and press on one button:

Sunday, September 16, 2012 11:07:12: state - button 1 is 0 - button 2 is 0 Sunday, September 16, 2012 11:07:13: state - button 1 is 0 - button 2 is 0 Sunday, September 16, 2012 11:07:13: state - button 1 is 1 - button 2 is 1 Sunday, September 16, 2012 11:07:13: state - button 1 is 1 - button 2 is 1 Sunday, September 16, 2012 11:07:14: state - button 1 is 0 - button 2 is 0

I know i need to De-bounce the button, but why do I read 1s on both buttons, if i press one?

Is my initialisation is good (GPIO) for the button?
setPin(pinButton, 0); setPullUp(pinButton, 1); // not sure is it for

You have an example of code for polling the button state?

I think I need to see the button class to work this out. Could you put it on pastebin.com and attach a link?

Hugo,

I have placed the complete code on http://pastebin.com/ETHH1WN9
I am looking forward to understand my mistake.

Should I use IRQ instead of polling for a button?

Thanks for your time
Patrick

Ok, so I found two issues:

  1. The buttons are on GPIO 0 and 1, not 1 and 2
  2. You were shadowing the pinButton value in your constructor, hence it was only ever looking at GPIO 0.

My modified constructor looks like this:

` constructor(port, address, pb)
{
server.log(format(“constructor”));
base.constructor(port, address);

    // Save pin
    pinButton = pb;
    
    // enable pin as input with pull-up
    setDir(pinButton, 0);
    setPullUp(pinButton, 1);
    
    // set button IRQ
    //setIrqMask(pinButton, 1);
    //setIrqEdges(pinButton, 1, 1);  // Set for rising and falling        
}

`

…even if the constructor parameter name is the same as a class variable, it won’t magically assign - the parameters to the constructor are local to that function, not the class.

Now it seems to work for me. Here’s my complete code: http://pastebin.com/kX3EHe4z

Thanks Hugo, basic cut and paste. I should have catch this.

It seam i was missing the setDir to set the input too.

Regards,
Patrick