stickyBUG Code

The following sample code requires the ioBUG library.

Source Code

// import libraries...
#include <Wire.h>
#include <ioBUG.h>

// ioBUG object...
ioBUG bug = ioBUG();

// set an Led to either on or off, depending on btnState value - for example
// btnState = %11111110, btnIndex = 1 -> switch off Led(1)
// btnState = %11111110, btnIndex = 0 -> switch on Led(0)
void setLed(byte btnState, byte btnIndex) {
   if ((btnState & bit(btnIndex)) == 0) {
      bug.setValue(btnIndex,255);
   }
   else {
      bug.setValue(btnIndex,0);
   }
}

// program setup...
void setup() {
  bug.begin(IO_BUG_ADDRESS); 
}

// main program loop...
void loop() {

   // if a button has been pressed, then switch on a corresponding
   // Led - more than one button can be held down, which will
   // switch on more than one Led...   
   if (bug.isBtnPressed()) {
      byte btnState = bug.getButtonState();
      for (byte index = 0; index < 8; index++) {
         setLed(btnState, index);  
      }     
   } 
}