Arduino and LEGO Projects
202 pág.

Arduino and LEGO Projects


DisciplinaArduino508 materiais3.079 seguidores
Pré-visualização28 páginas
// resets counter if we run out of sensors
 if (counter == 3) counter = 0;
 
 // The PING))) 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[counter], OUTPUT);
 digitalWrite(pingPin[counter], LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin[counter], HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin[counter], LOW);
 
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
37
 // The same pin is used to read the signal from the PING))): 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[counter], INPUT);
 duration = pulseIn(pingPin[counter], HIGH);
 
 // convert the time into a distance
 inches = microsecondsToInches(duration);
 
 // moves the servo to the left if left sensor is triggered
 if (inches < 6 && counter == 0) {
 if (angle != left) {
 for (int i=angle; i>left; i--) {
 servo1.write(i);
 delay(spd);
 }
 angle = left;
 }
 
 // moves to the center if center sensor is triggered
 } else if (inches < 6 && counter == 1) {
 // moves from left to center
 if (angle < center) {
 for (int i=angle; i<center; i++) {
 servo1.write(i);
 delay(spd);
 }
 // or moves from right to center
 } else {
 for (int i=angle; i>center; i--) {
 servo1.write(i);
 delay(spd);
 }
 }
 angle = center;
 
 // moves to the right if right sensor is triggered
 } else if (inches < 6 && counter == 2) {
 if (angle != right) {
 for (int i=angle; i<right; i++) {
 servo1.write(i);
 delay(spd);
 }
 angle = right;
 }
 
 // otherwise hold steady at the current position
 } else {
 servo1.write(angle);
 }
 
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
38
 // send the value in inches to the Serial Monitor for each sensor
 Serial.print(&quot;Sensor &quot;);
 Serial.print(counter);
 Serial.print(&quot;: &quot;);
 Serial.print(inches);
 Serial.println(&quot; inches&quot;);
 
 // increment counter for the next loop
 counter++;
 
 // short delay before starting over again
 delay(100);
}
long microsecondsToInches(long microseconds) {
 // According to Parallax's datasheet for the PING))), 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.
 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
 return microseconds / 74 / 2;
}
The code for running the servo motors based on the ultrasonic sensors starts the same way as the code used for 
the Ultimate Machine. You include the libraries to run the motor and define the servo motor before defining the rest 
of the variables. Instead of lighting the LEDs when the ultrasonic sensors see an object, you put a value in the array 
defined as sensor. Arrays are defined by their name followed by square brackets, like variable[], and items in it can 
be addressed with numbers in the brackets. You assign the value zero to all three of the sensor values, so the sensors 
will start from a state where they have not yet seen anything. In the setup() function, you start the servo and then 
center the motor so it is looking straight ahead.
For this project, the head will have three positions (left, right, and center). If either of the side sensors is triggered, 
the motor will turn all the way to that respective side, but if the middle sensor is triggered, it needs to know which side 
it is facing in order to know which way to turn. Using if else statements, you check the array to see which sensors 
are triggered and then turn. When a sensor is triggered, it checks whether the last sensor triggered was the same one, 
and if not, it turns and sets the position variable for the next iteration of the loop. In the code, you have Serial.print
statements so you can make sure it all works when connected to the computer before building the Android.
Building the Android
Once you have the code and have tested it, you can start building the Android. The Android\u2019s body is made of 
concentric circles that rest atop each other, with a dome for the head. The arms and legs are made of smaller circular 
rings that are attached to the main body.
Start with the Foundation
Since the Android is round and LEGO bricks are not, you need to accommodate this by building into as round a shape 
as you can with the bricks. This is done by having a four stud side and having a two brick spacing to create a step 
pattern. You also have a brace four bricks wide running through the bottom of the Android to support the legs and the 
Arduino when you install them (Figure 2-8).
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
39
As when building the walls of the Ultimate Machine, you alternate the way the bricks are laid out in the rings for 
the Android\u2019s body. The center row will just have a row of 2 x 4 bricks over the center to lock in the brace. You will add 
another layer in the step after the one seen in Figure 2-9 underneath the brace to lock it in.
Figure 2-8. The first layer of the Android\u2019s body
Figure 2-9. The second layer of the Android\u2019s body locks the first layer in place
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
40
When looking at a picture of the Android, the bottom curves inward. You can approximate that look by having a 
layer beneath the first layer that brings the ring one stud in. The center brace must be reinforced by using an alternate 
layout that is four studs wide. Figure 2-10 shows what the ring looks like while Figure 2-11 shows what the ring looks 
like when installed on the bottom of the first ring.
Figure 2-10. The smaller ring goes beneath the first ring
Figure 2-11. The smaller ring placed beneath the two rings that were previously assembled
The next level of the body will hold the Arduino in place.
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
41
Building a Harness for the Arduino
A LEGO harness will be strong enough to hold the Arduino so that nothing else will be needed to hold it. While you 
already soldered the sensors into the motor shield, they won\u2019t be seen in the pictures until the steps in which you 
begin to place them for clarity when looking at the building process (see Figures 2-12 and 2-13).
Figure 2-12. The next level of the body creates a harness for the Arduino
Figure 2-13. The Arduino is placed into the cradle that was created for it
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
42
Adding a Level for the Power Plug
The next level of the Arduino body will create a space for the power plug connection. The side that has the connection 
is the back of the Android, so everything else you build for the body will use it as a point of reference. The plug can be 
seen in Figure 2-14.
Figure 2-14. The power plug is given a hole in the back of the Android as the fourth layer is added
Figure 2-15. The fifth layer, above the plug, locks it into place
Building the Body
In Figures 2-15 to 2-18, the Android\u2019s body is built up. The bricks continue to be alternated to build up strength in the 
walls as the Android grows taller. You lay more concentric rings atop each other, alternating the way they lie on top of 
each other in order to build a strong body that will be able to support itself.
www.it-ebooks.info
Chapter 2 \u25a0 Using sensors with the android
43
Figure 2-16. The sixth layer is built on top of the plug
Figure 2-17. A third layer, the seventh, is built on top of the plug, continuing to alternate