Project #3

Broken Night Light

Build and program a nightlight that automatically turns off whenever someone walks past! This invention will lead to lots of frustration as people try to navigate the darkness and bump into stuff on the way…

The Kit

What you’ll need

Computer

Brain Board

Mini Breadboard

Ultrasonic Sensor

USB Cable

Colour LED

8 x Coloured wires

Have you installed the coding software?

You’ll need to download the Arduino software to be able to code your invention. But don’t worry – click here for instructions.

Step 1

Let’s build this!

To wire up this project, you’ll need the Brain Board, USB cable, RGB LED and ultrasonic sensor. The longest pin of the RGB LED is the ‘negative’ pin, and this needs to be connected to GND on the board. The LED’s other 3 legs can be connected to the board’s pins as shown in the diagram below.

For the ultrasonic sensor, we will only be using 4 of its 5 pins in this project. Connect its ‘Vcc’ pin to 5V on the board, its ‘Trig’ to PIN 7, ‘Echo’ to PIN 6, and ‘GND’ to GND on the board.

Move the slider to see the breadboard wiring
Click to see a photo of the assembled invention

Step 2

Sample Code   

Step 3

Let’s start coding

Like with previous projects, you’ll need to add and upload the sample code to the Brain Board to get it to work. Forgotten how to do this? Click on the titles below to expand the instructions for each step of the process.

Connecting & setting up the Brain Board

1. Connect to your computer

The Amazing Annoyatron kit includes a mini computer that you can send code (instructions) to, called an ‘EduKits UNO’ board. We might also call it an ‘Arduino’ board because this is the program that we program it with. Start by connecting this board to your computer using the supplied USB cable. This will give the board power and will allow it to ‘talk’ to your computer. If the cable is connected correctly, you will see small lights flashing on the board.

Always select your Arduino board before uploading a program

2. Select your board

Every time you open the Arduino software, make sure that you select the option that says ‘Arduino/Genuino Uno’ from the ‘Tools’ menu.

Creating and saving a new 'code' document

Like in other programs you may have used like Microsoft Word, we need to create a new document to work in whenever we start something new. First launch the Arduino software that you downloaded earlier (if you didn’t, learn how to here) and select File > New in the menu at the top of the window.

You’ll notice that a new window will have popped up. It already contains some code, but we need to delete this because you will be pasting in some sample code that EduKits has written already. Select all the code using your cursor and then hit the ‘delete’ key on your keyboard to clear the existing code.

Next, select File > Save and give your project a name to save it to your account. This makes saving your work easier for later on.

Now you’re ready to upload the sample code!

Adding and uploading the sample code

Delete the existing code, then copy and past the code from our website. You can then upload the program to your board.

1. Add the sample code

We’re going to start off with some sample code, just so that you can see exactly what the project does. You can find this by scrolling down the page to section ’02’. Copy and paste the code from here into the blank program in the Arduino software.

2. Upload the program

Now that you’ve got your code, you can send it to your Arduino board. Find the button with a right arrow on it and click it to upload. If you haven’t already saved your document, a window will then pop up prompting you to do so. Type in a name like ‘Ticking Clock’ and then hit okay.

The program will ‘think’ for a moment, and then you will see lights flashing on the board very quickly for a few seconds before turning off. This means that the program has now been uploaded.

int triggerDistance = 20;
int confusionTime = 2;

int trigPin = 7;
int echoPin = 6;
int redPin = 9;      // Where the 'red' LED pin is connected
int greenPin = 10;   // Where the 'red' LED pin is connected
int bluePin = 11;    // Where the 'red' LED pin is connected
 
int i = 0;

void setup() {
  Serial.begin(9600); // Let the board talk to the computer
  for(int l=redPin; l<=bluePin; l++) { pinMode(l, OUTPUT); }
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, inches, cm;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  cm = duration / 29 / 2; // Calculate sensor distance

  Serial.print(cm); Serial.print("cm"); Serial.println();

  if(cm < triggerDistance) { i++; if(i>3) {
    analogWrite(redPin, 0);     // Turn the brightness to
    analogWrite(greenPin, 0);   // 0 for all of our LEDS
    analogWrite(bluePin, 0);    // (we set them up earlier)
    delay(confusionTime * 1000);
    i = 0; }
  } else {
    analogWrite(redPin, 100);    // Set the brightness
    analogWrite(greenPin, 100);  // of the LEDs to the
    analogWrite(bluePin, 100);   // max (100)
    i = 0;
  }

  delay(50);
}

04.

Make it more annoying

Chaotic coding

We’ll guide you through changing the code to make the project more annoying.

Connect to a battery

Learn how to connect your project to a 9V battery (not included).

Change how far away the light will turn off

In the program, the light turns off if someone comes within 20cm of our ultrasonic sensor. However, unless you are expecting your family members to run straight into the light, you will want to change this so that it triggers from further away. Take a peep at line 1 where the trigger distance for the sensor is set.

int triggerDistance = 20; // The trigger distance in cm

As the line of code above notes, the trigger distance value needs to be in centimetres. If we wanted our sensor to trigger from a metre away, we would need to use the following code, as 1 metre = 100 cm:

int triggerDistance = 100; // The trigger distance in cm

Change how long the light will turn off

In this program, there’s something I call the ‘confusion time’. This is basically how long we want to turn off the night light after someone has walked in front of them. In the demo program, this ‘confusion time’ is set to 2 seconds. This can be found on line 2 of the code.

int confusionTime = 2; // Turn the light off for 2 seconds

Don’t think that one second is enough? Want to cause more confusion? Not a bad idea, really. Let’s change it up a little so that now the light will turn off for 3 and a half seconds when triggered. We will need to use the following code:

int confusionTime = 3.5; // Turn the light off for 3 and a half seconds

How do I make this thing stop?!?

Click here to find out how to clear the program and make that super-annoying sound finally stop.

Connect your project to a battery

Once you’ve finished building and programming your Annoyatron project, you can connect it to a battery and take it with you. Make sure that you have disconnected the board from the computer using the blue USB cable and instead connect the included battery clip. You will also need a 9V battery (not included in the kit) to connect to the other end of the cable.

Make sure that the Brain Board is never connected to both the computer and the 9V battery at the same time. This could cause damage to both the board or your computer.

How do I actually make this thing stop?

1

Create a new document in Arduino

Open the Arduino software and select File > New in the menu or use the keyboard shortcut Ctrl + N to create a new document.
2

Upload the program to your board

Make sure that the correct board is selected in the tools menu and then click the upload button to send the 'blank' program to the board, overwriting the current one.