// flex sensor with servo by Alexander Reeder // Oct 1 2007 int servoPin = 2; // 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 int modPulse = 800; int diffPulse = 0; int prevPulse = 0; 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 threshold = 20; 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() { analogValue = analogRead(analogPin); // read the analog input pulse = analogValue+200; diffPulse = pulse - prevPulse; if (diffPulse < 0) diffPulse = diffPulse*-1; prevPulse = pulse; // pulse the servo again if rhe refresh time (20 ms) has passed: if (millis() - lastPulse >= refreshTime) { if (diffPulse > threshold) pulse = analogValue+200+((pulse%8)*5); 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 } }