Project 2 – Week 5

In the past week we have looked to put the project together and create the shape of the model by testing out what materials can be used to create a good-looking and structurally sound machine. The body will be created by using a foam core egg, cutting it in half and then hollowing this out using wood then as a stable inner core for the PCB and Arduino to attach to.

File_000

File_001

The head then will be attached on a pivot point ready to be pulled back and forth on command. The tail itself will be attached on a hinge to the back of the body with a servo attached to it by a steel bar to pull this up and down. The gears then will be attached to this body to make the tail fan out.

The coding this week came almost to a stand still point. We wrote out the remainder of the code for the switch cases however this code is still not working and switching between the cases. This needs to be discussed with the tutor as each part we attempt to change seems to do nothing. We need to talk to the tutor in an attempt to figure this out as countless hours spent online researching have not helped as of yet.fritzing 2

#include <Servo.h>

int servoPin = 9;
Servo servoMotor;

int myServoPin = 10;
Servo myServo;

int theServoPin = 6;
Servo theServo;

int ledPin = 3;

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 last = false;
//boolean current = false;
int peacockSwitch = 0;
//int noCases = 0;
int last = 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
  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: "); //pirnt the value of the distance on the serial monitor
  Serial.println(distance);
  //--------------------------cases---------------------------------

  if (distance < 40 && distance < last)
    peacockSwitch = 0;
  Serial.println(distance);
  if (distance < 35 && distance < last)
    peacockSwitch = 1;
  Serial.println(distance);
  if (distance < 25 && distance < last)
    peacockSwitch = 2;
  Serial.println(distance);
  if (distance < 20 && distance < last)
    peacockSwitch = 3;
  Serial.println(distance);
  if (distance < 20 && distance > last)
    peacockSwitch = 4;
  Serial.println(distance);
  if (distance < 25 && distance > last)
    peacockSwitch = 5;
  Serial.println(distance);
  if (distance < 35 && distance > last)
    peacockSwitch = 6;
  Serial.println(distance);
  if (distance < 40 && distance > last)
    peacockSwitch = 7;
  Serial.println(distance);
  if (distance > 40)
    peacockSwitch = 8;

  switch (peacockSwitch)
  {

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

    //tail moves up
    case 1:
      Serial.println(distance);
      myServo.write(80);
      break;

    //tails spreads
    case 2:
      Serial.println(distance);
      theServo.write(80);
      break;

    //lights flash
    case 3:
      Serial.println(distance);
      //blinking led
      digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(500);                       // wait for 1/2 second
      digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
      delay(500);                       // wait for 1/2 second
      break;

    //lights off
    case 4:
      Serial.println(distance);
      //lights off
      digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
      break;

    //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
      {
        theServo.write(i);
        delay(100);
      }
      break;

    //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
      {
        myServo.write(i);
        delay(100);
      }
      break;

    //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;

    case 8:
      Serial.println(distance);
      servoMotor.write(0);
      theServo.write(0);
      myServo.write(0);
      break;

  }
}

Rob Harvey, Samuel Doubleday

Advertisements

One thought on “Project 2 – Week 5

  1. It looks like you r if statements need to have a range:
    instead of : if (distance < 40 && distance < last)
    you can type: if (distance 35 && distance < last) so it checks it is within a range

    Also are you sure the sensor reads in millimeters?

    Like

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