The final version of the “storybook” for today’s presentation

Here’s the final version of the “storybook” (i.e., an open source computer program) that was downloaded into an Arduino microcontroller for presentation as a desktop art sculpture (a/k/a dancebot):

/* Desktop art sculpture by Team Tree Trunk, November 2013
(Rick Hill, Janeil Hill and Jennifer Nye (with influence by Abi Leggette))

Created in conjunction with the Robot Hacks event sponsored by
MAKE magazine and GE, 3-20 November 2013.

This project was inspired by the work of Erin Kennedy, aka RobotGrrl.

Once upon a time, there were five photosensitive cells that wanted
to play together.

They were gathered inside a dark rectangular box when they felt
a commotion.

It was a two-eyed entity that called itself an ultrasonic sensor,
telling the five photosensitive cells that ultrasonic sensors are
like the bat or other creature that uses echolocation to describe
its  environment.

The six of them looked up and saw the big shadow and shape of
a giant that had lifted the lid of their box, exposing them to light,
showing them a world they had never seen before, full of odd
shapes and more entities just like themselves.

The giant removed them from the box and attached their legs into
holes that connected to rows of entities already in place, with
names like resistor, wire, LED and a complicated entity called
the Arduino.

The giant then played with them and gave them power.

They were more alive than ever before!

Excitedly, they joined with the wires, resistors, LEDs and Arduino
to move one-armed objects called servos.

Eventually, they left the rows of holes and were glued together
with metal to form a new network of connections inside a
cylindrical box.

The giant told them they were going to become a desktop art sculpture,
a Cubist version of a robot, dissected into its separate parts and put
on display like a Damien Hirst cadaver.

He walked them through a strange language, based on the idea of logic,
that made them more than the sum of their parts...

Five LDRs/photocells and one ultrasonic sensor are pointed outward
in the circular side wall of a hat box.

Each photocell controls the movement of a small analog servo
attached to the lid of the hat box.

The ultrasonic sensor controls the movement of large analog servo
attached to the lid of the hat box, which in turn is connected to
a set of pulleys.

If the distance from an object to the ultrasonic sensor is less than 5,
then all of the servos are returned to their original "zero" position;
otherwise they rotate based on readings from the photocells and ultrasonic sensor.

*/

int photocellPin0 = 0;     // the first photocell is connected to Arduino pin a0
int photocellPin1 = 1;     // the second photocell is connected to Arduino pin a1
int photocellPin2 = 2;     // the third photocell is connected to Arduino pin a2
int photocellPin3 = 3;     // the fourth photocell is connected to Arduino pin a3
int photocellPin4 = 4;     // the fifth photocell is connected to Arduino pin a4
int photocellReading;      // this is what we call the analog reading from the sensor
int yourDistance;          // this is what we call the analog reading from the ultrasonic sensor
int servoPosition;         // this is what we call the servo position between 0 and 180 degrees on a compass

#include <Servo.h> // add a virtual library book called Servo.h for reference
                   // by the logical sentences in this storybook

Servo myservo0;  // create servo object to control a servo on Arduino pin D3
Servo myservo1;  // create servo object to control a servo on Arduino pin D5
Servo myservo2;  // create servo object to control a servo on Arduino pin D6
Servo myservo3;  // create servo object to control a servo on Arduino pin D9
Servo myservo4;  // create servo object to control a servo on Arduino pin D10
Servo myservo5;  // create servo object to control a servo on Arduino pin D11

const int pingPin = 2; // tells the logical sentences that the ultrasonic sensor
                       // is connected to Arduino pin D2

// NOTE: anything that starts with a "/*" and ends with a "*/" is to be ignored
/*
const int closeD = 10; // cm; maximum closest distance - 0 to 10 cm range
const int midD = 20; // cm; maximum hand distance - 10 to 20 cm range
const int farD = 30; // cm; maximum farthest distance - 20 to 30 cm range
*/

int pos = 0;    // variable to store the servo position */

void setup(void) {
 myservo0.attach(3);  // attaches the servo 0 on pin 3 to the servo object
 myservo1.attach(5);  // attaches the servo 1 on pin 5 to the servo object
 myservo2.attach(6);  // attaches the servo 2 on pin 6 to the servo object
 myservo3.attach(9);  // attaches the servo 3 on pin 9 to the servo object
 myservo4.attach(10);  // attaches the servo 4 on pin 10 to the servo object
 myservo5.attach(11);  // attaches the servo 5 on pin 11 to the servo object

  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);

}

// This is the part of the logic that keeps the sensors and servo alive 
void loop(void) {

  // get the ultrasonic sensor raw distance in centimetres
  yourDistance = getDistance(pingPin); 

  // limit the reading from 0 to 50, ignoring smaller or larger numbers
  yourDistance = constrain(yourDistance, 0, 50); 

  // adjust the servo position (0 to 180 degrees) to match the ultrasonic readings (0 to 50)
  servoPosition = map(yourDistance, 0, 50, 0, 180);

  // tell the servo to move to the reading that was just called servoPosition 
  myservo5.write(servoPosition);

  // here's where all the servos are moved to their zero -0- position if
  // the distance from an object to the ultrasonic sensor is less than five -5-
  if (yourDistance < 5)
  {
     myservo0.write(0);
     myservo1.write(0);
     myservo2.write(0);
     myservo3.write(0);
     myservo4.write(0);
  }  

  // if the distance from an object to the ultrasonic sensor is five -5- or more
  // then we do something else;
  // in this case, as shown below, we record what the photocells are reading
  else
  {
  // we repeat this set of logical sentences five times

  //Repetition Number One
  // get the photocell sensor raw reading
  photocellReading = analogRead(photocellPin0);
  Serial.print("Analog reading 0 = ");  // send the phrase in quotes to the serial port
  Serial.println(photocellReading);     // send the raw analog reading to the serial port

  // limit the reading from 0 to 300, ignoring smaller or larger numbers
  photocellReading = constrain(photocellReading, 0, 300);

  // adjust the servo position (0 to 180 degrees) to match the ultrasonic readings (0 to 300)
  servoPosition = map(photocellReading, 0, 300, 0, 180);
  myservo0.write(servoPosition);

  //Repetition Number Two
  photocellReading = analogRead(photocellPin1);  
  Serial.print("Analog reading 1 = ");
  Serial.println(photocellReading);     // the raw analog reading
  photocellReading = constrain(photocellReading, 0, 300);
  servoPosition = map(photocellReading, 0, 300, 0, 180);
  myservo1.write(servoPosition);

  //Repetition Number Three
  photocellReading = analogRead(photocellPin2);  
  Serial.print("Analog reading 2 = ");
  Serial.println(photocellReading);     // the raw analog reading
  photocellReading = constrain(photocellReading, 0, 300);
  servoPosition = map(photocellReading, 0, 300, 0, 180);
  myservo2.write(servoPosition);

  //Repetition Number Four
  photocellReading = analogRead(photocellPin3);  
  Serial.print("Analog reading 3 = ");
  Serial.println(photocellReading);     // the raw analog reading
  photocellReading = constrain(photocellReading, 0, 300);
  servoPosition = map(photocellReading, 0, 300, 0, 180);
  myservo3.write(servoPosition);

  //Repetition Number Five
  photocellReading = analogRead(photocellPin4);  
  Serial.print("Analog reading 4 = ");
  Serial.println(photocellReading);     // the raw analog reading
  photocellReading = constrain(photocellReading, 0, 300);
  servoPosition = map(photocellReading, 0, 300, 0, 180);
  myservo4.write(servoPosition);
  }

  delay(300);  // wait about three tenths of a second to repeat everything in the loop
}

// NOTE: anything that starts with a "/*" and ends with a "*/" is to be ignored

/* boolean ping(int pingPin)
//boolean ping(int pingPin, int ledPin1, int ledPin2, int ledPin3)
{
  int d = getDistance(pingPin); // cm
  boolean pinActivated1 = false;
  boolean pinActivated2 = false;
  boolean pinActivated3 = false; 
  if (d < closeD) { 
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); 
    myservo5.write(180);
    pinActivated1 = true;
    pinActivated2 = false;
    pinActivated3 = false; 
  } else {
    if (d < midD) {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
      myservo5.write(90);
    pinActivated1 = false;
    pinActivated2 = true;
    pinActivated3 = false; 
    }
    else {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH); 
      myservo5.write(0);
    pinActivated1 = false;
    pinActivated2 = false;
    pinActivated3 = true; 
  }
  }
//  return pinActivated1, pinActivated2, pinActivated3;
  return pingPin;
} */

// This part of the logical sentences tells the ultrasonic sensor what to say.

int getDistance(int pingPin)
{
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();  
  return(cm); // You could also return inches
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
/*******************************************************************************/
//  This is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You could have received a copy of the GNU Lesser General Public
//  License along with this code; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/*******************************************************************************/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s