2 Grove Buttons executing same code

Hi there

I have a imp001.
I have 2 Grove buttons on my device, both connected to the 2 i2c Grove ports, however it appears that when I press either button the same code (function buttonPress()) is executed. See code below:

button <- hardware.pin8;

function buttonPress() {
  if (button.read() == 1) {
    server.log("Button 1 pressed");
  } else {
    server.log("Button 1 released");

button.configure(DIGITAL_IN_PULLUP, buttonPress);

button2 <- hardware.pin9;
function buttonPress2() {
  if (button2.read() == 1) {
    server.log("Button 2 pressed");
  } else {
    server.log("Button 2 released");

button2.configure(DIGITAL_IN_PULLUP, buttonPress2);

Can anyone please tell me what I am doing wrong here?

Appreciate in advance.

Is this the board you have?

Are your buttons just plain momentary buttons that you want to connect to digital input pins? You said ‘2 Grove buttons’ … not sure what that means.

The Seeed Grove switch uses the yellow pin which in both impExplorer Kit Grove I2C connectors (because they’re both wired to the same imp pins) is pin 8. You can’t read pin 9 using the Seeed Grove switch, because the switch is not connected to the white wire/pin 9.

If you want to use the two I2C connectors for two separate switches, you’ve got the right code, but you need to make sure that the switch plugged into the second connector uses the white wire as that is what is connected to pin 9.

More useful info here:

Thank you smittytone - should have read the documentation, appreciate mate

FYI, here is some sample code for using Grove-connected devices. It’s written for the imp006 Breakout Kit but is readily portable to the imp001 with minor changes (bus and pin names).

This topic was automatically closed after 60 days. New replies are no longer allowed.