RGB Shield Issue

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: RGB Shield Issue

Post by adafruit_support_rick »

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

  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...

Bigjeep093
 
Posts: 9
Joined: Wed Feb 20, 2013 11:13 pm

Re: RGB Shield Issue

Post by Bigjeep093 »

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.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: RGB Shield Issue

Post by adafruit_support_rick »

Sure - I think you already had it right before:

Code: Select all

  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 …

  }

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”