Cuckoo Clock

justinwang's picture

Cuckoo Clock

Assignment: Put the door and bird together so that the bird sticks its head out when the door opens and goes back as the door closes. Once you get the mechanics to work, try to control it from Arduino.

Pictures:

Attached.

Arduino Code:


/* Cuckoo Routine
 * Theory and Practice of Tangible User Interfaces
 * 28 October 2011
 * Martin, Sebastian, Hilfi, Margaret, and Justin
 *
 * Make cuckoo bird come out to indicate what "time" it is. The bird only comes out if time changes.
 */
 
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
// pins
int servoPin = 7;      // control pin for servo motor
int potPin = 0;        // select the input pin for the potentiometer 
 
// variables
int pos = 0;           // variable to store the servo position
int delayTime = 3;    // variable to control speed of movement
int val = 0;           // variable to store the pot position
int oldCounter = 0;    // variable to store previous counter
int newCounter = 0;    // variable to store updated counter
 
void setup() 
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);        // connect to the serial port 
 
 
void loop() 
  val = analogRead(potPin);
  val = map(val, 0, 1024, 1, 12); // map potentiometer
  newCounter = val;
  
  if(newCounter!=oldCounter)      // make sure "time" position has moved
  {
    for(int i = 0; i < newCounter; i++)    // do servo cycle for certain number of times
    {
      for(pos = 0; pos < 180; pos+=1)      // goes from 0 degrees to 180 degrees 
        {                                  // in steps of 1 degree 
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(delayTime);                
        } 
        for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
        {                                
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(delayTime);               
        }
    }
    Serial.print(oldCounter);
    Serial.print(" ");
    Serial.println(newCounter);
    
    delay(2000); // delay 2000 ms
    
    oldCounter = newCounter;               // set oldCounter to newCounter after servo cycles
  } 
}

 

Goose
Clock
0
Your rating: None