Hi:
I am working on this Ultrasonic Range Sensor by Max Electronics, MB7360:
Unlike the HC-SR04, it does not have separate pins for trigger and echo but a single pin does the job of both. The obstacle distance can either be calculate from voltage(using pin 3) or pwm(using pin 2). I am trying to use pwm. The arduino code is as follows:
void setup () {
Serial.begin(9600);
pinMode(pwPin1, INPUT);
}
void read_sensor(){
sensor1 = pulseIn(pwPin1, HIGH);
cm = sensor1/10 // converts the range to cm
inches = cm/2.54 // converts the range to inches
}
Is there an equivalent of the pulseIn function for the imp? Or do I explicitly have to send a high-low pulse at the pin? What I have done for the imp so far is as follows: It gives me a distance of 42mm but I don’t know if makes sense:
// Ultrasonic Range Sensor HRXL-MaxSonar(MB7360)
class HRXL {
// consts
static TO = 100; // timeout in ms
// pins
_trig = null;
//_echo = null;
// aliased methods
_tw = null;
_tr = null;
_hu = null;
_hm = null;
// vars
_ts = null; // pulse start time
_te = null; // pulse end time
constructor(trig) {
_trig = trig;
_hu = hardware.micros.bindenv(hardware);
_hm = hardware.millis.bindenv(hardware);
_tw = _trig.write.bindenv(_trig);
_tr = _trig.read.bindenv(_trig);
}
function read_mm() {
local st = _hm(); // start time for timeout
// Quickly pulse the trig pin
_tw(0); _tw(1); _tw(0);
// Wait for the pulse to go high, start timing
while (_tr() == 0 && (_hm() - st) < TO);
_ts = _hu();
// Wait for pulse to go low, stop timing
while (_tr() == 1 && (_hm() - st) < TO);
_te = _hu();
//if ((_hm() - st) >= TO) return -1;
return (_te - _ts);
}
}
//local r = range.read_cm();
// Ex Usage
trig <- hardware.pin8;
trig.configure(DIGITAL_IN);
range <- HRXL(trig);
server.log(range.read_mm()+" mm");
Thanks,
Apoorva.