Button debounce fsm

Hi, i need help.(sorry for bad english) I need to make a code for a university problem.
When X1 button is pressed it goes to the next state, when X2 is pressed it goes back, its supose to simulate cars entering and exiting a parquing lot. The problem is that i cant have any delays on the program,but if dont have the delay it jumps states (from 0 to 10 in an instante), my professor said to use a debounce on the buttons but i dont know how. And it needs to be an FSM.
The code might not be the best but i am trying my best.

short int L0_pin = 2;
short int L1_pin = 3;
short int L2_pin = 4;

short int LA_pin = 5;
short int LB_pin = 6;
short int LC_pin = 7;
short int LD_pin = 8;
short int LE_pin = 9;
short int LF_pin = 10;
short int LG_pin = 11;
short int LH_pin = 1;

short X1_pin = 13;
short X2_pin = 12;
short int L2_out;
short int L1_out;
short int L0_out; 

short int LA_out;
short int LB_out;
short int LC_out;
short int LD_out;
short int LE_out;
short int LF_out;
short int LG_out;
short int LH_out;
void setup() {
 pinMode(L0_pin, OUTPUT);
 pinMode(L1_pin, OUTPUT);
 pinMode(L2_pin, OUTPUT);

 pinMode(LA_pin, OUTPUT);
 pinMode(LB_pin, OUTPUT);
 pinMode(LC_pin, OUTPUT);
 pinMode(LD_pin, OUTPUT);
 pinMode(LE_pin, OUTPUT);
 pinMode(LF_pin, OUTPUT);
 pinMode(LG_pin, OUTPUT);
 pinMode(LH_pin, OUTPUT);
 
 pinMode(X1_pin, INPUT);
 pinMode(X2_pin, INPUT);

 
 Serial.begin(9600);
}
void loop() {
static short int trigger_in;
short int trigger_out;
static short int L2_in;
static short int L1_in;
static short int L0_in;

static short int LA_in;
static short int LB_in;
static short int LC_in;
static short int LD_in;
static short int LE_in;
static short int LF_in;
static short int LG_in;
static short int LH_in;

short int X1;
short int X2;


X1 = (short)digitalRead(X1_pin);
X2 = (short)digitalRead(X2_pin);

counter_fsm(X1,X2);

Serial.print(L2_in);
Serial.print(L1_in);
Serial.println(L0_in);
digitalWrite(L0_pin,L0_in);
digitalWrite(L1_pin,L1_in);
digitalWrite(L2_pin,L2_in);

digitalWrite(LA_pin,LA_in);
digitalWrite(LB_pin,LB_in);
digitalWrite(LC_pin,LC_in);
digitalWrite(LD_pin,LD_in);
digitalWrite(LE_pin,LE_in);
digitalWrite(LF_pin,LF_in);
digitalWrite(LG_pin,LG_in);
digitalWrite(LH_pin,LH_in);


L0_in = L0_out;L1_in = L1_out;L2_in = L2_out;
LA_in = LA_out;LB_in = LB_out;LC_in = LC_out;
LD_in = LD_out;LE_in = LE_out;LF_in = LF_out;
LG_in = LG_out;LH_in = LH_out;

}
void counter_fsm(short X1,short X2){
 static short int state; 

 switch (state) {
 case 0:
 delay(200);
 if ( X1 == 1) {state = 1;}
 if ( X2 == 1) {state = 0;}
 L2_out = 0;L1_out = 0;L0_out = 1; 
 LA_out = 0;LB_out = 0;LC_out = 0; LD_out = 0;LE_out = 0;
 LF_out = 0;LG_out = 1;LH_out = 1;  
 break;
 
 case 1:
 delay(200);
 if ( X1 == 1) {state = 2;}
 if ( X2 == 1) {state = 0;}
 L2_out = 0;L1_out = 0;L0_out = 1; 
 LA_out = 1;LB_out = 0;LC_out = 0; LD_out = 1;LE_out = 1;
 LF_out = 1;LG_out = 1;LH_out = 1;  
 break;
 
 case 2:
 delay(200);
 if ( X1 == 1) {state = 3;}
 if ( X2 == 1) {state = 1;}
 L2_out = 0;L1_out = 0;L0_out = 1; 
 LA_out = 0;LB_out = 0;LC_out = 1; LD_out = 0;
 LE_out = 0;LF_out = 1;LG_out = 0;LH_out = 1;  
 break;
 
 case 3:
 delay(200);
 if ( X1 == 1) {state = 4;}
 if ( X2 == 1) {state = 2;}
 L2_out = 0;L1_out = 0;L0_out = 1; 
 LA_out = 0;LB_out = 0;LC_out = 0; LD_out = 0;
 LE_out = 1;LF_out = 1;LG_out = 0;LH_out = 1;  
 break;
 
 case 4:
 delay(200);
 if ( X1 == 1) {state = 5;}
 if ( X2 == 1) {state = 3;}
 L2_out = 0;L1_out = 0;L0_out = 1; 
 LA_out = 1;LB_out = 0;LC_out = 0; LD_out = 1;LE_out = 1;
 LF_out = 0;LG_out = 0;LH_out = 1;  
 break;
 
 case 5:
 delay(200);
 if ( X1 == 1) {state = 6;}
 if ( X2 == 1) {state = 4;}
 L2_out = 0;L1_out = 1;L0_out = 0; 
 LA_out = 0;LB_out = 1;LC_out = 0; LD_out = 0;LE_out = 1;
 LF_out = 0;LG_out = 0;LH_out = 1;  
 break;
 
 case 6:
 delay(200);
 if ( X1 == 1) {state = 7;}
 if ( X2 == 1) {state = 5;}
 L2_out = 0;L1_out = 1;L0_out = 0; 
 LA_out = 0;LB_out = 1;LC_out = 0; LD_out = 0;LE_out = 0;
 LF_out = 0;LG_out = 0;LH_out = 1;  
 break;
 
 case 7:
 delay(200);
 if ( X1 == 1) {state = 8;}
 if ( X2 == 1) {state = 6;}
 L2_out = 1;L1_out = 0;L0_out = 0; 
 LA_out = 0;LB_out = 0;LC_out = 0; LD_out = 1;LE_out = 1;
 LF_out = 1;LG_out = 1;LH_out = 1;  
 break;

  case 8:
 delay(200);
 if ( X1 == 1) {state = 9;}
 if ( X2 == 1) {state = 7;}
 L2_out = 1;L1_out = 0;L0_out = 0; 
 LA_out = 0;LB_out = 0;LC_out = 0; LD_out = 0;LE_out = 0;
 LF_out = 0;LG_out = 0;LH_out = 1;  
 break;

  case 9:
 delay(200);
 if ( X1 == 1) {state = 10;}
 if ( X2 == 1) {state = 8;}
 L2_out = 1;L1_out = 0;L0_out = 0; 
 LA_out = 0;LB_out = 0;LC_out = 0; LD_out = 0;LE_out = 1;
 LF_out = 0;LG_out = 0;LH_out = 1; 
 break;

  case 10:
 delay(200);
 if ( X1 == 1) {state = 10;}
 if ( X2 == 1) {state = 9;}
 L2_out = 1;L1_out = 0;L0_out = 0; 
LA_out = 0;LB_out = 0;LC_out = 0; LD_out = 0;LE_out = 0;
LF_out = 0;LG_out = 1;LH_out = 0;
 break;
 }
}

really?

look this over
the use of LEDs in this code are not the same as in your code

// check multiple buttons and toggle LEDs

const byte    PinLeds [] = { 10, 11, 12 };
const byte    PinButs [] = { A1, A2, A3 };
const int     Nbut       =  sizeof(PinButs);

byte          butState [Nbut];
unsigned long msecButs [Nbut];

const unsigned long DebounceMsec = 20;

// -----------------------------------------------------------------------------
#define DebounceMsec    10
int
chkButtons ()
{
     unsigned long  msec = millis ();

    for (unsigned n = 0; n < sizeof(PinButs); n++)  {
        if (msecButs [n] && (msec - msecButs [n]) < DebounceMsec)
            continue;
        msecButs [n] = 0;

        byte but = digitalRead (PinButs [n]);
        if (butState [n] != but)  {
            butState [n] = but;

            msecButs [n] = msec ? msec : 1;      // make sure non-zero

            if (LOW == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (PinLeds [2], ! digitalRead (PinLeds [2]));
        break;

    case 1:
        digitalWrite (PinLeds [1], ! digitalRead (PinLeds [1]));
        break;

    case 0:
        digitalWrite (PinLeds [0], ! digitalRead (PinLeds [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(PinButs); n++)  {
        pinMode (PinButs [n], INPUT_PULLUP);
        butState [n] = digitalRead (PinButs [n]);
    }

    for (unsigned n = 0; n < sizeof(PinLeds); n++)  {
        digitalWrite (PinLeds [n], HIGH);       // off
        pinMode      (PinLeds [n], OUTPUT);
    }
}

i'll claim that msecButs [] is the state variable in an FSM

Arduino has an example in your IDE >> EXAMPLES >> BUILT IN EXAMPLES >> 02 DIGITAL >> DEBOUNCE, and here...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.