#include const int pca9555Address = 0x20; // test output value int v = 3; // direction of the LED animation int directionLeft = 1; // LED inversion int inverse = 0; // write a 16 bit value to a register pair // write low byte of value to register reg, // and high byte of value to register reg+1 void pca9555WriteRegisterPair(int reg, int value) { Wire.beginTransmission(pca9555Address); Wire.send(reg); Wire.send(value & 0xff); Wire.send((value >> 8) & 0xff); Wire.endTransmission(); } // read a 16 bit value from a register pair int pca9555ReadRegisterPair(int reg) { Wire.beginTransmission(pca9555Address); Wire.send(reg); Wire.endTransmission(); Wire.beginTransmission(pca9555Address); Wire.requestFrom(pca9555Address, 2); int data = 0; if (Wire.available()) data = Wire.receive(); if (Wire.available()) data |= Wire.receive() << 8; Wire.endTransmission(); return data; } // set IO ports to input, if the corresponding direction bit is 1, // otherwise set it to output void pca9555SetInputDirection(int direction) { pca9555WriteRegisterPair(6, direction); } // set the IO port outputs void pca9555SetOutput(int value) { pca9555WriteRegisterPair(2, value); } // read the IO port inputs int pca9555GetInput() { return pca9555ReadRegisterPair(0); } void setup() { Wire.begin(); pca9555SetInputDirection(1 << 15); } void loop() { // if button is pressed, invert output if (pca9555GetInput() & 0x8000) { inverse = 0; } else { inverse = 0xffff; } // set current output pca9555SetOutput(v ^ inverse); // animate LED position if (directionLeft) { v <<= 1; } else { v >>= 1; } if (v == 0x6000) { directionLeft = 0; } if (v == 3) { directionLeft = 1; } // wait 100 ms for next animation step delay(100); }