loading

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.


hey guys i cannot for the life of me get the libary to show up. when i look its in the folder of arduino lib but when i open ide and look in sketch include libaeies its nowhere to be found... am i missing sumthing?
<p>Hai Fjordcarver,</p><p>Congratulations, I love it , thanks for sharing </p><p>Its works fine ,good instructable , now wy co to the next step Part 2 </p><p>Kind regards</p><p>Sembot</p>
Hi! Congrats. <br>I would like to do this in my exhibition. <br>i couldnt download the mbrola voices because its not opening. I would like to get it downloaded soon to change the sound of my robo. <br> <br>Well your christmas tree was awesome dude.Iliked it so much! <br>Well thanks for the idea!
were did you get the rbbb
absolutely A W E S O M E ! ! !<br><br>(registered only to tell you)
Thanks so much!!
Nice, almost like they ones they use to make.
Thanks.
Hi - and you've made a great, fun Instructable. Congratulations.<br><br>I am using a Mac running 10.4 - a power pc processor.<br><br>I have Mbrola working just fine with the three voices. What I don't seem to have is the Mbrola Tools that are in your code on this page. Did that unzip for Windows? Do I need an extra step to get it for Mac?<br><br>The other code is working just great! Congratulations again.
Mbrola tools is the windows version of the binary install, so if the processing sketches are running you have what you need.<br>If you liked this I think you will like part 2, coming soon.<br>Vote for me and maybe one day I will be able to try it on a Mac too!<br>Cheers, thanks for the compliment!
Great work! <br><br>I'd like to put up a pointer posting about this over at Wired Magazine's GeekDad blog. Would it be okay if I were to borrow a couple of your photos for the posting? Email me at gmail.com as &quot;roy.wood&quot; if you'd like to discuss....
Please do, maybe link to the 'ible though. Share and enjoy. <br>If this is real and I see myself over on a blog at Wired.....well, it will certainly have me inspired to write up part two. Voice commands, feed reading and jokes.
Nicely done... but instead of a PC... you can get the Speakjet for the voice. You will have to putter around to get it to sound right... but it can also do sound effects and such. This way all you need is your small circuit board mounted in the base so it would be much more portable.<br><br>Speakjet: http://www.speakjet.com/<br><br>Another idea is to use the Radio Shack sound playback module... you would only have one phrase... but if triggered by an IR sensor when someone walks by... it could be a lot of fun.<br><br>Jerry
This is true, but I also have made the tree voice interactive. It tells me the time when I ask, the weather, and also tells jokes. I will be releasing an Instructable on how I have done this soon. Getting things working is quick, getting things cleaned up and organized to write about is more difficult. I have also been unsure as to how much to include.<br>Check the video on the last step if you want to see another animatronic I made telling jokes. It is a cool implementation in my opinion, as it is actually working with the person. So when it tells a knock knock joke, it is waiting for a response from you at each step.<br>I have wanted to play with a speakjet for a long time, and can think of many fun uses for one. To expensive for my blood though. I like to make things on the cheap or at least with what I already have.<br>check fjordcarver on youtube I have also done the IR sensor trigger with a zombie that says one of four things and activates a smoke machine.<br>Thanks for the input, great ideas that I will probably one day use to write another 'ible. <br>
love it!<br>I write for a blog called Geek Crafts, and I just included your awesome talking tree. <br>http://geekcrafts.com/robotic-christmas-tree/
Cool! Thanks!
See THIS is why Christmas Trees always scare me. You think they're nice and jolly but they're secretly watching you. Waiting....
....and they know when you have been naughty
Thanks for sharing this awesome project. Very good instructions. Have a great day!<br>Sunshiine
Share and enjoy!
I see a variable 'articulation' in you code with some numbers behind it. How does that work?
articulation is the string that is sent to the serial port. The Arduino interprets this and turns it into action. Example, if Arduino receives a 3, it will open the eyes. 7 turns the lights on, 6 off, 1-2 operate the mouth.<br>
Great project and a super instructable!
That looks awesome! Nicely done.
Thanks!!:)
This is great! I love it so much! I will have to make one, too late this year. I will build it for next Christmas!
Cheers, thanks so much!<br> By the new year i will have posted the other half, (I decided it was too much to include) speech recognition. I wanted to also show people how to use the minim library to play mp3s (Christmas songs) and have him animate with them.<br>I was struggling with where to end it. But I felt it was a complete lesson as is. We'll see how the masses judge it. <br>Post a picture on my 'ible if you do please. Still haven't had that happen. (a post of a project on one of mine)<br>I was very excited about this one....as you know
Cheers!
Hilarious! the voice is soooooo robotic, but I love it. what a great entry into the Advent Calendar.<br><br>Thanks for sharing. Great job!

About This Instructable

15,277 views

56 favorites

License:

Bio: Dad, maker, dreamer, hacker, painter.
More by fjordcarver: Make A 2D Video Game with Unity Acoustic/Electric Cookie Tin Unitar Make a Keepsake Book of your Child's Artwork
Add instructable to: