Chapter 3 · How Programming Works
Your First Logic
This is where it all comes together. You will build a real, working piece of logic from start to finish, test it without touching any hardware, and then solve a small challenge that teaches you something more important than the answer.
By the end of this lesson you will be able to:
- Build a working switch program from an input to an output
- Test a program in simulation with no Miniserver connected
- Judge a good solution from a bad one
The situation
A room has a button and a light. The customer wants the button to control the light.
Later they want something slightly cleverer: press the button, and the light comes on by itself for ten seconds, then goes off. You are going to build both, and you are going to build them the clean way.
Build a simple switch
Start with the simplest complete program. The Switch block holds a state, and its inputs let you toggle it, switch it on, or switch it off.
- Create a programming page named Living Room, and set its Default Room to Living Room.
- Insert the Switch block onto the page and Rename everything to your choice
- Connect digital inputs to the block’s Tg, On and Off inputs. Tg toggles the state, On switches it on, and Off switches it off.
- In the properties of each digital input, set the display type to Switch, so the input behaves correctly in simulation.
- Connect a digital output, a relay, to the block’s O output.
- Start the simulation and test each input in turn. Check that the output responds each time.
Test it in simulation
In the properties of a digital input you can choose how it behaves in simulation: a push button, a switch, or a frequency. If no physical button is wired, LiveView with manual value change also lets you set the input yourself and watch the output react.
Build a piece of logic, simulate it, and watch it behave before it ever touches a real building.
The challenge
Now the small challenge that teaches the real lesson.
Pressing a button on input I1 should switch on the relay output Q1 for ten seconds, then switch it off automatically. The customer also wants to trigger the same function from the app.
Before reading on, think about how you would build it. There are many ways. Only one is best.
The best solution
The clean solution uses a single block: the Push Button block. When its trigger input receives a pulse, it keeps its output on for the time set in its Don parameter, then switches it off.
- Insert a Push Button block, and give it a name that says what it does.
- Connect the digital input I1 to the block’s trigger input.
- Connect the relay Q1 to the block’s output.
- Set the block’s Don parameter of the Push Button to 10 seconds.
- Assign the block to its room, so it appears in the right place in the app.
- Start the simulation, press the input, and check that the relay stays on for 10 seconds.
Because it is one block with the timing set by a parameter, the control in the app matches the function exactly, with nothing extra to explain.
Why that solution is the best one
The same behaviour could be built from several blocks with extra wiring. It would even work. But it would be harder to read, harder to fix, and it would often show up wrongly in the app.
A good solution has four qualities worth carrying into everything you build. It works as specified. Its logic is simple and clear. Its block settings are correct. And it is named for its function and assigned to a room.
A bad solution might still work, but with confusing logic, missing settings, no room assignment, and a view in the app that does not match what the logic really does.