problem in controlling ultrasonic sensor with potentiometer

Hello,

So I basically want to create a thief detecting machine in which it's range can be ad-justified. For this I basically needed a way to convert potentiometer's input into an integer so as to compare it. I have tried different ways but they just don't seem to help me. I would be very thankful if anyone would help me by providing an arduino code for this purpose.

Thanks & Regards

It's better if you post your attempts here so we can help you and point out the problems, otherwise it just looks like we're doing your assignment for you.

const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
int val;
int potPin;
int minDistance = 2;
int maxDistance = 200;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {

int range = analogRead(A0);
int setpoint = map(analogRead(potPin), 0, 1023, minDistance, maxDistance);

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= setpoint){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

So, what's the problem?
(Don't say "it doesn't work")

What does the serial monitor show?

Please remember to use code tags when posting code.

int range = analogRead(A0);
int setpoint = map(analogRead(potPin), 0, 1023, minDistance, maxDistance);

You didn't give potPin a value.
Isn't it great that crt0 set it to zero for you?

The problem which I think happens is that :

when i turn the potentiometer (sorry i don’t know its name) the led and buzzer doesn’t activates even though there is an object in front of it.

Sometimes it suddenly starts the led and buzzer which doesn’t stops even though I point it towards blank space.

so i need to initialize potpin, it is actually analog 0.

/*

*/
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
int led = 13;
int minDistance;
int maxDistance;
int potPin = 0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(potPin, INPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
int range = analogRead(potPin);
int setpoint = map(analogRead(potPin), 0, 1023, minDistance, maxDistance);
digitalWrite(led, range < setpoint);
}

how bout this code
by the way i think i will try it again
will contact you soon

In your original code you had some prints.
What did they show?

You've already read analogue pin zero, and stored the result in "range" - why do you not then use "range" in the mapping?

The code you just posted has max and minutes distance set to zero.

Please remember to use code tags when posting code.

i have tackle the problem nw it works.