State Machines

Modelling a metronome and debouncing buttons

This lecture introduces the concept of the finite state machine (FSM or state machine for short). Many practical systems, audio-related and otherwise, can be modelled as state machines. This lecture presents applications to a metronome which marks the bar lines and various forms of sensor de-noising. It lays the groundwork for the ADSR envelope to be covered in a later lecture.

Table of contents

  1. What you’ll learn in this lecture
  2. What you’ll make in this lecture
  3. Code examples
  4. Recommended parts

Lecture 13: State Machines

What you’ll learn in this lecture

  • Structure and uses of state machines
  • Debouncing buttons
  • Hysteresis

What you’ll make in this lecture

  • Metronome which marks the bar lines
  • Button debouncer
  • Comparator with hysteresis

Code examples

Find the code examples here

metronome-envelope: Update to the metronome project from Lecture 9 which generates the tick sound using an exponential envelope instead of an audio file. In the lecture, this is extended to have a different pitch for the first beat of the bar.

scope-button: Simple example which displays the value of a digital input on the Bela oscilloscope.

debounce: Template project for implementing a state machine to debounce a push button.

hysteresis-comparator: Implements a simple comparator, with the task in the lecture to add hysteresis (variable threshold) for noise immunity.

Running the examples in this course assumes that you have a Bela Starter Kit or Bela Mini Starter Kit.