Lab 4: Stupid Pet Project (Rudi)

1. Design

As my stupid pet project, I think it would be fun to demonstrate myself in the morning: not able to get up easily. It is a feeling which everyone would be able to understand at least a little bit. So I planned to make a house that lights up when knocked, and then a gorilla doll with a fan as its umbrella comes out of the ceiling. After few seconds of delay, the gorilla goes back to the house, back to its bed–too bad, you failed in walking him up for school! To do so, I needed a vibration sensor, an LED and some motion and motor that reacts to the input from the vibration sensor. But I realized that it would be hard to make the gorilla slowly come upwards, I changed to a servo that will work as a blade of a mill and slowly rotate the gorilla to appear.

2. Building the Circuit

It was not hard to build the circuit. I used a vibration sensor, an LED, motor and a servo. They all fitted into one breadboard.

I added Button example, Servo Sweep example, and a self-written motor code to the Sensors Knock example from Arduino. I added the functions one by one, checking if each one works. It all worked.

The first problem I encountered was that the power is not enough to run both the servo and the motor. Once the motor runs, the servo shakes but does not rotate. So I adjusted to arrangement of the code so that the servo turns, then the motor runs, and then the servo comes back to the original state. By adjusting the order, the problem of the power was solved.

However, the second problem was that servo shows malfunction. The servo moved from 0 degrees to 90 degrees as I told it to, but once the motor runs, it returns to 0 degrees, then when the motor stops,  the servo moves back to 90 degrees to slowly turn from 90 degrees to 0 degrees.

I returned to the Servo Sweep example of Arduino, but when I ran this example, the servo did not show any malfunction. If I try with the same code above but with the motor disconnected, the servo did not show any malfunction either.


Therefore, I tried several different ways to solve it.
I first separated the motor to a separate external power (5V). But it didn’t work.
Then I included the running and exiting of the motor inside the for loop of the servo turning from 0 degrees to 90 degrees. This resulted in the servo moving only once but the motor repeating turning on and off, not moving on to the second for loop.  (Nicholas helped)
So I added printIn to every function (Knock, moving servo, motor on, motor off, moving servo back, waiting) to check if there is any problem showing on the serial monitor. It did not show anything weird. I checked with the same code but with motor disconnected, and the servo worked fine even though the serial monitor shows motor on and motor off, telling me that it’s running the same code. (Rudi helped)
Adding a potentiometer to the motor also didn’t work.
I decided I could not use the motor.

The third problem was that the loop would continue without a new vibration input once the code is initiated by the initial input. So I added else {} at the end, which stopped any function unless there is an input.

Fixing the circuit, I was able to learn that the order and exact nomenclature of the code is very important, and also that I can create the exact same result with different codes. When the code worked as I thought it would, or I was able to fix a problem I faced, it was very satisfying and motivating.

With the problems solved (or not), I attached a cardboard to the wing of the servo and tied gorilla to the cardboard using cables. The fan was going to be its flying backpack but sadly gorilla had to be alone.

3. Presentation

I named the work “Wake up for school!”. When you knock, the LED beside the door lights up and the gorilla wakes up. But after few seconds, it goes back to sleep. You failed in waking it up.

I originally set the delay after the gorilla goes back to sleep as 5000ms, but the audience all tried to wake the gorilla up two or three times after failing once. They had to wait too long to wake the gorilla up so I changed the delay to 1000ms.

4. Suggestions

I got two suggestions while presenting my work:

  • put the vibration sensor at the door so that the audience knock on the door instead of a separate paper
  • make the door open with the gorilla coming up
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

// these constants won't change:
const int ledPin = 13;      // LED connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not
const int motorSensor = 9; // motor connected to digital pin 9

// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light
int pos;    // variable to store the servo position

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  Serial.begin(9600);       // use the serial port
  pinMode(sensorReading, INPUT); // declare value read from the sensor pin as input
  pinMode(motorSensor, OUTPUT); // declre the motor as an OUTPUT
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    // send the string "Knock!" back to the computer, followed by newline

    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(300); // waits 300ms

    Serial.println("moving servo!");
    for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);   // waits 15ms for the servo to reach the position
    digitalWrite(ledPin, LOW); // turn the LED off
    delay(500); // waits 500ms

    Serial.println("motor on!");
    digitalWrite(motorSensor, HIGH); //turn on the motor
    Serial.println("motor off!");
    digitalWrite(motorSensor, LOW); // turn off the motor

    Serial.println("moving servo back!");
    for (pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position

  else {

Leave a Reply