Objective

For our project, we have 2 robotic figures appearing to have a disagreement with one another through the repetitive motion of the robots shaking their heads. There is a taller “parent” robot and a smaller “child” robot. The child shakes its head once, as if simply refusing to listen to the parent. After the child’s expression of defiance, the parent immediately chastises the child, by shaking its head. The number of times the parent shakes its head and the angle in which it rotates its head is randomized to make it seem as if it is having an extensive conversation with the child.

The shaking of the parent’s head is activated by a switch, which is pressed every time the child shakes its head. Once the parent finishes its rotations, the servo motor controlling the child’s head will rotate away from the switch. Once the child’s head hovers over a photoresistor on the child’s body, its head will rotate again to strike the switch on the parent’s body.

Video

Code

#include <Servo.h&gt;
Servo myservo; 
Servo leverServo;
const int SERVO_PIN = 6;
const int SWITCH_PIN=7; 
const int LEVER_SERVO = 5;
const int LIGHT_SENSOR = 9;

bool leverDown = false;



/// Configure the hardware once after booting up.  This runs once after pressing
//// reset or powering up the board.
void setup(void)
{
  //servo 
  myservo.attach( SERVO_PIN);
  leverServo.attach(LEVER_SERVO);
  // Initialize the stepper driver control pins to output drive mode.


  // Start with drivers off, motors coasting.

  
  // Initialize the serial UART at 9600 bits per second.
  Serial.begin(9600);
}

void shakeHead()
{
  int shakingTimes=random(2,6); 
  int shakingAngle=random(0,90);
  for (int i=0; i<=shakingTimes; i++){
    myservo.write(0);
    delay(500);
    myservo.write(shakingAngle);
    delay(500);
    //Serial.println(shakingTimes);
  
  }
  leverServo.write(180);
  leverDown = false;

}


// ================================================================================
/// Run one iteration of the main event loop.  The Arduino system will call this
/// function over and over forever.
void loop(void)
{
  int sensorValue=digitalRead(LIGHT_SENSOR);
  Serial.println(sensorValue);
  if (sensorValue == 0){ 
    leverDown=true;
  }
  if (leverDown == true)
  {
    leverServo.write(0);
    if(digitalRead(SWITCH_PIN) == 0)
    {
      shakeHead();
      
    }
  }
  else if (leverDown == false)
  {
    leverServo.write(180);
  }
}
/****************************************************************/