Go Down

Topic: Single switch, multiple mode sketch question (answered, video of final product) (Read 688 times) previous topic - next topic

stirfoo

How about something like

Code: [Select]

int curMode = 0;
int curHallMode = 0;
unsigned int pattern = 0;
/* assumes LED1 is on pin 2 ... LED12 is on pin 13 */
int pinOffset = 2;

// Each mode has 12 patterns selected by hall mode, correct?                   
// So each 3 represents a pattern                                               
const unsigned int modes[][12] = {
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,},
};

int getMode() {
    // read the button, return the mode                                         
    return 0;
}

int getHallMode() {
    // read the hall sensor, return the hall mode                               
    return 0;
}

void setup() {
    // ins and outs                                                             
}

void loop() {
    curMode = getMode();
    curHallMode = getHallMode();
    pattern = modes[curMode][curHallMode];
    for (int i=0; i<12; i++)
digitalWrite(i + pinOffset, pattern >> i & 0x0001);
}



The patterns wont be visual at all as they are represented by integers. You could use bit fields but that would just complicate matters. Strings could be used to store the patterns "010001010001" but then you'd have to manipulate that string to write to the LEDs.

lloyddean

#6
Feb 01, 2013, 03:36 am Last Edit: Feb 01, 2013, 05:31 am by lloyddean Reason: 1
You seem curious, continue to be so ...

As alluded to by PaulS things could be made more compact and easier to read.

Minimal changes to your code (many more improvements possible).

Code: [Select]

// <http://arduino.cc/forum/index.php/topic,146206.msg1098539.html#new>

#define ENTRIES(ARRAY)      (sizeof(ARRAY) / sizeof(ARRAY[0]))


const uint8_t   pinLED_00       =  0;
const uint8_t   pinLED_01       =  1;
const uint8_t   pinLED_02       =  2;
const uint8_t   pinLED_03       =  3;
const uint8_t   pinLED_04       =  4;
const uint8_t   pinLED_05       =  5;
const uint8_t   pinLED_06       =  6;
const uint8_t   pinLED_07       =  7;
const uint8_t   pinLED_08       =  8;
const uint8_t   pinLED_09       =  9;
const uint8_t   pinLED_10       = 10;
const uint8_t   pinLED_11       = 11;

const uint8_t   pinBUTTON       = 12;
const uint8_t   pinSENSOR_HALL  = 13;

const uint8_t   pinsLED[]       =
{
      pinLED_11, pinLED_10, pinLED_09, pinLED_08
    , pinLED_07, pinLED_06, pinLED_05, pinLED_04
    , pinLED_03, pinLED_02, pinLED_01, pinLED_00
};


uint8_t modeHall                = 0;
uint8_t stateHallThen           = LOW;

uint8_t modeLED                 = 0;
uint8_t stateButtonThen         = LOW;


void setPattern(uint16_t pattern)
{
    for ( int i = ENTRIES(pinsLED); i--; pattern >>= 1 )
    {
        digitalWrite(pinsLED[i], ((pattern & 1) ? HIGH : LOW));
    }
}

uint8_t debounceButton(uint8_t stateThen)
{
    uint8_t state = digitalRead(pinBUTTON);
    if ( stateThen != state )
    {
        delay(5UL);
        state = digitalRead(pinBUTTON);
    }

    return state;
}

boolean debounceHall(boolean stateThen)
{
    uint8_t state = digitalRead(pinSENSOR_HALL);
    if ( stateThen != state )
    {
        delay(5UL);
        state = digitalRead(pinSENSOR_HALL);
    }

    return state;
}

void loop()
{
    // button tracking

    uint8_t stateButton = debounceButton(stateButtonThen);
    if ( (stateButtonThen == LOW) && (stateButton == HIGH) )
    {
        modeLED += 1;
    }

    stateButtonThen = stateButton;
    if ( modeLED > 9 )
    {
        modeLED = 0;
    }


    // hall tracking

    uint8_t stateHall = debounceHall(stateHallThen);
    if ( (stateHallThen == LOW) && (stateHall == HIGH) )
    {
        modeHall += 1;
    }

    stateHallThen = stateHall;
    if ( modeHall > 11 )
    {
        modeHall = 0;
    }

    // Mode 0 - All On

    if ( modeLED == 0 )
    {
        setPattern(0b0000111111111111);
    }

    // Mode 1 - 1 pinLED_ tracking
    if ( modeLED == 1 )
    {
        const uint16_t  pattern[] =
        {
              0b0000000000000001
            , 0b0000000000000010
            , 0b0000000000000100
            , 0b0000000000001000
            , 0b0000000000010000
            , 0b0000000000100000
            , 0b0000000001000000
            , 0b0000000010000000
            , 0b0000000100000000
            , 0b0000001000000000
            , 0b0000010000000000
            , 0b0000100000000000
        };

        setPattern(pattern[modeHall]);
    }
}

void setup()
{
    pinMode(pinSENSOR_HALL, INPUT);
    pinMode(pinBUTTON, INPUT);

    for (int i = ENTRIES(pinsLED); i--; )
    {
        pinMode(pinsLED[i], OUTPUT);
    }
}


abecks44

#7
Feb 01, 2013, 04:17 am Last Edit: Feb 01, 2013, 05:30 am by abecks44 Reason: 1
Thanks for all the feedback guys, I actually just got my Uno a while ago, and now I'm going to spend the next couple hours trying to get this thing to work, but i still don't have the hall sensor yet... It seems like everyone is agreeing on the array thing, so I'm going to try and hopefully learn to do those.. And yes, the hall sensor is to get a gauge of how fast the board is moving. The whole point of the led patterns set the way they are is to get the effect of the leds being stationary over a single point over the ground while the board is actually moving.

Thanks for the help so far and keep posting array codes so I can try them out and learn how they work!

Oh, and lloyddean, the code you posted cycles through the 10 modes correctly, but it seems to be completely disregarding the hall sensor. I have the Uno hooked up to a breadboard of 12 leds and on mode 0 every led is on just like I wanted. Then on mode 1 just the first led is on just as I wanted as well, but when I push the hall sensor (it's currently a button b/c I don't have the actual hall sensor yet) nothing changes. Just that one led remains on.
If someone can find the problem with this I think I can build the rest of the modes myself, some help with this problem would be huge!

I also just noticed that when I press the hall button on any mode, the built in led turns on. I know the built in led is hooked up to pin 13 by default, but I'm still unsure if this is supposed to happen so I thought I'd include it.

lloyddean

My mistake, as usual when I'm tired ...

Maybe fixed above.

abecks44


My mistake, as usual when I'm tired ...

Maybe fixed above.


Yeah, that did the trick! Works exactly the way I wanted, thank you sooo much. I'm going to get to work trying to code the rest of the modes now, thanks again.

Go Up