Problem with Array of structs

Discussion created by protoease on Jan 5, 2011
Latest reply on Jan 6, 2011 by protoease

I have all my global variabls in a header file, global.h.  In it, I declare the following:



// SWITCH debounce counterstypedef struct {    unsigned char debounce_active;  unsigned int debounce_timer;  int held_timer;  unsigned char state0;  unsigned char state1;} SWITCH;extern SWITCH Switch[6];



As you can see, I'm trying to declare an array of structs called Switch, of type SWITCH.


In my main.c file, I initialize all of the structs:



    for(key_num = 0; key_num <= 6; key_num++)    {      Switch[key_num].debounce_active = FALSE;      Switch[key_num].debounce_timer = 0;      Switch[key_num].held_timer = 0;      switch(key_num)      {        case 0:          Switch[0].state0 = SWITCH0;          Switch[0].state1 = SWITCH0;          break;        case 1:          Switch[1].state0 = SWITCH1;          Switch[1].state1 = SWITCH1;          break;        case 2:          Switch[2].state0 = SWITCH2;          Switch[2].state1 = SWITCH2;          break;        case 3:          Switch[3].state0 = SWITCH3;          Switch[3].state1 = SWITCH3;          break;        case 4:          Switch[4].state0 = SWITCH4;          Switch[4].state1 = SWITCH4;          break;        case 5:          Switch[5].state0 = SWITCH5;          Switch[5].state1 = SWITCH5;          break;        case 6:          Switch[6].state0 = SWITCH1;  // state 6 is a combo of up & down keys          Switch[6].state1 = SWITCH4;          break;        default:          break;      };    }


No compiler warnings, errors, but when I debug the thing none of the member variables are getting set correctly.


What I can see when I do this is just garbage setting in the member variables, such as Switch[3].state0 will read some large int value that doesn't make any sense.



I would appreciate any help on this, it is driving me crazy.  This was just supposed to be a simple debounce routine.