You are on page 1of 11

# Have-It-Your-Way Catapult Each team will have to construct a catapult with the objective of shooting as many balls as possible

into a bucket in 1 minute, which will be located 10 feet away. Each team will have access to: -1 laptop running Windows -1 Arduino Uno microcontroller -3 servos -3 servo holders -1 ultrasonic sensor -1 3 PVC pipe -wire clothes hangers -plywood -hot-glue gun and hot glue -nuts and bolts -USB-B cable -jumper cables -breadboards Please do not worry if you are not sure what some resources do/mean. We will explain how to use each one of them. But before we do that, we will explain some concepts that will be useful. What is coding? Coding is the process of communicating with the computer to tell it what to do. There are many computer languages used to solve problems across many STEM fields. For this activity, you will be using Processing, which is a language designed for beginners. Your code will tell the catapult when to release and when to reload. For this project, the computer that we will be talking to is the Arduino Uno microcontroller. Processing Language: Basics We will explore two very useful statements: if and while. These statements will allow you to implement almost any logic you want on your code. If Statement The if statement allows the program to make a decision about which code to execute. If the expression being evaluated is true, then the statements enclosed within the block are executed. Otherwise, the statements are not executed. int i=0; if (i==1){ i=2;

} if (i==0){ i=3; } In the code snippet above, the first if statement will not be executed because the statement i==1 is not true. On the other hand, the second if statement will be executed because the statement i==0 is true. Therefore, the value of i at the end of this program will be 3. While Statement The while statement allows you to execute a series of statements continuously while the expression is true. The series of statements are delimited with curly brackets {}, and the expression being evaluated is delimited with parenthesis. The expression must be updated during the repetitions or the program will never end. This is known as an infinite loop. int i = 0; while (i < 10) { i = i + 1; } In the code snippet above, the while statement will run until the expression i<10 becomes false, which will happen after the while loop runs 10 times. Now that we understand some of the building blocks for coding, lets go over the Arduino Uno microcontroller, which is where the code will be executed.

Arduino Microcontroller The Arduino Uno microcontroller will be the brain of your project. The Uno is powered through a USB cable when the cable is connected to a computer. The UNO will allow you to communicate with the servos and the ultrasonic sensor (more on this on the following sections).

Your team will be able to upload code to it using the Arduino Software environment, which is already installed in the laptop provided. To open the Arduino Software environment, click the windows icon and type Arduino in the search box. The Arduino Uno should be connected to the laptop via USB as illustrated in the picture below.

We will start with a simple program that makes an LED (Light Emitting Diode) flash. This program will help you understand better how the Arduino Uno and the Processing language works. First Arduino Program: Flashing LED

// 1. the LED is connected to the Arduino board through pin 13 int led = 13; // 2. initializing a variable // 3. the setup routine runs once when you press reset: void setup() { // 4. initialize the digital pin as an output pinMode(led, OUTPUT); } // 5. the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // 6. turn the LED on (HIGH is the voltage level) delay(1000); // 7. wait for a second digitalWrite(led, LOW); // 8. turn the LED off by making the voltage LOW delay(1000); // wait for a second }

The code snippet above makes an LED flash. We will explain each segment of the code individually. Two forward slashes (//) indicate the computer that the following text is meant for developers to read and it should not be interpreted by the computer. This is commonly known as a comment. 2 The word int indicates the computer that you need space in memory to store an integer. The word led is the name of the variable, i.e. the name of the space in memory given by the computer. You would be able to use this name to get whats inside the space in memory associated with this variable. In this case, the variable led will contain the integer 13 in it. 3 A function is a set of instructions that performs a specific task. In Processing as well as in many other languages, parenthesis are used to indicate the start of a function, e.g. setup(). The function setup() is used to initialize (define a
1

5 6 7 8

value) any variables at the beginning of a program. This function is executed automatically immediately after the Arduino is powered or has been programmed. The word void is reserved to indicate that the function will not return any kind of output. Brackets ({}) are used to indicate the beginning and ending of a function. In this example, we can see how the line pinMode(led, OUTPUT) is enclosed by the brackets of the function setup(). The line pinMode(led, OUTPUT) is calling the function pinMode() with the parameters (inputs given to a function) led and HIGH. The variable led was previously explained in step 2 and OUTPUT is a constant defined in the Arduino libraries. This function sets the pin 13 connected to the LED into output mode so we can turn it on/off. The function loop() will be called automatically after the setup() function is called. This function will be called repeatedly, hence the name. The function digitalWrite() changes of a pin to HIGH or LOW. In this case, we are changing the state of pin 13 to HIGH. This allows us to turn the LED on. The function delay(1000) tells the computer to wait for 1,000 milliseconds. This line tell the Arduino to turn the LED off.

Now, we will explore the functionality of two hardware components that will allow us to create a catapult.

Servo A Servo is a small device that incorporates a three wire DC motor, a gear train and an integrated circuit. Servos are commonly used in radio control vehicles, and, in this case, they will allow us to move the catapult.

How to Power a Servo There are three wires needed to control a servo motor: ground( black cable), positive (red cable) and data (yellow cable). To connect the Arduino and the servo motor, we need a bread board. Bread boards facilitate the creation of prototypes and allows fast experimentation with circuit design. Holes in the breadboard are separated vertically and connected horizontally. Connect the servo to the Arduino by inserting the servos black wire to any of the Arduinos GND (ground) pins; connect the red wire to the Arduino +5V pin. Then connect the yellow cable to pin 2. The picture below shows the connection.

Code Snippet
#include <Servo.h>// 1. imports the Arduino library called Servo.h Servo myServo; // 2. creates a variable to represent the servo void setup() { myServo.attach(2); // 3. attaches the servo to pin 2 } void loop() { myServo.write(90); // 4. rotates the servo to the original position delay(1000); myServo.write(135); // rotates the servo 45 degrees delay(1000); }

The code snippet above rotates the servo 45 degrees from the original position and then rotates back to the original position. Libraries are collections of functions that let us extend the basic functionality of a platform. The pound sign (#) followed by the word include allows us to import Arduino libraries. 2 We will be using the Servo library to interface with our motors. For each servo, we need to declare an instance of a Servo. This instance will represent the physical servo in our code. By indicating where the servo is connected to (step 3), the Arduino will be able to talk to the servo and command it to execute commands. 3 The function attach() will indicates the pin number that is connected to the servo. In this case, the pin number is 2. 4 The function write() tells the servo in which position it should be. The rotation is indicated in degrees.
1

Ultrasonic Sensor

Ultrasonic sensors generate high frequency sound waves and evaluate the echo, which is received back by the sensor. This sensor will allows us to determine the proximity of a specific object.

How to Power an Ultrasonic Sensor To power an ultrasonic sensor, we will need a breadboard. Insert the Ultrasonic Sensor in the middle of breadboard as indicated in the picture below.

Connect the hole next to the sensors pin labeled as GND to one of the Arduinos GND pins using jumper cables. Do the same with sensors +5v pin. The Ultrasonic sensor will receive power from the Arduino. The last leg of the sensor is marked with the

abbreviation SIG (for signal). This pin will be connected to the Arduino digital pin 7. Please see the picture below for more details.

Code Snippet
int pingPin = 7; void setup() { // 1. initialize serial communication: Serial.begin(9600); } long microsecondsToInches(long microseconds) { // 2. converts microseconds to inches return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { // converts microseconds to centimeters return microseconds / 29 / 2; } void loop() { // 3 . ultrasonic sensor is triggered by a HIGH pulse of 2 or more microseconds // give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5);

digitalWrite(pingPin, LOW); // The same pin is used to read the signal from the ultrasonic sensor: a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); // 4. convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); // 5. print the information in the console Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); }

This sketch reads a PING))) ultrasonic range finder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor.
1

4 5

This line initializes serial communication at 9,600 bits per second. The baud rate (bits per second) allows two devices to communicate. If the two devices had different baud rates, it will be like two people talking in different languages. The functions microsecondsToInches() and microsecondsToCentimeters() allows us to interpret the information provided by the Ultrasonic sensor. There are 73.746 microseconds per inch (i.e. sound travels at 1130 feet per second). This gives the distance travelled by the ping, outbound and return, so we divide by 2 to get the distance of the obstacle. This code segment is used to determine how far away an object is. You are free to just copy and paste this segment. For more information, read the comments. The functions microsecondsToInches() and microsecondsToCentimeters() are called and their ouputs are stored in inches and cm respectively. Serial.print() prints information to the console of the Arduino Development Environment.

Building a Catapult

This section will give you pointers for how to build your catapult. You do not need to follow these pointers. You are allowed to load the balls manually, but do you think there is faster way to do this? How can you tell the Arduino whether there is a ball ready to be fired? How can you load the balls in a way that does not mess with the aiming?