Project #13

This invention uses a distance sensor that triggers when someone walks past, setting off an alarm. You can use this to guard your bedroom, give someone a fright, or for actually scaring off real burglars! We'll also guide you through changing the code to make this invention your own.

 12 mins  
Difficulty: Moderate

01.

Parts & wiring

The first invention we’ll be building with the Annoyatron is a ‘ticking’ sound that people will think is coming from a clock. This is an especially good prank to try in rooms that don’t have a clock – people will get super-confused as they try to work out where the sound is coming from!

What you’ll need for this project

To build this invention, you’re going to need to the following parts, which you can find in your Annoyatron box. It’s a good idea to put them on the table in front of you so that you can find them easily.

Brain Board

Buzzer

Ultrasonic Sensor

4 x Coloured Wires

How to connect the parts

To connect your buzzer, slide its pins into the Arduino board as shown – the positive leg should go in PIN 11 and the negative leg should go in GND. 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 Brain Board.

Hover over the PLUS signs for extra tips

This is the positive leg of the buzzer. It will have a ‘+’ sign on top of it and will also be longer than the other leg.
Don’t forget to plug the board into your computer with the included USB cable.

Have you downloaded the Arduino software?

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

02.

Our sample code

You’ll see what this is for in just a moment

int distance() {
  int16_t array[3] = {};
  for(int count = 0; count < 2; count++){
    float duration, distance;
    digitalWrite(7, HIGH);
    delayMicroseconds(10);
    digitalWrite(7, LOW);
    duration = pulseIn(6, HIGH) / 2 * 0.0544;
    delay(20);
    array[count] = duration;
  }
  int measure = array[0] += array[1] += array[2];
  return measure / 3;
}
 
int repeat = 4;
 
void setup() {
  pinMode(7, OUTPUT);
  pinMode(6, INPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  if (distance() < 10) {
    for (int count = 0; count < repeat; count++) {
      digitalWrite(11, HIGH);
      delay(250);
      digitalWrite(11, LOW);
      delay(250);
    }
  }
}

03.

Let’s start coding

Like with previous projects, you’ll need to add and upload the sample code to your 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.

Forgotten how to do these steps? Click on the titles to expand.

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.

Change how far away the alarm triggers

In the program, the alarm is triggered when someone comes closer than 10cm of the ultrasonic distance sensor. Take a look at line 22 in the code where this happens:

if (distance() < 10) { // If something is closer than 10 cm. . .

If we change the code on line 22, then we make the trigger distance shorter or longer. For example, if we change the number 10 in that line to something like 22, anything that came within 50cm of the sensor would set off the alarm. Here’s what that would look like:

if (distance() < 50) { // If something is closer than 50 cm. . .

Change how long the alarm stays on

Once the alarm has been triggered by someone (or something) coming too close to the ultrasonic distance sensor, we can control exactly how long it stays on for afterwards. In our program, we set the alarm to beep ten times before resetting, ready to be triggered again. Take a look at line 13:

int repeat = 10; // Trigger the alarm for 10 beeps

By changing this code, we can either increase or decrease the number of beeps that the alarm stays on for when triggered. If we wanted it to stay on for 20 beeps, we would use the following code:

int repeat = 20; // Trigger the alarm for 20 beeps

Change the sound of the alarm

We can change the tone – or in other words, the sound – of the beeping using something called the tone() function. Take a look at the code on lines 24-27, which is what we are currently using for our beeps:

digitalWrite(11, HIGH);
delay(250);
digitalWrite(11, LOW);
delay(250);

To be able to change the tone of the beeps, replace lines 24-27 of your program with the following code:

tone(11, 1000, 500);
delay(1000);

The following code works like this: tone(buzzer, pitch, length). Replace the word pitch with any number you like, and this will control the sound of the beep. Replace the word length with exactly how long you want the beep to go for in milliseconds.

Here’s a different beeping sound that you can try out on your project:

tone(11, 5000, 750);
delay(1000);

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.