//Needed Library #include //Global Variables Servo boxMonster; //this is a needed declaration saying what our servo motor is to be called int box = 11; //this is the digital pin that we are sending data from the microcontroller to the servo motor int degree = 40; //this is a variable to keep track of what angle the motor is currently located at int Speed = 2; //this has a maximum speed value of 6, as per the physical limitations of the servo motor int closedMouthAngle = 20; //this has a minimum value of 0, as per both library and phyiscal limitations of the servo motor int openedMouthAngle = 130; //this has a maximum angle value of 180, as per both library and phyiscal limitations of the servo motor //////////////////////////////////////// // // // Setup function // // // //////////////////////////////////////// void setup(){ delay(10); //delay a little bit boxMonster.attach(box); //a library function to say which pin data is being sent out on delay(5); //another small delay of 5 milliseconds boxMonster.write(degree); //telling the Box Monster to go to this initial position (mostly closed mouth) delay(750); //delay for another 750 ms before going into our infinite loop function }//END of setup //////////////////////////////////////// // // // Loop function // // // //////////////////////////////////////// void loop(){ //open and close the mouth a little bit openMouth(closedMouthAngle+30); closeMouth(closedMouthAngle); openMouth(closedMouthAngle+30); closeMouth(closedMouthAngle); //open wide openMouth(openedMouthAngle); delay(500); //open and close our jaw a bit closeMouth(openedMouthAngle-40); openMouth(openedMouthAngle); closeMouth(openedMouthAngle-40); openMouth(openedMouthAngle); //delay in the open position delay(1000); //close our mouth again closeMouth(closedMouthAngle); delay(1250); }//END of loop; repeat the entire loop again ////////////////////////////////////////////////////// // // // User Defined Functions // // // ////////////////////////////////////////////////////// void openMouth(int angle){ while(degree < angle){ //while the current degree angle is less than the desired angle... degree = degree + Speed; //increase the degree angle by a set amount boxMonster.write(degree); //move the servo motor to that new angle delay(23); //delay a little bit to give motor time to move } }//END of openMouth void closeMouth(int angle){ while(degree > angle){ //while the current degree angle is greater than the desired angle... degree = degree - Speed; //decrease the degree angle by a set amount boxMonster.write(degree); //move the servo motor to that new angle delay(23); //delay a little bit to give motor time to move } }//END of closeMouth