Introduction: Tutorial 3: Controlling Lights With a Button

This is the third tutorial, where we add a button (which is a Digital Sensor, that detects ON or OFF). We use the button to change the settings of lights (so it is no longer time dependent). Coding wise, we look at how to use variables to keep track of a setting... using 1 and -1 as a toggle.... and then using the mod function for times when you need more than 2 settings.


The Wiki for the button can be found here: https://docs.google.com/document/d/197Q2qxAVfFpiBKTDBb9035Ey0F2x4glOSPJtiAUpzPw/edit?usp=sharing

The Wiki for the LED can be found here: https://docs.google.com/document/d/16PjgJa6EOTU2Pw7gFAdgUt8tYK8eJsXtQvOHXAlh_6w/edit?usp=sharing.


If you need the LED Library, make a folder called LED in your Arduino Libraries folder, and add the 2 LED files below to that folder.

If you need the Button Library, make a folder called Button in your Arduino Libraries folder, and add the 2 Button files below to that folder.

Supplies

FROM YOUR KIT:

1 x Arduino with Grove Shield

1 x USB-B (Arduino end) to USB-A (laptop end).

1 x USB-A to USB-C adaptor (if using a MAC with no USB-A port)

2 x Grove LED Socket Kits (one with Red, one with Green)

2 x Grove cables

6 x screws or rickets (optional if you want to hold the modules down on cardboard)


BYO:

1 x Laptop with Arduino IDE software

1 x Piece of Cardboard (optional if you want to hold the modules down)

1 x Screwdriver (optional if you want to hold the modules down)

Step 1: Build the Circuit

Build the circuit by following the instructions in the PDF below.

Step 2: Code

The code for this project is as follows:

//Add Libraries
#include "Led.h"
#include "Button.h"

//Global Variables
int setting = 1;

//Create Objects
Led led1(2);
Led led2(3);
Button button1(4);

//Setup
void setup() {
}

//Main Loop
void loop() {
 if (button1.isPressed()) {
  setting = setting * -1; 
 }
 
 if (setting == 1) {
  led1.on();
  led2.off();
 }

 if (setting == -1) {
  led1.off();
  led2.on();
 }
  
  delay(300);
}


Let's break it down.


#include "Led.h"
#include "Button.h"

As we are using 2 different types of objects, we need 2 libraries (one for each class).


int setting = 1;

We need to create a setting variable (which is just an integer). It will simply take on 1 or -1. You can alternate easily between these 2 numbers just by multiplying by -1 each time.


Led led1(2);
Led led2(3);
Button button1(4);

This creates our 3 objects. The 2 LEDs, and the 1 Button.


Now let's look at the main loop. The first part

if (button1.isPressed()) {
  setting = setting * -1; 
 }

checks to see if the button is pressed. If it is, the setting is updated (by multiplying its current value by -1, and updating its value to this. This will mean it jumps between 1,-1,1,-1,1,-1 etc. each time you push a button.


The second part of the main loop then determines what state the lights should be in, depending on the setting. If the setting is +1, then the red light goes on (and green goes off)... if it is -1, then the green light goes on (and red goes off).

 if (setting == 1) {
  led1.on();
  led2.off();
 }

 if (setting == -1) {
  led1.off();
  led2.on();
 }


Step 3: Upload Onto Arduino

Finally, you are ready to upload. If you forgot how to upload from your laptop to the Arduino Board, refer to the tutorial on that below (PDF).

Step 4: A Modification (What to Do With More Than 2 Settings)

Ok, so the button is allowing us to alternate between 2 settings (red and green). But what if we want 3 settings or 4 settings (e.g. only red on, only green on, both off, both on)?

One of the easiest ways for each push of the button to cycle through the 4 settings is to use the MOD operation. Think of it like + or - or x, but this time, what it does, is it divides the first number by the second number... but doesn't return the answer, but simply the remainder. Let's look at MOD 4 (which in coding, we use % 4).

1 % 4 = 1 (because 1 divided by 4 = 0 remainder 1)

2 % 4 = 2 (because 2 divided by 4 = 0 remainder 2)

3 % 4 = 3 (because 3 divided by 4 = 0 remainder 3)

4 % 4 = 0 (because 4 divided by 4 = 1 remainder 0)

5 % 4 = 1 (because 5 divided by 4 = 1 remainder 1)

Hence, the answers will cycle through 0,1,2,3,0,1,2,3,0,1,2,3.

Code for 4 settings is this:

 if (button1.isPressed()) {
    setting = (setting + 1) % 4; 
  }

So the setting adds 1 to itself, BUT then divides itself by 4, and takes the remainder. The only possible values it will take is 0,1,2,3 (the only remainders you can have when dividing by 4). Then you just need to have different states of the lights for each setting. The overall code to get 4 settings (both on, only red on, only green on, both off) is this:

//Add Libraries
#include "Led.h"
#include "Button.h"

//Global Variables
int setting = 1;

//Create Objects
Led led1(2);
Led led2(3);
Button button1(4);

//Setup
void setup() {
}

//Main Loop
void loop() {
 if (button1.isPressed()) {
 setting = (setting + 1) % 4;
 }
 
 if (setting == 0) {
 led1.on();
 led2.on();
 }

 if (setting == 1) {
 led1.on();
 led2.off();
 }

 if (setting == 2) {
 led1.off();
 led2.on();
 }

 if (setting == 3) {
 led1.off();
 led2.off();
 }
  
 delay(300);
}


Step 5: Challenge: HeadTorch

Here's a challenge now...

Using 3 LEDs and a button connected to the Grove board, make some code and upload it so that the button cycles through 3 settings

SETTING 0: off

SETTING 1: all 3 lights on

SETTING 2: only the middle light is on


You could also try adding a fourth setting, which is for the middle light to flash. We suggest checking out the Button's Wiki Example as a guide of how you might do this (https://docs.google.com/document/d/197Q2qxAVfFpiBKTDBb9035Ey0F2x4glOSPJtiAUpzPw/edit?usp=sharing)