I am using Arduino Uno + HC SR04 Ultrasonic distance sensor and I want to add a potentiometer to manually set a minimum/maximum distance. So far I have been able to measure distances(e.g. moving objects nearer/further) but with no maximum or minimum set. Through this potentiometer I want to stop the program when the values reach the minimum or maximum. Unfortunately, I don't know how to put this in code so any help would be greatly appreciated. For any reference, this is my working code so far with no potentiometer installed.
void setup()
{
pinMode (2, OUTPUT);
pinMode (5, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(2, HIGH);
long duration, cm;
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(3, HIGH);
delayMicroseconds(3);
digitalWrite(3, LOW);
pinMode(4, INPUT);
duration = pulseIn(4, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.println(" cm");
delay(150);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}