10/03/2007

i hope you dance (quick sketch prototype)

using servo-motor to make him dance!
will continue...





video


code is here:
int servoPin = 13; // Control pin for servo motor
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 0; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

//int analogValue = 0; // the value returned from the analog sensor
int analogPin = 0; // the analog pin that the sensor's on
int val;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulse = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}

void loop() {
if(Serial.available())
{
val = Serial.read();
}
//analogWrite(servoPin, val);

//}
//analogValue = analogRead(analogPin); // read the analog input
pulse = val * 30 + minPulse; // convert the analog value
// to a range between minPulse
// and maxPulse.
Serial.println(millis() - lastPulse);

// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}


Serial.println(pulse);
//Serial.println(lastPulse);
}

No comments: