Project 2 – Week 4

In the past week we have looked to develop the tail further looking at how the wooden sticks could be hidden using a material other than paper. This solution would be to use some black tights and using its elastic material to also aid us in the retraction of the tail and to weave the peacock feathers in to. This black material will help us make the colourful feathers stand out in the model and give it a cleaner finish than just the wooden sticks.

IMG_5122

IMG_5123

 

 

 

 

 

 

 

We have also looked at adding switches to the code to allow the tail and head to move at different times depending on how far the user is away from the sensor. Witht he code we have managed to write out all the cases in which our code will have but we do not have the correct if statements in place as we are unsure on what they should be.

#include <Servo.h>
int servoPin = 9;
Servo servoMotor;

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 long variable for the distance

boolean distance = false;
boolean last = false;
boolean current = false;
int Cases = 0;

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(servoPin); //attach the servo to pin 9
}

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: "); //pirnt the value of the distance on the serial monitor
  Serial.println(distance);

  //------------------------cases-----------------------------
  current = distance;
if (
  cases();
}

void cases() {

  switch (Cases)
  {

    //30-40cm away
    //head moves back
    case 0:
      Serial.println(distance);
      servoMotor.write(30);
      break;

    //25-35cm away
    //tail moves up
    case 1:
      Serial.println(distance);
      servoMotor.write(80);
      break;

    //15-25cm away
    //tails spreads
    case 2:
      Serial.println(distance);
      servoMotor.write(80);
      break;

    //5-15cm away
    //lights flash
    case 3:
      Serial.println(distance);
      //blinking led
      break;

    //if current distance was 15
    //lights off
    case 4:
      Serial.println(distance);
      //lights off
      break;

    //if current distance 25
    //tail closes
    case 5:
      Serial.println(distance);
      for (int i = 0 ; i < 80 ; i += 2) // integer i is set at 0; if i <80; i + 2 degree every 10th of a second
      {
        servoMotor.write(i);
        delay(100);
      }
      break;

    //if current distance 35
    //tail moves down
    case 6:
      Serial.println(distance);
      for (int i = 0 ; i < 80 ; i += 2) // integer i is set at 0; if i <80; i + 2 degree every 10th of a second
      {
        servoMotor.write(i);
        delay(100);
      }
      break;

    //if current distance  40
    //head returns to original
    case 7:
      Serial.println(distance);
      for (int i = 0 ; i < 30 ; i += 2) // integer i is set at 0; if i <30; i + 2 degree every 10th of a second
      {
        servoMotor.write(i);
        delay(100);
      }
      break;

      //default:
      //  Serial.println(distance);
  }
}

Once we have learned this then the only thing left to do will be to put the model together and finally to test out the LED flashing for the flickering of the tail.

IMG_5120

IMG_5117

IMG_5121

IMG_5119

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