Advanced Concepts: OOP, State Machines, and Connected Projects
This lesson pulls together the professional patterns that keep larger projects manageable. You will learn object-oriented programming to package behavior into reusable objects, state machines to organize multi-step logic cleanly, and you will move to the wireless T-Display ESP32 board to host a web page and drive a built-in color screen. These ideas return often in the months ahead.
Setting up the ESP32 board
You are moving from the HERO or Uno board to the T-Display ESP32, which is more capable and, importantly, has Wi-Fi built in. It needs a little setup. Install the TFT_eSPI library into your libraries folder, then in the editor select the ESP32 Dev Module board (with the ESP32 board package installed), set PSRAM to disabled and flash size to 4MB, and leave the rest at default. Choose the correct serial port; if unsure, unplug other USB devices and use the remaining port. The library's Examples menu is a good place to confirm everything uploads.
Object-oriented programming
Object-oriented programming organizes code into reusable structures called objects, which makes larger programs modular and easier to manage. You define a class, a blueprint, with private data that only the object can touch and public functions that others can call. An LED class might hold a private pin number and offer public on and off functions, plus a constructor that runs when the object is created to set the pin as an output. Once defined, LED myLED(9) creates an object, and myLED.on() calls its function. The class hides the wiring details behind clean, self-describing calls.
State machines
A state machine manages complex behavior by breaking it into a fixed set of states and the transitions between them. A traffic light is the classic example: it can only be red, green, or yellow, and it moves through them in a set order. You represent the states with an enumeration, track the current state in a variable, and use a switch statement to run the code for whichever state is active, ending each case with break and setting the next state. You could write the same thing with a chain of if and else if, but the switch form is cleaner and makes the states and transitions obvious.
Wireless communication with the ESP32
The HERO board cannot go online because it has no wireless chip, but the ESP32 is designed for Wi-Fi. With a few lines you can connect it to your network and have it act as a tiny web server: it listens on port 80 and serves a web page you can open from any device on the same network. You put your network name and password at the top of the sketch, which means you should remove those credentials before sharing your code. Prefer a 2.4 GHz network, since these boards are more reliably compatible with it than 5 GHz.
Driving the built-in color display
The T-Display has a small 1.14 inch color LCD driven by the TFT_eSPI library. You create a display object, set a background and text color, and position text with setCursor using x and y coordinates. One thing that surprises people: y increases downward, so the point 0,0 is the top-left corner and larger y values move down the screen. Clearing the screen each update and redrawing prevents old values from overlapping new ones.
Working through it
Write and use an LED class. Define a class LED with a private pin, a constructor that sets the pin to OUTPUT, and public on and off functions. Create LED myLED(9) and call myLED.on(), delay, myLED.off() in loop. The code reads like plain English.
Build a traffic-light state machine. Declare enum State {RED, GREEN, YELLOW} and a currentState starting at RED. In loop, switch on currentState: light the matching LED, wait, turn it off, set the next state, and break. Each pass advances the cycle.
Connect the ESP32 to Wi-Fi and serve a page. Put your SSID and password at the top, start a server on port 80, connect in setup, and in loop respond to a browser request with a small HTML page. Read the assigned IP from the Serial Monitor and open it in a browser on the same network.
Show a live reading on the LCD. Wire a potentiometer to an analog pin such as GPIO 27. Initialize the TFT display, then each half second read the potentiometer, clear the screen, set the cursor, and print the value. Turn the knob and watch the number update on the board's own screen.