Instructables

Animatronic Talking Christmas Tree - Part 1

Featured

Step 11: Program the Arduino

Now that you understand how I am operating the servos, let's write some more useful code for it.

We will start by defining a bunch of functions using the values that we recorded in the last step that will animate the tree, and also add serial communication capability and program some actions to go along with data that is received from the serial port.

Open up a new sketch in the Arduino environment and enter the following. (remember that you will need to use your own values for the servo positions)
int ledPin = 13;    //assign pin 13 as the pin that the lights are on
int mouthPin = 8;    //this is the pin where the mouth is connected
int eyesPin = 9;    //eyes are on pin 9

//for serial communication
int message = 0;
                    
void setup(){
  Serial.begin(9600);   //start the serial port with a baud rate of 9600
  //define the outputs
  pinMode(ledPin,OUTPUT);
  pinMode(mouthPin,OUTPUT);
  pinMode(eyesPin,OUTPUT);
  //close the eyes
  closeEyes();
  //close mouth
  closeMouth();
  //wait a few seconds so we can witness the opening after hitting reset
  delay(3000);


void loop(){
//digitalWrite(ledPin,HIGH);
//openEyes();
//talk(3);
if(Serial.available()>0){  //if the serial port is sending data
    //read the incoming byte
    message = Serial.read();
  //animate our robot according to the serial data that we recieve
    if(message == '1'){
      talk(1);
    }
    if(message == '2'){
      talk(2);
    }
    if(message == '3'){
      openEyes();
    }
    if(message == '4'){
      closeEyes();
    }
    if(message == '5'){
      blinkEyes();
    }
    if(message == '6'){ 
      digitalWrite(ledPin,LOW);
    }
    if(message == '7'){ 
      digitalWrite(ledPin,HIGH);
    }
  }
}

void talk(int times){
  for(int j = 0;j < times;j++){
  //first we open the mouth
for(int i = 0;i <10;i++){
  digitalWrite(mouthPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(1100);  //delay for the pulse length set in declarations
  digitalWrite(mouthPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}
   //then close it
for(int i = 0;i <10;i++){
  digitalWrite(mouthPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(500);  //delay for the pulse length set in declarations
  digitalWrite(mouthPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}
}
}

void closeMouth(){
   for(int i = 0;i <10;i++){
  digitalWrite(mouthPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(500);  //delay for the pulse length set in declarations
  digitalWrite(mouthPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}
}

void openEyes(){
   for(int i = 0;i <30;i++){
  digitalWrite(eyesPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(800);  //delay for the pulse length set in declarations
  digitalWrite(eyesPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}

}

void closeEyes(){
   for(int i = 0;i <30;i++){
  digitalWrite(eyesPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(2500);  //delay for the pulse length set in declarations
  digitalWrite(eyesPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}
}

void blinkEyes(){
  for(int i = 0;i <10;i++){
  digitalWrite(eyesPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(2500);  //delay for the pulse length set in declarations
  digitalWrite(eyesPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}
  for(int i = 0;i <10;i++){
  digitalWrite(eyesPin,HIGH);  //turn on the servo pin, i.e send 5V
  delayMicroseconds(800);  //delay for the pulse length set in declarations
  digitalWrite(eyesPin,LOW);  //turn servo pin back off to end the pulse
  delay(20);
}
}

Here is a video of what it should do at this point.


 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!