So far your circuit talks. Today it learns to listen. Add a button and take command.
Enter the access code from the card inside your Beginner Kit (or from your GAMERIFT order email). Your name appears on every page and printout as the licensee.
Don't have a kit yet? Get the Beginner Kit, the code ships inside. Lost your code? Contact us with your order number.
Until now your circuit has been a performer: it does its show whether you watch or not. Real machines are different. A console waits for the jump button. A kettle waits for the switch. Today you add the kit's most humble part, a little button, and suddenly your circuit has an input. It reacts to you. This is the half of electronics that makes games possible.
Time: 45–60 minutes · Difficulty: 2/5 · You need: Lessons 01 and 02 done.
Every pin on your board can be a mouth or an ear. OUTPUT means the pin speaks, sending volts out, like it does to your LED. INPUT means the pin listens, reporting what voltage arrives. All of computing is this: read inputs, decide, set outputs. You are about to write your first program that decides.
Here is a strange truth: an input pin connected to nothing does not read "off". It reads static from the air, flickering randomly between HIGH and LOW like a radio between stations. Engineers call this a floating pin. The fix is to gently pull the pin to a known voltage when the button is not pressed. Your board has tiny built-in pull-up resistors for exactly this, switched on with the magic word INPUT_PULLUP.
With INPUT_PULLUP, the resting pin reads HIGH, and pressing the button connects it to ground, so it reads LOW. Yes, pressed = LOW. It feels upside down the first time. Every engineer trips on this once; you are tripping on it now so your code never has to.
Unplug the USB first. The button's four legs come in two joined pairs; the legs on the same side of the little gap are connected to each other. Push the button in so it straddles the middle channel of the breadboard, it clicks in flat.
| From | To | Why |
|---|---|---|
| R3 pin 4 | Button leg (top-left) | The listening wire |
| Button leg (bottom-left) | R3 GND | Pressing connects pin 4 to ground |
| R3 pin 13 | Resistor → LED long leg | Same faithful light as Lesson 01 |
| LED short leg | R3 GND | Loop complete |
Two legs of the button do all the work. If your button seems to be "always pressed", you have used two legs from the same joined pair, move one wire to the other side of the gap.
Type it, don't paste it. Meet the most important word in programming: if.
// GAMERIFT Beginner Kit, Lesson 03: Press to Play // Your first machine that listens and decides. const int buttonPin = 4; const int lightPin = 13; void setup() { pinMode(buttonPin, INPUT_PULLUP); // listen, with the built-in pull-up on pinMode(lightPin, OUTPUT); // speak } void loop() { int state = digitalRead(buttonPin); // what does the pin hear right now? if (state == LOW) { // LOW means PRESSED (upside-down logic!) digitalWrite(lightPin, HIGH); // light on while held } else { digitalWrite(lightPin, LOW); // light off when released } }
if (state == HIGH). Now the light is on until you press. You just made a "release detector". Some machines really work this way (fridge door light, for one).Film 15 seconds of your remix and save it for your lesson log, the final build (Lesson 12) submission loves a good journey. Stuck? Send a photo of your wiring via contact us, a real person will spot the crossed leg.
A button knows two words: yes and no. Next lesson you meet a part that knows hundreds, the twisty knob, and use it to dim a light like a cinema.