1st time user with PWM and a MoxBotix sensr.

I’ve created a basic US distance measuring device but have upgraded my sensor to the MaxBotix MB1000 EZ0. It uses PWM for input and I’m struggling to activate an LED when the distance below 1.5’ Formerly, I was using the standard US sensor that arrives in the Arduino Uno kit. My issue is that I don’t fully understand the output of the new sensor, and as such, cannot tell the LED when to turn on. Clearly, in the code below, I haven’t defined duration… but with this sensor, I’m not sure that I need to.
I would appreciate any assistance.

const int pwPin1 = 3; 
long sensor1, cm, inches;
#define led 13
#define buzzer 11

int sound = 250;

void setup () {
  Serial.begin(9600);
  pinMode(pwPin1, INPUT);
  pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void read_sensor()
{  sensor1 = pulseIn(pwPin1, HIGH);
  ;inches = cm/2.54; // converts the range to inches
}


void printall(){         
  Serial.print("S1");
  Serial.print(" ");
  Serial.print(sensor1);
  Serial.println("");


}

void loop () {
  read_sensor();    
  distance = (duration/2) / 29.1;
  printall();
   if (distance <= 1200) {
    digitalWrite(led, HIGH);
    sound = 250;
}
  else {
    digitalWrite(led,LOW);
  }
  delay(250); //refresh rate 1/4 second
}

Errors…

In function ‘void loop()’:
MaxUS:33:3: error: ‘distance’ was not declared in this scope
distance = (duration/2) / 29.1;
^
MaxUS:33:15: error: ‘duration’ was not declared in this scope
distance = (duration/2) / 29.1;
^
exit status 1
‘distance’ was not declared in this scope

well... where did you declare what distance is ?

Why not do a simplified program with just readSensor() and Serial.print(sensor1) in loop()? Then you can check what values you are getting from the sensor with various distances.

Steve

It looks like you took their example PWM code and mucked it up.

your read_sensor() function sets the variable inches so you should probably use that to figure out if you are within 1.5 feet or not.

Note that your inches variable is declared as 'long' so you will only get whole inches which means you should probably test for inside/outside of 18 inches...

J-M-L: well... where did you declare what distance is ?

Turns out I didn't do that either. Thank you for pointing it out.

blh64: It looks like you took their example PWM code and mucked it up.

your read_sensor() function sets the variable inches so you should probably use that to figure out if you are within 1.5 feet or not.

Note that your inches variable is declared as 'long' so you will only get whole inches which means you should probably test for inside/outside of 18 inches...

You are correct. I have mucked it up pretty decently. My struggle at this point, with my tiny Arduino/coding brain, is that I'm not sure how to if/then, else for the LED. For instance, I would like the LED to go High if less than 1.5' Formerly, I would have used the "distance" to do this. But I do not have these defined, and I don't think the PWM output likes these definitions. What am I missing here? I was assuming I could use PWM much the same as I did with the original US sensor, but I'm not using the same input as the former device. How do I define distance for this setup?

Thanks again to all for your extremely quick responses yesterday. I didn't expect that. This community is fantastic. Also, I can only post a reply every 5 min, as I"m a new user.

There are several different outputs from that sensor. The one I think you're using returns a pulse length proportional to the distance. The documentation says you will see 147 microseconds per inch. So I guess the part you've missed out is after the pulsein() gives you a pulse length in microseconds you need to divide "sensor1" by 147 and that will give you distance in inches. Give it a try (and post your new code here).

Steve

slipstick:
There are several different outputs from that sensor. The one I think you’re using returns a pulse length proportional to the distance. The documentation says you will see 147 microseconds per inch. So I guess the part you’ve missed out is after the pulsein() gives you a pulse length in microseconds you need to divide “sensor1” by 147 and that will give you distance in inches. Give it a try (and post your new code here).

Steve

const int pwPin1 = 3; //this may be different depending on the Arduino being used, and the other PW pins being used.
long sensor1, cm, inches;
#define led 13
#define buzzer 11

int sound = 250;

void setup () {
  Serial.begin(9600);
  pinMode(pwPin1, INPUT);
  pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void read_sensor()
{  sensor1 = pulseIn(pwPin1, HIGH);
  ;distance = sensor1 / 147; // inches?
}

void printall(){         
  Serial.print("S1");
  Serial.print(" ");
  Serial.print(sensor1); 
  Serial.println(""); 


}

void loop () {
  read_sensor();    
  distance = sensor1 / 147;
  printall();
   if (distance <= 1200) {
    digitalWrite(led, HIGH);
    sound = 250;
}
  else {
    digitalWrite(led,LOW);
  }
  delay(250); //refresh 1/4 second
}

ERRORS----

Arduino: 1.8.8 (Mac OS X), Board: "Arduino/Genuino Uno"

/ In function 'void read_sensor()':
MaxUS:18:4: error: 'distance' was not declared in this scope
   ;distance = sensor1 / 147; // inches?
    ^
/Users/MOliver/Documents/Arduino/MaxUS/MaxUS.ino: In function 'void loop()':
MaxUS:32:3: error: 'distance' was not declared in this scope
   distance = sensor1 / 147;
   ^
exit status 1
'distance' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I thought you had already worked out that you can't use a variable without first defining it. Your program has 3 variables defined called sensor1, cm and inches. So why not use them? E.g. inches = sensor1 / 147; Then you can look to see if inches is more or less than 18. Forget about "distance".

Steve

slipstick: I thought you had already worked out that you can't use a variable without first defining it. Your program has 3 variables defined called sensor1, cm and inches. So why not use them? E.g. inches = sensor1 / 147; Then you can look to see if inches is more or less than 18. Forget about "distance".

Steve

You are a superstar. I'm not quite sure why that didn't compute in my head earlier. I just fixed it, as you suggested, and it compiled perfectly. Now on to adding two additional sensors to this rig. The center/main is the US and the two outward facing will be IR. The IR seem to draw a decent amount, but as I'm on the road traveling for work, I can't measure. Time will tell.

Thank you again to all who posted and for slipstick for getting through my skull.

slipstick:
I thought you had already worked out that you can’t use a variable without first defining it. Your program has 3 variables defined called sensor1, cm and inches. So why not use them? E.g. inches = sensor1 / 147; Then you can look to see if inches is more or less than 18. Forget about “distance”.

Steve

Well, I’m back at it. I have returned from my work trip and uploaded the changes to the code. While it compiles fine and loads to the arduino, the sensor seems to be working well… My led doesn’t switch to high. Further, when in serial view, I’m still getting numbers in the thousands e.g. S1 4500 (which is indicating distance) but this isn’t what I’ve asked it to do.

So I have two problems I believe.

1st is that my code isn’t correctly asking the arduino to calculate distance in inches.

2nd is that the LED isn’t switching to high when it sees a distance less than 18"

Thoughts?

const int pwPin1 = 3; // Yellow signal wire
long sensor1, inches;
#define led 13  //Blue signal
#define buzzer 11 //Orange signal

int sound = 250;

void setup () {
  Serial.begin(9600);
  pinMode(pwPin1, INPUT);
  pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void read_sensor()
{  sensor1 = pulseIn(pwPin1, HIGH);
  ;inches = sensor1 / 147; // inches output
}

void printall(){         
  Serial.print("S1");
  Serial.print(" ");
  Serial.print(sensor1); 
  Serial.println(""); 


}

void loop () {
  read_sensor();    
  printall();
   if (inches <= 20) {
    digitalWrite(led, HIGH);
    sound = 250; //Sensor within 20 inches
}
  else {
    digitalWrite(led,LOW);
  }
  delay(250); //refresh 1/4 second
}

Why not add a print of inches to printAll() so you can see exactly what you’re working with?

Anyway if sensor1 = 4500 then inches isn’t <= 20. It needs to be below 2940.

Steve

I agree. I’ll have it say inches in the print loop. At this point… is it possible to have it “print” the inches… and then use that information to determine how far it is from an object… e.g. Inches 20 and if <=20 then digitalWrite (led, HIGH); ???

This is where I seem to be getting mixed up.

Again, I’m not sure I’ve written it correctly, as at some point I changed the if statement to look for the high (raw) numbers I’m getting and it still doesn’t turn on.

Matt