Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


Fourth Week

What's a servo?

Places to buy equipment

Wikipedia on decoupling capacitors

resistor calculator for LEDs

Fade LED in and out

int LEDPin=10;
int Val = 1;
int Changer = 1;

void setup(){
  pinMode(LEDPin,OUTPUT);
}

void loop(){  
  if(Val==255||Val==0){
    Changer=-Changer;
  }
  analogWrite(LEDPin, Val);
  Val=Val+Changer;
  delay(30);
}

Servos the hard way

int servoPin = 2;     // Control pin for servo motor
int minPulse = 750;   // 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

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 = map(analogValue,0,1023,minPulse,maxPulse);    // convert the analog value
                                                       // to a range between minPulse
                                                       // and maxPulse.

 // 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(analogValue);

}


/*
This code was written for a pot
it assumes you're going to get values from 0 to 1023
if you don't, the servo won't move through its whole range. 

Determine the range of numbers the sensor is giving you and adjust the servo formula to fit.

Here's a scaling formula that will make the adjustment:

(pulseWidth - minPulse) / pulseRange = (sensorValue - minSensorValue) /sensorRange

Multiply both sides by pulseRange, and you get:

(pulseWidth - minPulse) = (sensorValue - minsensorValue * pulseRange /sensorRange

Add the minimum pulse to both sides and you get:

pulseWidth = ((sensorValue - minSensorValue) * pulseRange / sensorRange) + minPulse
*/

  Edit | View | History | Print | Recent Changes | Search Page last modified on February 18, 2009, at 01:22 PM