Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. State Machines - my brain won't do what I want it to

State Machines - my brain won't do what I want it to

Scheduled Pinned Locked Moved The Lounge
tutorial
34 Posts 21 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D David ONeil

    Make a tool to create languages...

    The forgotten roots of science | C++ Programming | DWinLib

    honey the codewitchH Offline
    honey the codewitchH Offline
    honey the codewitch
    wrote on last edited by
    #14

    I've already created a lot of tools for building compiler front-ends. I even developed my own language (actually a subset of C#) for reasons. This however, would be too complicated for the examples I intend to present.

    Real programmers use butterflies

    D 1 Reply Last reply
    0
    • honey the codewitchH honey the codewitch

      I've already created a lot of tools for building compiler front-ends. I even developed my own language (actually a subset of C#) for reasons. This however, would be too complicated for the examples I intend to present.

      Real programmers use butterflies

      D Offline
      D Offline
      David ONeil
      wrote on last edited by
      #15

      You said you were bored. Think greater! A program for generating any language! :laugh: An 'inverse-parser' if you will! It could spit out anything, and each syntax could take experts hundreds of years to decipher (or you could make that your next project!) :laugh: :laugh:

      The forgotten roots of science | C++ Programming | DWinLib

      1 Reply Last reply
      0
      • honey the codewitchH honey the codewitch

        I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

        Real programmers use butterflies

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #16

        As Griff already suggested, a vending machine would probably fit the bill.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • honey the codewitchH honey the codewitch

          I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

          Real programmers use butterflies

          G Offline
          G Offline
          Garth J Lancaster
          wrote on last edited by
          #17

          There was a QA a while ago about 'decoding' some data - the eventual solution was proposed using a RegEx - I was in two minds about it, I personally would have used a state-machine, or maybe it was a 'borderline case' (as opposed to me, 'nut case')

          1 Reply Last reply
          0
          • honey the codewitchH honey the codewitch

            I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

            Real programmers use butterflies

            R Offline
            R Offline
            Rage
            wrote on last edited by
            #18

            honey the codewitch wrote:

            a simple example of a state machine that is real world at all

            Do you own a car ? Think about any car part that is based on electronics or electrical actors -> It runs a state machine. Embedded is full of state machines. Actually, state machines are the AI of embedded world.

            Do not escape reality : improve reality !

            1 Reply Last reply
            0
            • honey the codewitchH honey the codewitch

              I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

              Real programmers use butterflies

              Greg UtasG Offline
              Greg UtasG Offline
              Greg Utas
              wrote on last edited by
              #19

              An example that could grow into something non-trivial, but that everyone understands, is a set of four traffic lights.

              Robust Services Core | Software Techniques for Lemmings | Articles

              <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
              <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

              1 Reply Last reply
              0
              • honey the codewitchH honey the codewitch

                I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                Real programmers use butterflies

                F Offline
                F Offline
                F ES Sitecore
                wrote on last edited by
                #20

                If you can't think of a real world example then maybe it's not something people particularly need to know about?

                1 Reply Last reply
                0
                • honey the codewitchH honey the codewitch

                  I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                  Real programmers use butterflies

                  Sander RosselS Offline
                  Sander RosselS Offline
                  Sander Rossel
                  wrote on last edited by
                  #21

                  When I hear "state machine" I think of games. Maybe you could write something about that? Also, don't know if I used it right, but I once used a state machine for order processing. The order could go from "ordered" to "paid" and "paid" to shipped, but never "ordered" to "shipped", or something like that. It was a bit more complicated than that, but it's been a while so I don't remember, but it was something like that.

                  Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                  F 1 Reply Last reply
                  0
                  • honey the codewitchH honey the codewitch

                    I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                    Real programmers use butterflies

                    T Offline
                    T Offline
                    Tim Deveaux
                    wrote on last edited by
                    #22

                    How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...

                    R Mircea NeacsuM F 3 Replies Last reply
                    0
                    • T Tim Deveaux

                      How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...

                      R Offline
                      R Offline
                      Rich Leyshon
                      wrote on last edited by
                      #23

                      That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software. The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel. Surprising fun and games for such a simple system.

                      T K 2 Replies Last reply
                      0
                      • R Rich Leyshon

                        That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software. The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel. Surprising fun and games for such a simple system.

                        T Offline
                        T Offline
                        Tim Deveaux
                        wrote on last edited by
                        #24

                        Yep - needs rules real quick. Always going to the closest floor request would not be a good idea, e.g.

                        1 Reply Last reply
                        0
                        • honey the codewitchH honey the codewitch

                          I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                          Real programmers use butterflies

                          D Offline
                          D Offline
                          Dar Brett 0
                          wrote on last edited by
                          #25

                          Reading input devices like a mouse, keyboard, joystick - without a fancy event based framework is a good example. Given that the hardware only gives you the state of pressed/not-pressed events like click, tap, double-click, etc. require and abstract state machine on top of the state of the physical buttons, and best of all most beginner programmers are going to understand the examples without needing any other domain knowledge.

                          1 Reply Last reply
                          0
                          • honey the codewitchH honey the codewitch

                            I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                            Real programmers use butterflies

                            M Offline
                            M Offline
                            Matthew Dennis
                            wrote on last edited by
                            #26

                            Way back in the stone-age, I a state machine and a timer interrupt to implement a Serial port on a micro controller. Due to the efficiency of the state machine, I was able to handle 9600 baud in the background while the software handled its main task, probably driving a printer. I think that was state driven as well. It’s amazing what you could do in lees than 4K ROM and 120 byes of Ram, including stack. One of the big advantage of some state machine implementation is they take very little RAM.

                            "Time flies like an arrow. Fruit flies like a banana."

                            1 Reply Last reply
                            0
                            • T Tim Deveaux

                              How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...

                              Mircea NeacsuM Online
                              Mircea NeacsuM Online
                              Mircea Neacsu
                              wrote on last edited by
                              #27

                              Tim Deveaux wrote:

                              How about an elevator.

                              I second that: if my memory serves me well, Knuth used it as an example in TAOCP (The Art of Computer Programming) and he didn't use FSMs (I'll have to go review that). If Knuth himself used it, it must be good. Mircea

                              1 Reply Last reply
                              0
                              • honey the codewitchH honey the codewitch

                                I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                                Real programmers use butterflies

                                M Offline
                                M Offline
                                Marc Clifton
                                wrote on last edited by
                                #28

                                There's a bunch of articles on state machines already here on CP. Games, like Hunt the Wumpus - Wikipedia[^] are perfect examples for using a state machine. BTW, I actually knew Gregory Yob, he was very much a mentor to me in my early 20's.

                                Latest Articles:
                                Abusing Extension Methods, Null Continuation, and Null Coalescence Operators

                                1 Reply Last reply
                                0
                                • R Rich Leyshon

                                  That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software. The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel. Surprising fun and games for such a simple system.

                                  K Offline
                                  K Offline
                                  kmoorevs
                                  wrote on last edited by
                                  #29

                                  Speaking of elevators, I've always wanted to ask if repeated button pushes get precedence. It would explain why when I'm standing there and the button is already lit, others feel the need to press it again, sometimes repeatedly. Do they know something I don't?

                                  "Go forth into the source" - Neal Morse

                                  K 1 Reply Last reply
                                  0
                                  • T Tim Deveaux

                                    How about an elevator. Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest. ...er *puff* yeah... something like that...

                                    F Offline
                                    F Offline
                                    F ES Sitecore
                                    wrote on last edited by
                                    #30

                                    Surely elevators don't use state machines? The basics could be done with a state machine, but what about if multiple floors are selected from multiple people in the lift? What if I go from 2 to 20 and someone on 15 presses up? What if they press down? It's surely too complex?

                                    T 1 Reply Last reply
                                    0
                                    • Sander RosselS Sander Rossel

                                      When I hear "state machine" I think of games. Maybe you could write something about that? Also, don't know if I used it right, but I once used a state machine for order processing. The order could go from "ordered" to "paid" and "paid" to shipped, but never "ordered" to "shipped", or something like that. It was a bit more complicated than that, but it's been a while so I don't remember, but it was something like that.

                                      Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                                      F Offline
                                      F Offline
                                      F ES Sitecore
                                      wrote on last edited by
                                      #31

                                      Unity uses FSM to manage AI and general game play flow.

                                      1 Reply Last reply
                                      0
                                      • F F ES Sitecore

                                        Surely elevators don't use state machines? The basics could be done with a state machine, but what about if multiple floors are selected from multiple people in the lift? What if I go from 2 to 20 and someone on 15 presses up? What if they press down? It's surely too complex?

                                        T Offline
                                        T Offline
                                        Tim Deveaux
                                        wrote on last edited by
                                        #32

                                        Maybe simplify it - each 'floor' (like a cell on a Turing machine) has a signalled state - off, on wanting an up pickup, on wanting a down pickup, or on as a destination. And the elevator is always moving through all floors, in one of the two directions. If the elevator polls each floor state as it approaches it and reacts accordingly, it doesn't matter whether the signals are set inside or outside the elevator. Lots of subtleties to add on of course (bloody feature creep) but...

                                        1 Reply Last reply
                                        0
                                        • honey the codewitchH honey the codewitch

                                          I'm trying to come with a simple article on building state machines. I figured it wouldn't be too complicated, nor should it be since I wanted the article to be accessible to beginners. The trouble is, I can't think of a simple example of a state machine that is real world at all, and I don't want to lead with something contrived because I also want to explain the "why" of state machines with a practical example. This shouldn't be very complicated. I've been writing state machines for various things since the mid 1980s. In all that time I must have done something simple, especially since I was coding on a 16-bit machine back in the beginning. Grrrr

                                          Real programmers use butterflies

                                          M Offline
                                          M Offline
                                          Member 12982558
                                          wrote on last edited by
                                          #33

                                          A bank account is an excellent example of a state machine, since it can be viewed on different levels. For the bank your account is on one of two states: you have either money or a dept. For yourself you might add states like a. positive saldo b. overdraft (i.e. nearing the end of the month) c. too much for a regular bank account: need to transfer to a saving account So, dependent on the view you take you can identify a few states your account is in

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups