latching switches


I'm developing a system using Arduino Uno and MCP 23017 expander chips. At the moment I have the Uno connected to 23017 with 8 momentary switches at the input and 8 led's at the output.
With my current sketch (attached) I can only get the led's to light when holding the buttons down. I need the buttons to latch the led's on and then off again with a second push of the switch.

The project will eventually need 64 switches…ideally 72 but I understand you can only cascade x 8 23017 chips. Finally I want be able to store any combination of switch states into memory ..EEprom ???

A save and recall button will be needed to retrieve the memory …also up down buttons for memory store selection…..number displayed on a 2 digit led display.

Anyone out there able to say if thats all possible…
actually at this stage it would be great to know how to latch those damn led's…..been trying but don't really know what i'm doing :slight_smile:

*#include "Wire.h"    // Use the Wire Library for I2C
byte a=0;            // Variable to store the Byte in

void setup()
  Wire.begin();                // wake up I2C bus
  Wire.beginTransmission(0x20); // Connect to chip
  Wire.write((byte)0x01);      // Select Bank B
  Wire.write((byte)0x00);      // Set all of bank B to outputs
  Wire.endTransmission();      // Close connection

void loop()
  // Read the inputs of bank A
  Wire.beginTransmission(0x20); // Connect to chip
  Wire.write(0x12);            // Set Memory Pointer to Bank A
  Wire.endTransmission();      // Close connection
  Wire.requestFrom(0x20, 1);    // Request one Byte;                // Put the Byte into variable 'a'

// Write the Byte to Bank B
  Wire.beginTransmission(0x20);  // Connect to chip
  Wire.write(0x13);              // Set Memory Pointer to Bank B
  Wire.write(a);                // Write the Byte
  Wire.endTransmission();        // Close connection

delay(100);                    // Small delay to debounce switch

All possible but a lot quicker and more expandable using some shift registers without any limit to the number of LEDs you can switch (within reason).

Drop me a PM for my fees :wink:

Cheers Pete.