Using map function with a servo motor and ultrasonic sensor

I want to have my servo motor rotate a certain amount for each distance it gets on the ultrasonic sensor till it reaches 180 after a certain distance. So for example if I want the max to be 50 cm I want it to be at the halfway mark of 90 at 25 cm and in-between each degree for each distance till 50. I was told the best way to do this is the map function but don't really know how to start and get the map function to work

#include <Servo.h>
const int TRIG_PIN  = 6;  
const int ECHO_PIN  = 7; 
const int SERVO_PIN = 9; 
const int DISTANCE_THRESHOLD = 50; 

Servo servo; 
float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);  
void loop() {                          
  digitalWrite(TRIG_PIN, HIGH);
  digitalWrite(TRIG_PIN, LOW); 


This is what i have so far but don't know where to go from here to get the map function setup and working

Your sketch seems to be missing a critical section of the ranging code.


You posted in the section for tutorials. You posted a question about your project you did not post a tutorial. So your post has been moved.

i just looked at the tutorials section. Perhaps the general problem is: the first post does not tell people what a "tutorial" actually is. so the innocence do not know not to post.

Might very well be true, but that is a problem for the moderators. I am not a moderator I can only move posts and change titles due to my trust level.

But two topics tell people not to post questions.

Please post a link to it. A servo is one thing and a servo motor is something different. A link would clarify things.

if you try to compile your code it should tell you that val is not declared, so this would be a good start to making it work

that was accidently left from a old attempt I got rid of that the new void loop section looks like this

void loop() {                          
  digitalWrite(TRIG_PIN, HIGH);
  digitalWrite(TRIG_PIN, LOW); 

duraion_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.17 * duration_us;

  Serial.print("distance: ");
  Serial.println(" cm");


Doesn't that still give errors?

it doesn't give any I've already ran it but it obviously just doesn't give a error and doesn't move the motor any as that isn't set up yet


pulseIn returns an unsigned long, not a float, even if spelled correctly.

Try mapping an integer, not a float.

ok ill look into that more and try and figure that out. I also wonder if this code I found on another forum site post about a similar question would work if I change a few things around (potpin);     // read the pot

if(pin10==HIGH)               // pin 10 selects servo range
  val=map(val,0,1023,0,180);  // map the pot value ...
else                          //   into one of
  val=map(0,1023,0,90);       //   the servo ranges


Maybe You think about

if ( pin10 ) ==HIGH ) 

I think it was just offered as an example :wink:

Use the code from your head, not the code from someone else’s head.

One never know.....

I thought the "" was a dead giveaway.