Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Ideas for keeping track of the state of individual switches within a matrix?

  1. #1

    Ideas for keeping track of the state of individual switches within a matrix?

    I will have an 8x8 switch matrix that's connected to a 16 port I/O expander that's communicated to via I2C. Each switch has a diode attached to it so that when multiple switch closures occur simultaneously there isn't any ghosting. In total there will be 64 switches.

    Right now I have just a single uint64_t variable where each bit represents one of the switches. This gets updated once per loop.

    In my code I'll want to process events from these switches, but only once per closure and/or release.

    The "Bounce" library is basically exactly what I want ... but it's made for directly reading I/O ports rather than through an I2C device (or RAM variable).

    What's the best way to implement something like this?

    Would I need an array of 64 uint8_t variables to keep track of the state of every switch? That seems like the logical choice, but does consume a chunk of RAM where each would only be keeping track of a few states (around 5 or 6). I can't really think of any way around it though. So that's why I'm here

    RAM is there to be used though, so why not? I just hate wasting... don't know why. As an aside: Is there a minimum size of actual RAM used regardless of the type used?

    Thanks!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,467
    Quote Originally Posted by Geomancer View Post
    Would I need an array of 64 uint8_t variables to keep track of the state of every switch? That seems like the logical choice, but does consume a chunk of RAM where each would only be keeping track of a few states (around 5 or 6). I can't really think of any way around it though. So that's why I'm here

    RAM is there to be used though, so why not? I just hate wasting... don't know why.
    I'd go with the simplest way first, then try optimizing only after it's confirmed working, with a backup copy saved!


    Is there a minimum size of actual RAM used regardless of the type used?
    Yes, lots of stuff uses RAM by default. The USB code in particular needs to allocate several buffers to send and receive packets.

  3. #3
    Quote Originally Posted by PaulStoffregen View Post

    Yes, lots of stuff uses RAM by default. The USB code in particular needs to allocate several buffers to send and receive packets.
    Whoops, I worded my question wrong.

    I was meaning instead is, would a uint8_t switchState[64] take up 64 bytes of RAM, or something more? I'm use to 8-bit micros rather than 32-bit.

  4. #4
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,262
    or a single uint64_t, and bitshift all 64 bits (or simply bitSet, bitClear, etc)

    EDIT I reread your using this method, this would be the most efficient and space conserving method
    Last edited by tonton81; 05-07-2018 at 11:03 PM.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,924
    Quote Originally Posted by Geomancer View Post
    Whoops, I worded my question wrong.

    I was meaning instead is, would a uint8_t switchState[64] take up 64 bytes of RAM, or something more? I'm use to 8-bit micros rather than 32-bit.
    Yes, uint8_t switchState[64] would take 64 bytes of RAM. 64 bits would be much more compact. At some point you'll need a second copy of at least part of the data to compare old versus new state.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •