I used a switch to receive info about where my rocker was. I put the switch in the center so that when the toy moves through the center, the switch will be pressed, and the toy will act accordingly. The servo moves from 0 to 135 to create the rocking motion. So, if the servo is at 0, when the switch is pressed, it will move to 135, and vice versa. I wanted this to create a motion that is smooth and satisfying, however the motion turned out to be choppy and not what I expected.

The choppy motion is due to the switch being pressed often – everytime the rocker passes through the middle. I tried making it so that the code ignores some of the times that the switch is pressed, but that resulted in the rocker not having enough energy to even pass through the middle due to the switch’s physical resistance.

Even though the motion wasn’t what I expected, I still think it’s fun and interesting to look at.

#include <Servo.h&gt; 

Servo hobby;
const int SWITCH_PIN  = 9;
int HOBBY_POSITION = 0;

void setup()
{
  hobby.attach(2);
  hobby.write(HOBBY_POSITION);
  delay(500); 
}

void loop()
{
    while(digitalRead(SWITCH_PIN) == 0); //wait until switch is pressed
    
    //when pressed, following will run
    if (HOBBY_POSITION == 0){ // moves the actuator to other side which rocks the piece
      hobby.write(135);
      HOBBY_POSITION = 135;
    }
    else{
      hobby.write(0);
      HOBBY_POSITION = 0;
    }
    
    delay(500); 
}