Reacting to Sensors In this tutorial we will be going over how to program the Arduino to react to sensors. By the end of this workshop you will have an understanding of how to use sensors with the Arduino and create an interactive sculpture. This is the Arduino Uno: USB Port Power Socket Motor Shield Arduino This is the Arduino motor shield: Digital pins (0-13) Bread Board 5V Rail Arduino Proto Shield Ground Rail Analog pins (0 5) Digital pins (14 19) The Proto shield is a bare white board that gives us access to a bread board. It also has a 5V and GND rail. We can use these to easily wire up sensors without soldering. 1
Reacting to Sensors / Arduino IDE This is the Arduino Software, called the IDE (Integrated development environment) Verify: Check code for errors New: Open new file Save: Save current file Upload: Put code on to your Arduino Open: Open a saved file Serial Monitor: Read and write data to and from your Arduino Editor: Where you can type your code Console: Notifies you of any errors when compiling or uploading The line inside the editor that your cursor is on Type of Arduino and on what serial port it is connected to With this computer program you will be able to write code and upload it to the Arduino Uno. By following these steps you will have your Arduino driven sculpture set up in no time. All the while we encourage you to explore and play with the provided code 2
Reacting to Sensors / Starting your program Open the Arduino IDE that was illustrated earlier. An icon like the one on the right here should be on your desktop. If not, navigate to where programs are generally installed or ask your instructor for assistance. Once open navigate to File and Save As, giving your program a unique name. It can be ReactingToSensors, KnightRider or whatever you like. Before we continue navigate to Sketch and then Include Library. At the very bottom of the next drop down list you should find a library named Artbotics. If not please ask you instructor for assistance. The top left of the IDE, just above the Editor; you should see a tab with the name of your file. Underneath that at the top of the Editor you should see. This line imports the Artbotics library of commands to be used in your program. It gives you access to objects with prewritten code that will make driving the car easier. Make sure to go back and do the previous step if you don t see this or else you will run into trouble later on. 3
Reacting to Sensors / Button 1 Follow these steps to wire your button/ switch to your Arduuino Step 1 Step 2 Step 3 Step 1: Take the yellow wire and plug it into the same column on the breadboard as your switch and the other end into digital pin 7 Step 2: Take the red wire from the button and plug it into 5v Take the black wire from the button and plug it into the breadboard Step 3: Take the resistor and plug it into the same column on the breadboard as your switch and plug the other end into GND The line Button button(7) is an object that lets you use a button or switch. The number that is being passed (7) refers to the pin on the Arduino where the button will be plugged into. A Motor object was also added to control the motor on terminal A. Button button(7); void setup() void loop() 4
Reacting to Sensors / Button 2 Button button(7); Now update your void loop() function to match the code on the right. If statement If block void setup() void loop() if(button.getbuttonstate()) Inside the void loop() function is if(button.getbuttonstate()), this is referred to as an if statement. It creates conditions that determine when certain pieces of code will execute. If statements generally take on this form: if(boolean statement)code to execute if true Boolean statements are logic statements that can only be, true(high) or false(low). For example, if(3 > 2)True code to execute, 3 is obviously greater then 2, making this statement true, so the code within the curly braces will be executed. Any code following the if block will always be executed but code inside the if block (shown above within the curly braces ) will only execute when the if statement is true. The condition that is being used for the if statement is button.getbuttonstate(). It will return true if the button is being pressed, or false if the button is not being pressed. In the following code example, the code in the if block will be executed when the if statement reads that the button is being pressed. If the button is not being pressed the if statement will skip the code inside the if block. 5
Reacting to Sensors / Button 3 Update your code to match what is shown on the right. Button button(7); void setup() When the button is pressed a sequence of instructions can be written to rotate a motor with various duration, power, and direction settings. void loop() if(button.getbuttonstate()) motora.rotate(2, 2, HIGH); motora.rotate(2, 2, LOW); Duration Power Direction Now that you have your button wired and your code set up press the verify button to compile your code. Plug the USB cable into the Arduino, then press the upload button. When the IDE is finished uploading the program, unplug the USB cable from the Arduino and continue. While the program is running the motor should rotate twice when you press the button. 4 Follow these steps to wire your range sensor to the Arduino Plug the red wire from the range sensor into 5V, black into GND, and yellow into analog pin 2 (A2) 6
Reacting to Sensors / Range Sensor 5 To use a sensor (rather than a button) a value must be reached to trigger it. For example, a range sensor might give a value of around 400 when an obstacle is a foot away from its emitter. So in that case you would use 400 as the trigger value. To check the trigger value of your sensor go back to the IDE and create a new program. Go to File and New, then File and Save As, naming the file whatever you like. Make sure to include the Artbotics library under Sketch and Include Library. Update your code to match what is shown on the right. Sensor is another object in the Arbotics library, and it only requires one Argument, the analog pin 2 (A2). Analog pins are different from digital pins since they can take a range of values as opposed to digital which will only take LOW or HIGH. Sensor rangesense(a2); void setup() void loop() Update your code to match what is shown on the right. In the void setup() function, Serial is an object which will allow you to communicate with the Arduino. Serial.begin() is a function that sets up the Arduino to transmit data over the USB port. 9600 is the transfer rate for sending data. Sensor rangesense(a2); void setup() Serial.begin(9600); void loop() Update your void loop() function to match what s shown on the right. print is a common function used to print data out to a console. Serial can print anything to the console for example Serial.print( Hello World ); would print hello world each time the instruction was called. Instead print will print out the value from rangesense.getreading(); which is a function that will return an integer between 0 and 1023 based on the distance of an object in front of the sensor. Sensor rangesense(a2); void setup() Serial.begin(9600); void loop() Serial.print(rangeSense.getReading()); Serial.print("\n"); delay(500); delay() is just being used to keep the function call Serial.print from flooding the Serial console since it will be running in a loop very quickly otherwise. Note: Serial.print("\n"); is printing "\n", this is an Escape Character used to tell the console to start printing on the next line ( \n = new line, \t = tab, ) 7
Reacting to Sensors / Range Sensor 6 Press the verify button to compile your code. Plug the USB cable into the Arduino, then press the upload button. When the IDE is finished uploading the program leave the USB cable plugged into the Arduino and continue. With your code uploaded and the USB cable still plugged into the Arduino hit the Serial Monitor button. This will open a console that will start listing numbers every half second. The numbers correspond to the values of the range sensor. Note: Choose the 9600 baud, as that is what reflects in our code Place your hand in front of the sensor until it is about 1 foot away. Write down a rough estimate of the value that you see. It should be approximately 300. This number may vary depending on battery life. The number should increase as you move your hand closer, and decrease as you move your hand further away. Update your void loop() and void setup() functions to match what is shown on the right. In the if statement the boolean statement is rangesense.getreading() >= 300. Whenever the sensor reads a value greater than or equal to 300 it will return true and turn the motor on for 2 seconds at a time. If it continues to read a value of 300 or higher than the motor should continuously spin. #include<artbotics.h> Sensor rangesense(a2); void setup() voidloop() if(rangesense.getreading() >= 300) motora.rotate(2, 3, HIGH); Press the verify button to compile your code. Plug the USB cable into the Arduino, then press the upload button. When the IDE is finished uploading the program, unplug the USB cable from the Arduino and continue. Switch the power pack on the Arduino on and pass your hand over the sensor. The motor should turn on for at least 2 seconds after waving your hand over the sensor. 8
Driving and Drawing / Light Sensor 7 Follow these steps to wire your light sensor to the Arduino note: image not to scale Step 2: Take the red wire from the light sensor and plug it into 5v Take the black wire from the light sensor and plug it into the breadboard. Step 2 Step 3 Step 1: Connect black and red alligator clips to the light sensor s terminals. Step 3: Take the resistor and plug it into the same column on the breadboard as your light sensor and plug the other end into GND Step 4 Step 4: Take the yellow wire and plug it into the same column on the breadboard as your light sensor and the other end into Analog pin 2 Open a new file by going to File and New, then go to File again and Save As, typing whatever you like for the name. Update your code to resemble what is shown on the right. Sensor lightsensor(a2); is the object that will read values from the light sensor that you hooked up to analog pin 2 (A2). In the function void loop(), the print call on the Serial object will be used to read the values from the light sensor to find it s trigger value. #include<artbotics.h> Sensor lightsensor(a2); void setup() Serial.begin(9600); void loop() Serial.print(lightSensor.getReading()); Serial.print("\n"); delay(500); Press the verify button to compile your code. Plug the USB cable into the Arduino, then press the upload button. When the IDE is finished uploading the program unplug the USB cable from the Arduino and continue. 9
Driving and Drawing / Light Sensor 8 With the USB cable still plugged into the Arduino hit the Serial Monitor button. This will open a console that will start listing numbers every half second. The numbers correspond to the values of the light sensor. Note: Choose the 9600 baud, as that is what reflects in our code Now cover the sensor with your hand such that very little light is on it. Take a note of the approximate value you are reading and continue. The value should increase as it gets lighter and decrease as it gets darker. Update your void loop() and void setup() functions to match what is shown on the right. Replace 100 with the value you obtained on the previous step. Sensor lightsensor(a2); void setup() void loop() if(lightsensor.getreading() <= 100) motora.rotate(2, 150, HIGH); Press the verify button to compile your code. Plug the USB cable into the Arduino, then press the upload button Shown above. When the the Artbotics IDE is finished library uploading was not added the Missing program, to our #include code. unplug You <Artbotics.h> the will USB get cable an error from inside the Arduino the and continue. console that says car was not declared in this scope inside the void loop() function. Add to the top of your code. Remember, Car is an object or Switch prewritten the power code. In pack order of the to access Arduino that on code and cover we need the to light include sensor it. with using your #include hand. The motor should turn <libraryname.h> on and stay on for at least 2 seconds after removing your hand from over the sensor. 10