Page 224 - 15 Dangerously Mad Projects for the Evil Genius
P. 224

200       15 Dangerously Mad Projects for the Evil Genius


                                                           ■  monitorDuration determines how long in
          LISTING 15-1    (continued)
                                                              seconds the robot should stay still while
              digitalWrite(leftBPin, LOW);                    monitoring before it moves to a new location.
            }
                                                           ■  alarmDuration sets the period in seconds  the
            else if (rotation == CLOCKWISE)
            {                                                 alarm should sound once movement is
              digitalWrite(leftAPin, HIGH);                   detected. You will probably want to keep this
              digitalWrite(leftBPin, LOW);
                                                              short during testing!
            }
            else if (rotation ==                             The “setup” function initializes all these pins to
              COUNTER_CLOCKWISE)
                                                           the right mode. You will also see that we set
            {
                                                           posPin and negPin to 5V and GND, respectively.
              digitalWrite(leftAPin, LOW);
              digitalWrite(leftBPin, HIGH);                We are actually using those pins to supply power
            }                                              to the proximity sensor. This lets us use adjacent
           }
                                                           pins in the wiring, thus simplifying matters.
           void setRight(int rotation)                       One of the things we have to be very careful
           {                                               about is to prevent both the A and B control
            if (rotation == HALT)
                                                           signals to a motor controller from becoming high
            {
                                                           at the same time. If this happens, there will be
              digitalWrite(rightAPin, LOW);
              digitalWrite(rightBPin, LOW);                smoke and dead transistors! To prevent this, we
            }                                              limit the places in the sketch where we change
            else if (rotation == CLOCKWISE)
                                                           those pins to the functions setLeft and setRight.
            {
                                                           These functions set the direction of turning for a
              digitalWrite(rightAPin, HIGH);
              digitalWrite(rightBPin, LOW);                motor to be one of HALT, CLOCKWISE, or
            }                                              COUNTER_CLOCKWISE. If you always use
            else if (rotation ==
                                                           these functions, you can’t go wrong.
              COUNTER_CLOCKWISE)
            {                                                In a layer above these functions are another
              digitalWrite(rightAPin, LOW);                series of functions—halt, forward, backward, left,
              digitalWrite(rightBPin, HIGH);
                                                           and right—that control the direction of the robot as
            }
                                                           a whole rather than the individual motors.
           }
                                                             At the top of the heap, we have the “loop”
           void buzz(int duration)
                                                           function, which is invoked continuously. This first
           {
             digitalWrite(buzzPlusPin, HIGH);              calls the “monitor” function and then the
             delay(duration);                              moveToNewPlace function.
             digitalWrite(buzzPlusPin, LOW);
                                                             The “monitor” function repeatedly reads the
             delay(duration);
           }                                               analog input connected to the PIR sensor, and if it
                                                           is over the trigger threshold, it sets a timer and
                                                           turns on the buzzer. When the timer has expired,
        two sensors connected to analog inputs of the
                                                           the buzzer is turned back off again.
        Arduino.
                                                             The moveToNewPlace function invokes the two
           We then have a section of variables that we may
                                                           methods called turnInRandomDirection and
        be interested in tweaking to modify the behavior of
                                                           forwardOrProximity in turn. As their names
        our robot:
                                                           suggest, turnInRandomDirection rotates to the left
                                                           for a random period of between 100 and 3000
   219   220   221   222   223   224   225   226   227   228   229