Hardware Reference
In-Depth Information
3. he inner beam clears.
4. he outer beam clears.
To interpret the sensor data, you need to write a program that looks for these orders of
events. However, birds don't just ly in and out of their nest boxes; they also might pop just
their heads out, have a look around and then pop back into the box.
his bobbing out of heads has sensors in this sequence:
1. he inner beam breaks.
2. he outer beam breaks.
3. he outer beam clears.
4. he inner beam clears.
When a mother bird is tending her young, she may stand in the nest box entrance and pop
her head into the box. It may be clearer to represent this number of sequences in a diagram,
as shown in Figure 17-7. he next task is to write a program to implement this diagram,
which you will see is best done with a state machine.
Building a State Machine
Diagrams similar to Figure 17-7 that show the transition (movement) between sequences of
states occur frequently in computer science, and as such there are standard techniques to
implement one in code. State machines (sometimes called more fully, inite state machines or
FSMs) consist of states and conditions that govern when there is a change from one state to
another.
In the diagram in Figure 17-7, each circle is a state, with the transitions (movement) between
states represented by arrows and labelled with the event that causes the transition. he
arrow from the block dot shows the starting state.
State machines are implemented in programs with a variable to hold the current state and a
loop. he loop contains a series of if statements that determine when there should be a
change from one state to the next. On each iteration of the loop, all the conditions that allow
the leaving of the current state are tested, and if satisied the current state is updated,
together with any necessary actions required for the transition.
Search WWH ::




Custom Search