• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!


Sensor to Servo Scaling

Page history last edited by PBworks 16 years, 8 months ago

Sensor to Servo Scaling

This program takes a reading from analogPin and converts that value from 0-1024 to an angle between 0-170 and then sends that angle to servoPulse to move the servo to a position relative to the sensor reading. This method of converting one value into another when they have different ranges is called scaling.


/*  Analog In to Servo Out
    Reads analog value, converts reading into usable
    angle value, and outputs neccy servo pulses.

int servoPin = 2;            //  R/C  Servo connected to digital pin 2
int analogPin = 0;           //  input pin for variable resistor
int pulseWidth;              //  function variable for delay
int analogValue;             //  stores converted angle value

void setup()
  pinMode(servoPin, OUTPUT);      // sets servo pin as output

// this is a function for determining our pulsewidth for the servo
void servoPulse(int servoPin, int myAngle)
  pulseWidth = (myAngle * 10) + 600;           // this determines our delay below (for a standard pot)
  digitalWrite(servoPin, HIGH);                // set servo high
  delayMicroseconds(pulseWidth);               // wait a very small amount (determined by pulsewidth)
  digitalWrite(servoPin, LOW);                 // set servo low
  delay(20);                                   // refresh cycle of typical servos (20 ms)
void loop()
  analogValue = analogRead(analogPin);         // reads analog value
  analogValue /= 6;                            // converts to usable angle
  servoPulse(servoPin, analogValue);           // calls servo function

Comments (0)

You don't have permission to comment on this page.