Mr Baggins Computer Technology

Better Buttons

First things first, we’re going to improve our button code so it’s easier to read. We’re going to do this by defining our own custom word in the code PRESSED that means low, so instead of having to mentally flip “low = on” we can just read something closer to “if button = pressed” which makes way more sense.

Way up the top of our Sketch (Remember, that’s what these code files are called), put this line under the one that sets LED to 13.

1
#define PRESSED LOW

This lets us create our own keyword PRESSED that we can use instead of LOW whenever we want. Now to use it, we will rewrite our loop to also be much more readable rather than clever. Change your loop to look like this, paying careful attention to curly brackets, capitals and semicolons. Upload this code and make sure it works before continuing.

1
2
3
4
5
6
7
void loop(){
  if (digitalRead(buttonPin) == PRESSED) {
    digitalWrite(LED,HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

Try reading through this code out loud or in your head as English. “If reading the buttonPin is PRESSED then write the LED to HIGH…”

Doing more with a button

Back a few lessons, we made an LED flash with the following four lines of code:

1
2
3
4
  digitalWrite(LED, HIGH);
  delay(100);
  digitalWrite(LED, LOW);
  delay(100);

These turn the LED on, wait, turn it off, wait, then repeat

There’s no reason this has to be our entire code though. We can put these 4 lines ANYWHERE we want to flash a light. Like for instance, whenever we push the button. Find the part in our code that matches this flashing code (The line of code that turns the LED on), and add the other 3 lines after it, so it looks like this:

1
2
3
4
5
6
7
8
9
10
void loop(){
   if (digitalRead(buttonPin) == PRESSED){  
     digitalWrite(LED,HIGH);                
     delay(100);                            // NEW!
     digitalWrite(LED, LOW);                // NEW!
     delay(100);                            // NEW!
   } else {
     digitalWrite(LED, LOW);
   }
}

Now try it out and note what it does!

If and only if

We used the words if and else to split our code in half. The round brackets after if are a condition. The condition has to be something that is true or false. Our condition is whether or not the button is pressed. If the button is pressed, the first chunk of code runs, which makes the light flash now. If it’s not true, the code jumps to else and does whatever is after else instead.

In English it might sound like “If you have more than $4, buy a large chocolate milk, else buy a small chocolate milk” or in fake-code:

1
2
3
4
5
  if (money > $4) {
      buymilk(Large);
  } else {
      buymilk(small);
  }

It’s important that you get used to if as it’s a very important building block for making our code, electronics or robots feel smart, making decisions that make sense or in response to their environment.

Challenges

Here is a list of things to try and do with buttons, lights, buzzers and if. We’ve already ticked some off if you’ve followed along with the instructions.

  1. Make the button turn the light on and off
  2. Make the button make the light flash
  3. Make 2 lights, one that’s on when the button is not pushed, swapping to the other light when the button IS pushed. Sort of like a “mode selector” or “push to talk” button.
  4. Make 2 lights that stay off until the button is pushed. When pushed, they flash alternately like a railway crossing that has detected a train coming.
  5. Make 2 lights with 2 buttons. Each button controls just one light independently of the other.
  6. Make 2 buttons with 1 light, where the light only comes on if BOTH buttons are pushed.
  7. Make the opposite: the light only goes OFF when both buttons are pushed
  8. Make a combination lock with 3 or 4 or more buttons: The light only comes on if the correct combination of buttons is held down at the same time.

Hopefully you can see that with just buzzers, lights and buttons we have opened up LOTS of possibilities. Don’t worry, we’ve still got plenty more to add too!

Goals for this site

How this site was made

About the Author