Step 11: Program the Arduino
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 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.
robottree.txt3 KB
Remove these ads by
Signing Up






























Not Nice
















Visit Our Store »
Go Pro Today »



