{\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf420 {\fonttbl\f0\fswiss\fcharset77 Helvetica;} {\colortbl;\red255\green255\blue255;} \margl1440\margr1440\vieww9000\viewh8400\viewkind0 \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural \f0\fs24 \cf0 /*\ * Servo with Potentiometer control\ * Theory and Practice of Tangible User Interfaces\ * October 11 2007\ */\ \ int servoPin1 = 7; // Control pin for servo motor\ int servoPin2 = 8;\ \ int potPin = 0; // select the input pin for the potentiometer\ int potPin2 = 1;\ \ int pulseWidth1 = 0; // Amount to pulse the servo\ int pulseWidth2 = 0;\ long lastPulse = 0; // the time in millisecs of the last pulse\ int refreshTime = 20; // the time in millisecs needed in between pulses\ int val; // variable used to store data from potentiometer\ int val2;\ \ int minPulse = 500; // minimum pulse width\ \ void setup() \{\ pinMode(servoPin1, OUTPUT); // Set servo pin as an output pin\ pinMode(servoPin2, OUTPUT);\ pulseWidth1 = minPulse; // Set the motor position to the minimum\ pulseWidth2 = minPulse;\ Serial.begin(9600); // connect to the serial port\ Serial.println("servo_serial_better ready");\ \}\ \ void loop() \{\ val = analogRead(potPin); // read the value from the sensor, between 0 - 1024\ val2 = analogRead(potPin2);\ \ if (val > 0 && val <= 999) \ \{\ pulseWidth1 = val * 2 + minPulse; // convert angle to microseconds\ \ Serial.print("moving servo to 1");\ Serial.println(pulseWidth1,DEC);\ updateServo(servoPin1, pulseWidth1); // update servo position\ \}\ \ if (val2 > 0 && val2 <= 999)\ \{\ pulseWidth2 = val2 * 2 + minPulse;\ Serial.print("moving servo to 2");\ Serial.println(pulseWidth2, DEC); \ updateServo(servoPin2, pulseWidth2);\ \}\ \}\ \ // called every loop(). \ void updateServo(int servoPin, int pulseWidth) \{\ // pulse the servo again if rhe refresh time (20 ms) have passed:\ if (millis() - lastPulse >= refreshTime) \{\ digitalWrite(servoPin, HIGH); // Turn the motor on\ delayMicroseconds(pulseWidth); // Length of the pulse sets the motor position\ digitalWrite(servoPin, LOW); // Turn the motor off\ lastPulse = millis(); // save the time of the last pulse\ \ \}\ \}\ \ }