RGB Shield Issue
Moderators: adafruit_support_bill, adafruit

Re: RGB Shield Issue

by adafruit_support_rick on Fri Mar 01, 2013 4:14 pm

Ohhh! You want the buttons to toggle! Your original code treated them as momentaries, so I went with that.

First, yes, readButtons returns the current state of all buttons. It simply reads the current state of the MCP23017 and pulls out the button bits. This is typical behavior for this sort of thing. Usually, you would maintain a local copy of the last state read, and perform an exclusive-or with the value returned by readButtons to determine which buttons, if any, changed state. Then you'd react to the new state of the changed buttons.

For toggles, you can simply read the state of your output pin. If it reads as HIGH, then your relay is enabled, and you can turn it off. And vice-versa.
Code: Select all | TOGGLE FULL SIZE
  if (digitalRead(buttonPin) == HIGH)  // button is pressed
  {   
      digitalWrite(ledPin, !digitalRead(ledPin));        // toggle current output state - if HIGH, write LOW. if LOW, write HIGH
  }


Put a delay(250); at the bottom of the loop so you don't cycle through too quickly
Last edited by adafruit_support_rick on Fri Mar 01, 2013 4:18 pm, edited 1 time in total.
Reason: changed '~' to '!'. '~' doesn't work...oops...

adafruit_support_rick
 
Posts: 12768
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: RGB Shield Issue

by Bigjeep093 on Fri Mar 01, 2013 6:56 pm

that works perfectly.

thanks!

and I have to ask. is there a way to do that WITH the buttons that are on the Adafruit RGB LCD Shield? The ones that are predefined? UP, DOWN, LEFT, RIGHT, SELECT?

that would free up a few pins.
Bigjeep093
 
Posts: 9
Joined: Wed Feb 20, 2013 11:13 pm

Re: RGB Shield Issue

by adafruit_support_rick on Fri Mar 01, 2013 11:59 pm

Sure - I think you already had it right before:
Code: Select all | TOGGLE FULL SIZE
  uint8_t buttons = lcd.readButtons();

  if (buttons) {  //if any buttons are pressed

    if (buttons & BUTTON_LEFT)    // Button LEFT is PUMP 1, relay 2
    {   
        digitalWrite(leftRelayPin, !digitalRead(leftRelayPin)); // toggle relay
    }

    …etc …

  }

adafruit_support_rick
 
Posts: 12768
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY