Make the ping distance match the pot distance?

Hi
I have a Personal Fan that is working.  But,

I am trying to change the ping distance to operate a rely.
Trying to get the Ping distance to track a pot input so the distance the fan will come on can be changed by the pot.
In the serial monitor the pot input will change from 0 to 1024.
I was not able to get the ping distance to read out and how to make the Distance value track to the input pot?

Thanks to anybody that could look at this ad steer me in the write direction. 


#include "Ultrasonic.h"

int RELAY = 9;  // RELAY Pin.
Ultrasonic sensor1(11,10); // Ultrasonic(int TP, int EP);
//int TRIG = 11; // Trigger Pin
//int ECHO = 10; // Echo Pin
int Range; // The range of the object from Ping Sensor
int Dist; // The Distance value
int POTPIN = A1;
//#declare relayState;
int  relayState;
//int distPot; distControl;
//#declare distPot;
int distPot;
//#declare distControl;
int distControl;

long cm;

//distance min and max
const int minDistance = 2;  //dist in CM dont go below 2
const int maxDistance = 200;  //dist in CM



//Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.

void setup() {
  pinMode(RELAY, OUTPUT); //To the relay via the transistor
  //Dist = 15; //The distance in inches. Change this for increasted or dicreasted range.

Dist = map(Dist,0, 5, 1, 1023);
Serial.begin(9600);
}

void loop(){
  //Range = ultrasonic.Ranging(CM); // Range is calculated in Centimeters.

Dist = analogRead(POTPIN);
 
// check the distance pot
  distPot = analogRead(Dist);
  //distControl = map(distPot,0,1024,minDistance,maxDistance);
  distControl = constrain(RELAY,0,255);
  if(relayState){
    Dist  = distControl.Ranging(CM);
    digitalWrite(RELAY,HIGH);
  }
     else{
     digitalWrite(RELAY,LOW);
  }

   // RELAY = map(cm,distControl,minDistance); //variable map formula relationship
    RELAY = constrain(RELAY, 0, 255);



//int i = analogRead(POTPIN);
// if (i == Dist)digitalWrite(RELAY,HIGH);
// else digitalWrite(RELAY,LOW);


  //Range = ultrasonic.Ranging(INC); // Range is calculated in Inches.
 
  //if (Range < Dist) {
    //digitalWrite(RELAY, HIGH);  
  //} else if (Range > Dist) 
    //digitalWrite(RELAY, LOW);
     //delay(90);
 
Serial.print(Dist);
Serial.println("inch");
delay(100);
//Serial.print(sensor1);
  Serial.print(distControl);
//Serial.println("POTPIN");
delay(100);


}

sort by: active | newest | oldest
WWC (author) 4 years ago
Now that it is compiling, the ping is accurate in the serial monitor but the pot distance in still 60 to 120 not 0 yo 1023
For reasons I don't understand, I can't post the code back here. I'll do it in an hour or so.

Steve
WWC (author)  steveastrouk4 years ago
OK
Check your email, I sent you a link now.
WWC (author)  steveastrouk4 years ago
OK got.

I will study this to see whats what compared to what i had.

Thanks you are fast.
Follow up message tomorrow.

Thanks
Wayne
WWC (author)  WWC4 years ago
Have 1.03.

If i change to Range = ultrasonic.Ranging(CM); // Range is calculated in Centimeters.

It will compile.

Serial monitor range is accurate in CM
The Dist in the serial monitor is 60 to 120, not 0 to 1023
Mine appears to be 1.1.2, and the sample code doesn't show "Ranging" as an argument
/*
 * HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
 *
 * SVN Keywords
 * ----------------------------------
 * $Author: cnobile $
 * $Date: 2011-09-17 02:43:12 -0400 (Sat, 17 Sep 2011) $
 * $Revision: 29 $
 * ----------------------------------
 */

#include <Ultrasonic.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

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

void loop()
  {
  float cmMsec, inMsec;
  long microsec = ultrasonic.timing();

  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
  Serial.print("MS: ");
  Serial.print(microsec);
  Serial.print(", CM: ");
  Serial.print(cmMsec);
  Serial.print(", IN: ");
  Serial.println(inMsec);
  delay(1000);
  }

WWC (author)  steveastrouk4 years ago
I see now my ultrasonic.h file was incomplete. I redownloaded it and install now it compiles
Mine appears to be 1.1.2, and the sample code doesn't show "Ranging" as an argument


/*
 * HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
 *
 * SVN Keywords
 * ----------------------------------
 * $Author: cnobile $
 * $Date: 2011-09-17 02:43:12 -0400 (Sat, 17 Sep 2011) $
 * $Revision: 29 $
 * ----------------------------------
 */

#include <Ultrasonic.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

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

void loop()
  {
  float cmMsec, inMsec;
  long microsec = ultrasonic.timing();

  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
  Serial.print("MS: ");
  Serial.print(microsec);
  Serial.print(", CM: ");
  Serial.print(cmMsec);
  Serial.print(", IN: ");
  Serial.println(inMsec);
  delay(1000);
  }

Not to point out the obvious, but Range is commented out !
WWC (author)  steveastrouk4 years ago
That's because i have something not correct there and it errors.
Post up a clean copy of what you think SHOULD work, and I can make more sense of it. At least I can get it to build for you.

Steve
WWC (author)  steveastrouk4 years ago

#include "Ultrasonic.h"


Ultrasonic sensor1(11,10); // Ultrasonic(int TP, int EP);
int RELAY = 9; // RELAY Pin.
int Range; // The range of the object from Ping Sensor
int Dist; // The Distance value
int POTPIN = A1;
int relayState;
int distPot; distControl;
long cm;

//distance min and max
const int minDistance = 2; //dist in CM dont go below 2
const int maxDistance = 200; //dist in CM
Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.

void setup() {
pinMode(RELAY, OUTPUT); //To the relay via the transistor
Dist = map(Dist,0, 5, 1, 1023);
Serial.begin(9600);
}

void loop(){
Range = ultrasonic.Ranging(CM); // Range is calculated in Centimeters.

Dist = analogRead(POTPIN);

// check the distance pot
distPot = analogRead(Dist);
distControl = constrain(RELAY,0,255);
if(relayState){
Dist = distControl.Ranging(CM);
digitalWrite(RELAY,HIGH);
}
else{
digitalWrite(RELAY,LOW);
}

// RELAY = map(cm,distControl,minDistance); //variable map formula relationship
RELAY = constrain(RELAY, 0, 255);

Serial.print(Dist);
Serial.println("inch");
delay(100);
//Serial.print(sensor1);
Serial.print(distControl);
//Serial.println("POTPIN");
delay(100);


}
You have two declarations for the ultrasonic "sensor" and "ultrasonic", the arguments for "ultrasonic", TRIG and ECHO aren't declared, and the ultrasonic has no argument "ranging", the output is "timing", which has the units of microseconds, which are passed to the convert routine to return distance..

thus you should have:

Range = Ultrasonic.convert( ultrasonic.timing,cm); // Range is calculated in Centimeters.

for a start. Then it faults because you have this line

if(relayState){
Dist = distControl.Ranging(CM);
digitalWrite(RELAY,HIGH);

but distControl isn't declared as an ultrasonic, and I don't think it should be.

I think I see what you want: measure range, read pot. If range is < set distance on pot, turn on fan ? What's the range of control to be ? 10-100 cm say ?
WWC (author)  steveastrouk4 years ago
Got rid of the ultrasonic TRIG and ECHO.
Changed to the Range = Ultrasonic.convert( ultrasonic.timing,cm); // Range is calculated in Centimeters.
This is erroring also but i think its cuz of something else in the code.


Its complaining distControl isn't declared. I tried int and #define but still errors.

Yes i am trying to get the ping triggering range to change with the pot instead of going into the IDE to do it..
I've got "working" code, but no way to post it yet for reasons that defeat me !
WWC (author)  steveastrouk4 years ago
Its having a problem with Range = ultrasonic.convert( ultrasonic.timing(),Ultrasonic::CM);


It doesn't like the convert and timing. Says they are not in the ultrasonic library. It there an alternative for these?
They're in my library, which I downloaded and installed from your link this afternoon !! The code compiled just fine. Make sure you have his latest version I suppose.
WWC (author)  steveastrouk4 years ago
OK
Try this:
#include <Ultrasonic.h>


int RELAY = 9; // RELAY Pin.
int RelayState =0;
int TRIG = 11;
int ECHO = 10;
int Range; // The range of the object from Ping Sensor
int Dist; // The Distance value
int PotPin = A1;

//distance min and max
const int minDistance = 2; //dist in CM dont go below 2
const int maxDistance = 200; //dist in CM
Ultrasonic ultrasonic(TRIG,ECHO); // Create and initialize the Ultrasonic object.

void setup() {
pinMode(RELAY, OUTPUT); //To the relay via the transistor

Serial.begin(9600);
}

void loop(){
Range = ultrasonic.convert( ultrasonic.timing(),Ultrasonic::CM);

// check the distance pot
Dist = map(analogRead(PotPin),0,1023,minDistance,maxDistance);

if (Range > Dist) RelayState==LOW;

if  (Range<(Dist-1)) RelayState==HIGH;

digitalWrite(RELAY,RelayState);



Serial.print(Dist);
Serial.println("inch");
delay(100);
//Serial.print(sensor1);
Serial.print(Dist);
//Serial.println("POTPIN");
delay(100);


}