Fancy Mr. Rex

 
In a time long long ago there was a dinosaur who was quite unlike the rest.
For he wore something peculiar on his chest.
His name was Fancy Mr. Rex
He didn’t have big muscles to flex
Rex had pondered for ages what to do,
It was Tyra he did pursue
He couldn’t figure out how to impress
So he decided to class up his dress
It took him fortnights to decide,
It was the fanciest of bows he tied.

The above poem gives more insight into the story behind Fancy Mr. Rex and why he wears a bow tie. The following processing sketch is the origin of Fancy Mr. Rex.
Processing Sketch: http://www.openprocessing.org/sketch/68556

If I were to expand this piece/improve it, I would make the background move one a track so as to appear that it is scrolling. I would also attach Rex to some main support that would hold up his wait so that his weight wasn’t rested on the servo motors. This would also for smoother more realistic motion. Also some of the directly attached servo motors would be better done with pulley systems.

Here is the Fritzing Diagram:

Here is the code for ze fancyest of dinos:

#include    // servo library
Servo servo1;  // servo control object
Servo servo2;  // servo control object
Servo servo3;  // servo control object
Servo servo4;  // servo control object
//Servo servo5;  // servo control object

const int buttonPin = 2;
boolean onOffSwitch = false;

//-----WALK SETTINGS-----
int walkState = 1;
int walkStateNum = 5;
int waitTime = 150;

void setup()
{
  pinMode(buttonPin, INPUT);

  walkState = 1;

  servo1.attach(5);  //---jaw
  servo2.attach(6);  //---leg behind
  servo3.attach(9);  //---leg infront
  servo4.attach(10); //---arms
  //servo5.attach(11); //---extra arm

  onOffSwitch = true;
  normalMode ();
}

void loop()
{
  //-----Check Button State-----
  int  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW)
  {
    onOffSwitch = -onOffSwitch;
  }

  if (onOffSwitch)
  {
    walkMode();
  }
  else
  {
    normalMode();
  }
}

void normalMode ()
{

}

void walkMode ()
{
  //-------SERVO CODE-------
  if (millis() % waitTime == 0)
  {
    walkState = (walkState + 1) % walkStateNum;
    updateJaw(walkState);
    updateLegBehind(walkState);
    updateLegInFront(walkState);
    updateArms(walkState);
  }
}

void updateJaw(int walkS)
{
  int positionSet;
  if (walkS % 3 == 0)
  {
    positionSet = map(random(0, 100), 0, 100, 110, 160);
  }
  else if (walkS % 3 == 1)
  {
    positionSet = map(random(0, 100), 0, 100, 110, 160);
  }
  else
  {
    positionSet = map(random(0, 100), 0, 100, 110, 160);
  }

  servo1.write(positionSet);  // Move to next position
}

void updateLegBehind(int walkS)
{
  int positionSet;
  if (walkS % 3 == 0)
  {
    positionSet = map(random(0, 100), 0, 100, 110, 160);
  }
  else if (walkS % 3 == 1)
  {
    positionSet = map(random(0, 100), 0, 100, 110, 160);
  }
  else
  {
    positionSet = map(random(0, 100), 0, 100, 110, 160);
  }

  servo2.write(positionSet);  // Move to next position
}

void updateLegInFront(int walkS)
{
  int positionSet;
  if (walkS % 3 == 0)
  {
    positionSet = map(random(0, 100), 0, 100, 110, 120);
  }
  else if (walkS % 3 == 1)
  {
    positionSet = map(random(0, 100), 0, 100, 60, 80);
  }
  else
  {
    positionSet = map(random(0, 100), 0, 100, 0, 30);
  }

  servo3.write(positionSet);  // Move to next position
}

void updateArms(int walkS)
{
  int positionSet;
  if (walkS % 3 == 0)
  {
    positionSet = map(random(0, 100), 0, 100, 130, 140);
  }
  else if (walkS % 3 == 1)
  {
    positionSet = map(random(0, 100), 0, 100, 140, 160);
  }
  else
  {
    positionSet = map(random(0, 100), 0, 100, 160, 180);
  }

  servo4.write(positionSet);  // Move to next position
}

Post a comment