mbot-ultrasonic-sensor

gabbapeople/mbot-lib/mbot-ultrasonic-sensor

Node controls "Me Ultrasonic Sensor".
mbot-ultrasonic-sensor
@/mbot-ultrasonic-sensor
Node controls "Me Ultrasonic Sensor".
S1port
Input port pin.
UPDpulse
Triggers new read.
mbot-ultrasonic-sensor
S1
UPD
DIST
DISTnumber
Distance in meters.
To use the node in your project you should have the gabbapeople/mbot-lib library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
};
    long lastEnterTime = millis();
    bool measureFlag = true;
    float measureValue = 0;
    double MAXcm = 400;

{{ GENERATED_CODE }}



//////////////////////////////////////////////////////////////////////
long measure(unsigned long timeout,int port)
{
  long duration;
  if(millis() - lastEnterTime > 23)
  {
   measureFlag = true; 
  }

  if(measureFlag == true)
  {
    lastEnterTime = millis();
    measureFlag = false;

    ::pinMode(port, OUTPUT);
    ::digitalWrite(port, LOW);
    delayMicroseconds(2);
    ::digitalWrite(port, HIGH);
    delayMicroseconds(10); 
    ::digitalWrite(port, LOW);
    ::pinMode(port, INPUT);
    duration = pulseIn(port, HIGH, timeout);
    measureValue = duration;
  }
  else
  {
    duration = measureValue;
  }
  return(duration);
}


void evaluate(Context ctx) {
    if (!isInputDirty<input_UPD>(ctx))
        return;

  auto port = getValue<input_S1>(ctx);
  long distance = measure(MAXcm * 55 + 200,port);

  if(distance == 0)
  {
    distance = MAXcm * 58;
  }
    
    double dis = distance / 58.0 / 100;
    emitValue<output_DIST>(ctx,dis); 
}