Thread: I need help modifying current state machine to open lock with a specific combination

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    I need help modifying current state machine to open lock with a specific combination

    Code:
    I need help to have this state machine open the lock with a combination of 4712
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NoneRight 0
    #define OneRight 1
    #define TwoRight 2
    #define Open 3
    
    //prototypes
    void LockStateMachine(unsigned char NewEvent);
    void OpenLock(void);
    void LatchLock(void);
    
    int main(int argc, char *argv[])
    {
        unsigned char KeyReturn;
        while(1)
        {
                printf("Enter a number: ");
                scanf("%c", &KeyReturn); /*check for events */
                getchar();
                LockStateMachine(KeyReturn); /* run state machine */
        }
      system("PAUSE");	
      return 0;
    }
    
    void LockStateMachine(unsigned char NewEvent)
    {
         static unsigned char CurrentState;
         unsigned char NextState;
         switch(CurrentState)
         {
                             case NoneRight :
                                  switch(NewEvent)
                                  {
                             case '2':
                                  NextState = OneRight;
                                  break;
                             default: /* we are already in NoneRight */
                                      break;
                                   }
                                   break;
                                   case OneRight :
                                   switch(NewEvent)
                                   {
                             case '1':
                                  NextState = TwoRight;
                                  break;
                             default: /* anything else sends us back */
                                      NextState = NoneRight;
                                      break;
                                       }
                                       break;
                                       case TwoRight :
                                       switch(NewEvent)
                                       {
                             case '8':
                                  NextState = Open;
                                  OpenLock();
                                  break;
                             default:
                                     NextState = NoneRight;
                                     break;
                             }
                             break;
                             case Open :
                             NextState = NoneRight;
                             LatchLock();
                             break;
         }
         CurrentState = NextState;
         return;
    }
    void OpenLock(void)
    {
         printf("Lock opened\n");
    }
    
    void LatchLock(void)
    {
         printf("Lock closed\n");
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Is it your own code? Where do you have difficulty, with your code?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NoneRight 0
    #define OneRight 1
    #define TwoRight 2
    #define ThreeRight 3
    #define Open 4
    
    //prototypes
    void LockStateMachine(unsigned char NewEvent);
    void OpenLock(void);
    void LatchLock(void);
    
    int main(int argc, char *argv[])
    {
        unsigned char KeyReturn;
        while(1)
        {
                printf("Enter a number: ");
                scanf("%c", &KeyReturn); /*check for events */
                getchar();
                LockStateMachine(KeyReturn); /* run state machine */
        }
      system("PAUSE");	
      return 0;
    }
    
    void LockStateMachine(unsigned char NewEvent)
    {
         static unsigned char CurrentState;
         unsigned char NextState;
         switch(CurrentState)
         {
                             case NoneRight :
                                  switch(NewEvent)
                                  {
                                                  case '4':
                                                       NextState = OneRight;
                                                       break;
                             default: /* we are already in NoneRight */
                                      break;
                                   }
                                   break;
                             
                             case OneRight :
                                  switch(NewEvent)
                                  {
                                             case '7':
                                                  NextState = TwoRight;
                                                  break;
                                             default: /* anything else sends us back */
                                                  NextState = NoneRight;
                                                  break;
                                  }
                                       
                                       break;
                             case TwoRight :
                                       switch(NewEvent)
                                       {
                                             case '1':
                                                  NextState = ThreeRight;
                                                  break;
                                             default: /* anything else sends us back */
                                                  NextState = NoneRight;
                                                  break;
                                       }
                                       
                                       break;
                             case ThreeRight :
                                       switch(NewEvent)
                                       {
                                                       case '2':
                                                            NextState = Open;
                                                            OpenLock();
                                                            break;
                                                       default:
                                                               NextState = NoneRight;
                                                               break;
                                       }
                                       break;
                             case Open :
                                       NextState = NoneRight;
                                       LatchLock();
                                       break;
         }
         CurrentState = NextState;
         return;
    }
    void OpenLock(void)
    {
         printf("Lock opened\n");
    }
    
    void LatchLock(void)
    {
         printf("Lock closed\n");
    }
    there, 4712, oh and the problem is that when it runs, even when I type in numbers it just keeps asking again and never actually goes to any state or opens, it compiles w/o errors :S

    >>apparently currentstate is not int properly?
    Last edited by Charak; 03-16-2011 at 07:36 AM.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    As your NewEvent variable has the value being passed by the main. And current state variable is locally declared and not initialized. Depending upon the compiler you are using, not sure whether it's a garbage or a zero. So,your switch statements will not even execute. You can try this with writing a default at the end of switch(current_State) or whatever the name is....


    NOTE: Please read the indentation and indent your code from next time....
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Dev C++

    how do i fix currentState?
    Last edited by Charak; 03-16-2011 at 08:28 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Charak View Post
    Dev C++

    how do i fix currentState?
    First of all if you're on DevC++ you need to update to a newer IDE and compiler.

    Second... Current_State is a static variable (i.e. holds it's value between calls to the function it's in) so you can't use an initializer value on it. It's fine as it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Class Design question.
    By g4j31a5 in forum C++ Programming
    Replies: 10
    Last Post: 11-08-2006, 10:28 PM
  3. Way to get the current open programs displayed?
    By CPPguy1111 in forum C++ Programming
    Replies: 6
    Last Post: 06-22-2005, 12:24 AM
  4. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM