Project 2 – Week 6

During this past week, we have worked mainly on the prototype to get it work and made to a high standard of manufacture. We have mainly been focused on attaching the mechanisms that have been worked out during the project to the egg shape we have. After we managed to do that we began ‘beautifying’ the peacock. What we did here was add the feathers to the tail, attach the blue felt to the body. We have also made some legs for the peacock to make it look more alive.

File_000.jpeg

File_001

File_002

With the code this week we added in a ‘IF’ statement before each of the for loops in the last case. The reason for this is because us for loops we had were just constantly looping as they didn’t know what angle the servo was at. So, what we did was add an IF statement before each for loop to check the angle of the servo, then if the angle of the servo corresponds to the angle we have written in the IF statement it will then proceed with the for loop. To make the HC SR04 sensor visible and in a position where it can detect the movement of the hand closer to the sensor we have soldered some wires to it so that it can be placed on the base of our model.

#include <Servo.h> //include servos

int servoMotorPin = 9; //define servoPin
Servo servoMotor; //name the servo

int myServoPin = 10; //define myServoPin
Servo myServo; //name the servo

int theServoPin = 6; //define theServoPin
Servo theServo; // name the servo

int ledPin = 3; // define led pin

const int trigPin = 12; //defining the pins
const int echoPin = 13;

long duration; //this is the long variable for the travel time
int distance;   // the initiation for the distance

int peacockSwitch = 0; // intiating peackcockswitch to zero
int last = 0; // initiating last to zero

void setup() {
  Serial.begin (9600); //start the serial communication
  pinMode(trigPin, OUTPUT); //pin 12 is the output of the sensor
  pinMode(echoPin, INPUT);  //pin 13 is the input of the sensor
  servoMotor.attach(servoMotorPin); //attach the servo to pin 9
  myServo.attach(myServoPin); //attach the servo to pin 10
  theServo.attach(theServoPin); //attach the servo to pin 6
  pinMode(ledPin, OUTPUT); //pin 3 is the output of the LED
}

void loop() {
  //-----------------------sensor setup------------------------------------
  digitalWrite(trigPin, LOW); //to make the sensor read as low
  delayMicroseconds(2); //delay 2 microseconds

  digitalWrite(trigPin, HIGH); //to generate the ultrasound wave
  delayMicroseconds(10); //stay high for 10 microseconds
  digitalWrite(trigPin, LOW); //the set the trig pin back to low so the signal isnt constantly sent

  duration = pulseIn(echoPin, HIGH); //read the travel time of the soundwave and put that value in the variable duration
  distance = duration * 0.034 / 2; //multiply the duration by the speed of sound

  Serial.print("Distance: "); //print the value of the distance on the serial monitor
  Serial.println(distance);
  //--------------------------cases---------------------------------
  if (distance == last) {
    digitalWrite(trigPin, LOW); //to make the sensor read as low
    delayMicroseconds(2); //delay 2 microseconds

    digitalWrite(trigPin, HIGH); //to generate the ultrasound wave
    delayMicroseconds(10); //stay high for 10 microseconds
    digitalWrite(trigPin, LOW); //the set the trig pin back to low so the signal isnt constantly sent

    duration = pulseIn(echoPin, HIGH); //read the travel time of the soundwave and put that value in the variable duration
    distance = duration * 0.034 / 2; //multiply the duration by the speed of sound

    Serial.print("Distance: "); //print the value of the distance on the serial monitor
    Serial.println(distance);
  }
  if (distance != last) {
    if (distance < 50 && distance >= 40 && distance < last) // if distance is less than 60, greater or equal to 50 and less than last
      peacockSwitch = 0;

    if (distance < 40 && distance >= 30  && distance < last) // if distance is less than 50, greater or equal to 35 and less than last
      peacockSwitch = 1;

    if (distance < 30 && distance >= 20 && distance < last)// if distance is less than 35, greater or equal to 20 and less than last
      peacockSwitch = 2;

    if (distance < 20 && distance >= 10 && distance < last)// if distance is less than 20, greater or equal to 5 and less than last
      peacockSwitch = 3;

    if (distance > 55) // if distance is greater than 65
      peacockSwitch = 4;
  }
  switch (peacockSwitch)
  {
    //head moves back
    case 0:
      Serial.println(distance);
      Serial.println("|");
      Serial.println(last);
      servoMotor.write(30);
      delay(1000);
      break;
    //tail moves up
    case 1:
      Serial.println(distance);
      Serial.println("|");
      Serial.println(last);
      myServo.write(80);
      delay(1000);
      break;
    //tails spreads
    case 2:
      Serial.println(distance);
      Serial.println("|");
      Serial.println(last);
      theServo.write(80);
      delay(1000);
      break;
    //lights flash
    case 3:
      Serial.println(distance);
      Serial.println("|");
      Serial.println(last);
      //blinking led
      digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(50);                       // wait for 1/2 second
      digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
      delay(50);                       // wait for 1/2 second
      break;
    case 4:
      Serial.println(distance);
      Serial.println("|");
      Serial.println(last);
      if (theServo.read() == 80)
        for (int i = 80 ; i > 0 ; i -= 1) // integer i is set at 0; if i 
        {
          theServo.write(i);
          delay(20);
        }
      if (myServo.read() == 80)
        for (int i = 80 ; i > 0 ; i -= 1) // integer i is set at 0; if i 
        {
          myServo.write(i);
          delay(20);
        }
      if (servoMotor.read() == 30)
        for (int i = 30 ; i > 0 ; i -= 1) // integer i is set at 0; if i 
        {
          servoMotor.write(i);
          delay(20);
        }

      digitalWrite(ledPin, LOW);
      delay(1000);
      break;
  }
  last = distance; // reset so that the last = distance
}

Final diagram_bb.jpg

We have also produced two presentation boards ready for the exhibition in the atrium. The first board was produced on A2 paper, this has the flow chart of the code displayed on it, the circuit drawn in fritzing and the final images of the model. On the second board, we have produced a ‘how to’ guide for our model so that anyone who wants to use our product can do so.

All in one - Page 1.jpeg

Samuel Doubleday, Rob Harvey

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