2

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;
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Ylli Salihu
  • 38
  • 1
  • 4
  • I didn't undesrtand what you want to stop. Do you want to stop ( shut down ) the shield or dou you want to stop the distance sensor measurement OR do you want to stop the measurement when the potentiometer gets min/maximum value? – Andrew Paes Dec 11 '14 at 18:27
  • I want to stop the distance sensor measurement when e.g. the sensor doesn't detect any object within 5cm(5cm being the min value set through the potentiometer). – Ylli Salihu Dec 11 '14 at 18:52
  • If you stop the sensor measurement functionality when not detecting anything, so when something come into his range, this will not automatically reactivated, because the sensor is off, his trigger is off. The sensor is always active or it needs to be activated by a third trigger. Do you have a passive sensor reading as motion sensor or pressure? – Andrew Paes Dec 11 '14 at 19:03
  • So you do not want to stop the measurement. You want to define the limits (capacity) of the sensor using the pot, but if you want the sensor to stop scan for objects when he did not find anything, then know that it will not be reactivated when something come into its range. To do this it is necessary to make a linearization of the signal pot and the sensor capacity. – Andrew Paes Dec 11 '14 at 19:04

1 Answers1

0

I really do not understand what you mean:

"I want to stop the distance sensor measurement when e.g. the sensor doesn't detect any object within 5cm(5cm being the min value set through the potentiometer)"

Then left open for you to put the desired function

PS: Sorry for my delay, I had some personal problems

enter image description here

// http://www.parsicitalia.it/files/HC-SR04-Datasheet.pdf
// HC-SR04
// Sentry Angle : max 15 degree
// Sentry Distance : 2cm - 500cm
// High-accuracy : 0.3cm
// Input trigger signal : 10us TTL impulse
// Echo signal : output TTL PWL signal
// Size : 45*20*15mm

int _trigPin = 11;    //Trig - green Jumper
int _echoPin = 12;    //Echo - yellow Jumper
int _sensorMaxRng = 500; //cm
int _sensorMinRng = 2; //cm

int _potentiometer = 9; // Potentiometer - Analog Pin
int _val = 0;
int _borderLineVal = 0;

long _duration, _cm, _inches, _potentioRng;

void setup() {
    Serial.begin(9600);

    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

void loop()
{
    cleanRefreshSensor();

    _val = analogRead(_potentiometer); //reading the Potentiometer value interval: 0 - 1023
    _potentioRng = (long)((_sensorMaxRng - _sensorMinRng) / 1023);
    Serial.println(_val);
    Serial.println(_potentioRng);

    pinMode(_echoPin, INPUT);
    _duration = pulseIn(_echoPin, HIGH); // duration is the time (in microseconds) from the sending of the ping to the reception of its echo off of an object

    // convert the time into a distance
    // The speed of sound is "340 m/s" or "29 microseconds/centimeter"
    _cm = _duration / 2 / 29.1; //Calculate the distance based on the speed of sound divided by 2 ( signal going and coming back)
    _inches = _duration / 2 / 74;

    Serial.print(_inches);
    Serial.print(" in, ");
    Serial.print(_cm);
    Serial.print(" cm");
    Serial.println();

    if(_cm > _potentioRng){
        Serial.print(" IN ");
    }
    else
    {
        Serial.print(" OUT ");
    }

    delay(150);
}

// The sensor is triggered by a HIGH pulse of 10 or more microseconds
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
void cleanRefreshSensor()
{
    digitalWrite(_trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(_trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(_trigPin, LOW);
}
Andrew Paes
  • 1,940
  • 1
  • 15
  • 20
  • 1
    Thank you for your answer and help Andrew. I managed to make it work and I am sorry that my question was not clear initially. I placed the sensor in place and it is working well. – Ylli Salihu Dec 18 '14 at 08:43