Ultrasonic code modification

Hello all, I found this code online and I want to add a second ultrasonic sensor to it. I have them wired to the same pins. Would I have to include the second sensor into the code? If so could someone help me?

Please post your code.
In code tags.

Are you saying you've got both sensors wired to the same pins as each other?
Or same pins as the example code?

What type of ultrasonic sensors are you using ?

Please post a schematic of your project

Your post was MOVED to its current location as it is more suitable

Here is the link for the project im modifying.
https://create.arduino.cc/projecthub/ronfrtek/arduino-led-ring-ultrasonic-distance-sensor-d15582

It is the Hc-sr04 both sensors are wired to the same pins.

//----------------------------------------------
//
//         Sketch Generated by Visuino
//               www.visuino.com
//          Version 8.0.0.37
//
//------------------ Source --------------------
//
// neo-pixel-distance-ultrasound.visuino
//
//----------------------------------------------

#define VISUINO_ARDUINO_NANO

#include <OpenWire.h>
#include <Mitov.h>
#include <Mitov_UltrasonicRanger.h>
#ifdef __VISUINO_USE_NEO_PIXELS_BUS_LIBRARY__
#include <NeoPixelBrightnessBus.h> // The NeoPixelBus library https://github.com/Makuna/NeoPixelBus needs to be installed trough the Arduino IDE for this Scetch to compile!
#else // __VISUINO_USE_NEO_PIXELS_BUS_LIBRARY__
#include <Adafruit_NeoPixel.h> // The Adafruit NeoPixel library https://github.com/adafruit/Adafruit_NeoPixel to be installed trough the Arduino IDE for this Scetch to compile!
#endif // __VISUINO_USE_NEO_PIXELS_BUS_LIBRARY__
#include <Mitov_NeoPixel.h>
#include <Mitov_RampToValue.h>
#include <Mitov_Converters.h>
#include <Mitov_MultiMerger.h>
#include <Mitov_Math.h>



// Shared Component Member Variables

namespace ComponentVariables
{
class
{
  public:
    bool Value1 : 1;
    uint32_t Value2 : 3;
    bool Value3 : 1;
    bool Value4 : 1;
    bool Value5 : 1;
    bool Value6 : 1;
    bool Value7 : 1;
    bool Value8 : 1;
    bool Value9 : 1;
    bool Value10 : 1;

} BitFields;

class Variable1
{
  public:
    inline static bool GetValue() {
      return BitFields.Value1;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value1 = AValue;
    }

};

class Variable2
{
  public:
    inline static uint32_t GetValue() {
      return BitFields.Value2;
    }
    inline static void SetValue( uint32_t AValue ) {
      BitFields.Value2 = AValue;
    }

};

class Variable3
{
  public:
    inline static bool GetValue() {
      return BitFields.Value3;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value3 = AValue;
    }

};

class Variable4
{
  public:
    inline static bool GetValue() {
      return BitFields.Value4;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value4 = AValue;
    }

};

class Variable5
{
  public:
    inline static bool GetValue() {
      return BitFields.Value5;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value5 = AValue;
    }

};

class Variable6
{
  public:
    inline static bool GetValue() {
      return BitFields.Value6;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value6 = AValue;
    }

};

class Variable7
{
  public:
    inline static bool GetValue() {
      return BitFields.Value7;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value7 = AValue;
    }

};

class Variable8
{
  public:
    inline static bool GetValue() {
      return BitFields.Value8;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value8 = AValue;
    }

};

class Variable9
{
  public:
    inline static bool GetValue() {
      return BitFields.Value9;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value9 = AValue;
    }

};

class Variable10
{
  public:
    inline static bool GetValue() {
      return BitFields.Value10;
    }
    inline static void SetValue( bool AValue ) {
      BitFields.Value10 = AValue;
    }

};

class Shared_float_0 : public ::ComponentVariables::Variable6
{
  protected:
    static float FValue;

  public:
    inline static float GetVariableValue( const float AInitialValue )
    {
      if ( ::ComponentVariables::Variable6::GetValue() )
        return FValue;

      return AInitialValue;
    }

    inline static void SetVariableValue( const float AValue ) {
      FValue = AValue;
      ::ComponentVariables::Variable6::SetValue( true );
    }
    inline static float &GetReference() {
      return FValue;
    }

    inline Shared_float_0() {
      ::ComponentVariables::Variable6::SetValue( false );
    }

};

float Shared_float_0::FValue;

} // ComponentVariables

// Arduino Constant Declarations

namespace VisuinoConstants
{
class FloatValue0
{
  public:
    inline static constexpr float GetValue() {
      return 10000;
    }
};

class FloatValue3
{
  public:
    inline static constexpr float GetValue() {
      return 1000;
    }
};

class FloatValue2
{
  public:
    inline static constexpr float GetValue() {
      return 0;
    }
};

class FloatValue5
{
  public:
    inline static constexpr float GetValue() {
      return 10;
    }
};

class FloatValue1
{
  public:
    inline static constexpr float GetValue() {
      return 0.401123046875;
    }
};

class FloatValue4
{
  public:
    inline static constexpr float GetValue() {
      return 1;
    }
};

} // VisuinoConstants

// Pin Call Declarations

namespace PinCalls
{
class PinCallerReceive0
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive1
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive2
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive3
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive4
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive5
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive6
{
  public:
    void Notify( void *_Data );

};
class PinCallerReceive7
{
  public:
    void Notify( void *_Data );

};
} // PinCalls

// Call Chains

namespace CallChains
{
class InterruptRaising1
{
  public:
    inline static uint32_t Count() {
      return 1;
    }
    static void Call();

};
class InterruptFalling1
{
  public:
    inline static uint32_t Count() {
      return 1;
    }
    static void Call();

};
class TryActive1
{
  public:
    inline static uint32_t Count() {
      return 0;
    }
    static void Call( bool & AResult );

};
class TryActive2
{
  public:
    inline static uint32_t Count() {
      return 0;
    }
    static void Call( bool & AResult );

};
} // CallChains

// System Objects Declarations

namespace BoardDeclarations
{
Mitov::TElapsedMicros ElapsedSystemMicros;
} // BoardDeclarations

// Arduino Board Declarations

namespace BoardDeclarations
{
namespace Types
{
typedef Mitov::ArduinoDigitalOutputChannel <
Mitov::ConstantProperty<22, bool, false>, // IsAnalog
      Mitov::ConstantProperty<24, bool, false>, // IsCombinedInOut
      Mitov::ConstantProperty<21, bool, false>, // IsOpenDrain
      Mitov::ConstantProperty<26, bool, false>, // IsOutput
      Mitov::ConstantProperty<20, bool, false>, // IsPullDown
      Mitov::ConstantProperty<5, bool, false >, // IsPullUp
      Mitov::ConstantProperty<23, bool, false>, // IsRawInput
      Mitov::DigitalPin_NoImplementation<6 >, // OutputPin
      3 // PIN
      > ArduinoDigitalChannel_3;
} // Types

namespace Instances
{
Types::ArduinoDigitalChannel_3 ArduinoDigitalChannel_3;
} // Instances

} // BoardDeclarations

// Interrupts

namespace Interrupts
{
void __ICACHE_RAM_ATTR__ Handler3();

OpenWire::PinChangeInterrupt<3, ::CallChains::InterruptRaising1, ::CallChains::InterruptFalling1> Pin3;

void __ICACHE_RAM_ATTR__ Handler3()
{
  Pin3.InterruptHandler();
}

} // Interrupts

// Declarations

namespace Declarations
{
namespace Types
{
typedef Mitov::UltrasonicRanger_Interrupt <
Mitov::ConstantProperty<5, bool, false>, // ClockInputPin_o_IsConnected
      Mitov::ConstantProperty<7, bool, true >, // Enabled
      Mitov::TypedVariable<15, bool, ::ComponentVariables::Variable1 >, // FClocked
      Mitov::TypedVariable<17, uint32_t, ::ComponentVariables::Variable2 >, // FState
      Mitov::AnalogPin_EmbeddedPinImplementation<3, ::PinCalls::PinCallerReceive0 >, // OutputPin
      Mitov::ConstantProperty<12, uint32_t, 100 >, // PauseTime
      Mitov::DigitalPin_DirectBoardPinImplementation<2 >, // PingOutputPin
      Mitov::ConstantProperty<11, uint32_t, 2 >, // PingTime
      Mitov::ConstantProperty<9, uint32_t, 1000 >, // Timeout
      Mitov::DigitalPin_NoImplementation<6 >, // TimeoutOutputPin
      Mitov::ConstantPropertyFloat<10, float, ::VisuinoConstants::FloatValue0 >, // TimeoutValue
      Mitov::ConstantProperty<8, Mitov::TArduinoRangerUnits, Mitov::rdCm > // Units
      > UltrasonicRanger1;
} // Types

namespace Instances
{
Types::UltrasonicRanger1 UltrasonicRanger1;
} // Instances

namespace NeoPixel_Declarations
{
#ifdef __VISUINO_USE_NEO_PIXELS_BUS_LIBRARY__
namespace NeoPixelType
{
typedef NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> NeoPixels1;
}
NeoPixelType::NeoPixels1 NeoPixels1( 16, 6 );
#else // __VISUINO_USE_NEO_PIXELS_BUS_LIBRARY__
namespace NeoPixelType
{
typedef Adafruit_NeoPixel NeoPixels1;
}
NeoPixelType::NeoPixels1 NeoPixels1( 16, 6, NEO_GRB + NEO_KHZ800 );
#endif // __VISUINO_USE_NEO_PIXELS_BUS_LIBRARY__
} // NeoPixel_Declarations
namespace Types
{
typedef Mitov::NeoPixels <
NeoPixel_Declarations::NeoPixelType::NeoPixels1, // 0_TYPE
                      NeoPixel_Declarations::NeoPixels1, // 1_NAME
                      Mitov::ConstantPropertyFloat<6, float, ::VisuinoConstants::FloatValue1 >, // Brightness
                      Mitov::TypedVariable<8, bool, ::ComponentVariables::Variable3 > // FModified
                      > NeoPixels1;
} // Types

namespace Instances
{
Types::NeoPixels1 NeoPixels1;
} // Instances

namespace Types
{
typedef Mitov::NeoPixelsColorPixelGroup <
Declarations::Types::NeoPixels1, // 0_TYPE_OWNER
             Declarations::Instances::NeoPixels1, // 1_NAME_OWNER
             16, // COUNT
             Mitov::TypedVariable<7, bool, ::ComponentVariables::Variable4 >, // FModified
             Mitov::VariablePropertyColor<3, Mitov::TRGBWColor, uint64_t, 0x00000000 >, // InitialColor
             Mitov::VariableProperty<6, uint32_t, 0 >, // InitialIndex
             0 // START
             > TArduinoNeoPixelsColorPixelGroup1;
} // Types

namespace Instances
{
Types::TArduinoNeoPixelsColorPixelGroup1 TArduinoNeoPixelsColorPixelGroup1;
} // Instances

namespace Types
{
typedef Mitov::ValueSource <
Mitov::EmbeddedCallChain<CallChains::TryActive1 >, // Elements_TryActive
      Mitov::TypedPin_EmbeddedPinImplementation<3, ::PinCalls::PinCallerReceive1, Mitov::TColor >, // OutputPin
      Mitov::TColor, // TYPE
      Mitov::ConstantPropertyColor<5, Mitov::TColor, uint32_t, 0x0000FF > // Value
      > ColorValue1;
} // Types

namespace Instances
{
Types::ColorValue1 ColorValue1;
} // Instances

namespace Types
{
typedef Mitov::RampToValue <
Mitov::ConstantProperty<5, bool, true >, // Enabled
      Mitov::TypedVariable<10, bool, ::ComponentVariables::Variable5 >, // FModified
      Mitov::VariablePropertyFloat<7, float, ::VisuinoConstants::FloatValue2 >, // InitialValue
      Mitov::ConstantPropertyFloat<9, float, ::VisuinoConstants::FloatValue2 >, // MaxStep
      Mitov::AnalogPin_EmbeddedPinImplementation<3, ::PinCalls::PinCallerReceive2 >, // OutputPin
      Mitov::ConstantPropertyFloat<8, float, ::VisuinoConstants::FloatValue3 > // Slope
      > RampToValue1;
} // Types

namespace Instances
{
Types::RampToValue1 RampToValue1;
} // Instances

namespace Types
{
typedef Mitov::AnalogToUnsigned <
Mitov::ConstantProperty<7, bool, true >, // Constrain
      Mitov::TypedPin_EmbeddedPinImplementation<5, ::PinCalls::PinCallerReceive3, uint32_t >, // OutputPin
      Mitov::ConstantProperty<6, bool, true >, // Round
      Mitov::ConstantPropertyFloat<4, float, ::VisuinoConstants::FloatValue4 > // Scale
      > AnalogToUnsigned1;
} // Types

namespace Instances
{
Types::AnalogToUnsigned1 AnalogToUnsigned1;
} // Instances

namespace Types
{
typedef Mitov::MultiMerger <
2, // COUNT_InputPins
Mitov::TypedPin_EmbeddedPinImplementation<3, ::PinCalls::PinCallerReceive4, Mitov::TRGBWColor > // OutputPin
> RGBWColorMultiMerger1;
} // Types

namespace Instances
{
Types::RGBWColorMultiMerger1 RGBWColorMultiMerger1;
} // Instances

namespace Types
{
typedef Mitov::CompareValue_Binding <
Mitov::CompareType_Implementation_ctBigger, // CompareType
      Mitov::ConstantProperty<4, bool, true >, // Enabled
      Mitov::TypedVariable<9, bool, ::ComponentVariables::Variable7 >, // FStarted
      Mitov::ConstantProperty<8, bool, true >, // OnlyChanged
      Mitov::DigitalPin_EmbeddedPinImplementation<5, ::PinCalls::PinCallerReceive5 >, // OutputPin
      float, // TYPE
      Mitov::SharedVariablePropertyFloat<6, float, ::VisuinoConstants::FloatValue5, ::ComponentVariables::Shared_float_0 > // Value
      > CompareValue1;
} // Types

namespace Instances
{
Types::CompareValue1 CompareValue1 = Types::CompareValue1( 0.0f );
} // Instances

namespace Types
{
typedef Mitov::CompareValue_Binding <
Mitov::CompareType_Implementation_ctSmaller, // CompareType
      Mitov::ConstantProperty<4, bool, true >, // Enabled
      Mitov::TypedVariable<9, bool, ::ComponentVariables::Variable8 >, // FStarted
      Mitov::ConstantProperty<8, bool, true >, // OnlyChanged
      Mitov::DigitalPin_EmbeddedPinImplementation<5, ::PinCalls::PinCallerReceive6 >, // OutputPin
      float, // TYPE
      Mitov::SharedVariablePropertyFloat<6, float, ::VisuinoConstants::FloatValue2, ::ComponentVariables::Shared_float_0 > // Value
      > CompareValue2;
} // Types

namespace Instances
{
Types::CompareValue2 CompareValue2 = Types::CompareValue2( 0.0f );
} // Instances

namespace Types
{
typedef Mitov::ValueSource <
Mitov::EmbeddedCallChain<CallChains::TryActive2 >, // Elements_TryActive
      Mitov::TypedPin_EmbeddedPinImplementation<3, ::PinCalls::PinCallerReceive7, Mitov::TColor >, // OutputPin
      Mitov::TColor, // TYPE
      Mitov::ConstantPropertyColor<5, Mitov::TColor, uint32_t, 0x00FF00 > // Value
      > ColorValue2;
} // Types

namespace Instances
{
Types::ColorValue2 ColorValue2;
} // Instances

} // Declarations

// Type Converters

namespace TypeConverters
{
Mitov::Convert_BinaryToClock<Mitov::TypedVariable<0, bool, ::ComponentVariables::Variable9 >> Converter0;
Mitov::Convert_BinaryToClock<Mitov::TypedVariable<0, bool, ::ComponentVariables::Variable10 >> Converter1;
} // TypeConverters

// Bindings

namespace Bindings
{
inline void CompareValue1_ValueInputPin_o_Receive( void * ___PTR___, float __AOriginalValue__ )
{
  float ___A_VALUE___ = *(float *)___PTR___;
  Declarations::Instances::CompareValue1.SetValue( __AOriginalValue__, ___A_VALUE___ );
}

inline void CompareValue2_ValueInputPin_o_Receive( void * ___PTR___, float __AOriginalValue__ )
{
  float ___A_VALUE___ = *(float *)___PTR___;
  Declarations::Instances::CompareValue2.SetValue( __AOriginalValue__, ___A_VALUE___ );
}

} // Bindings

// Call Chains

namespace CallChains
{
void InterruptRaising1::Call()
{
  Declarations::Instances::UltrasonicRanger1.InterruptHandler( true );
}

void InterruptFalling1::Call()
{
  Declarations::Instances::UltrasonicRanger1.InterruptHandler( false );
}

void TryActive1::Call( bool & AResult )
{
}

void TryActive2::Call( bool & AResult )
{
}

} // CallChains

// Pin Call Declarations

namespace PinCalls
{
void PinCallerConverterReceive1( void *_Data );
void PinCallerConverterReceive2( void *_Data );
} // PinCalls

// Pin Call Implementations

namespace PinCalls
{
void PinCallerReceive0::Notify( void *_Data )
{
  float __Value_CompareValue1_ValueInputPin__ = Declarations::Instances::CompareValue1.Value();
  float __Value_CompareValue2_ValueInputPin__ = Declarations::Instances::CompareValue2.Value();
  Declarations::Instances::RampToValue1.InputPin_o_Receive( _Data );
  ::Bindings::CompareValue1_ValueInputPin_o_Receive( _Data, __Value_CompareValue1_ValueInputPin__ );
  ::Bindings::CompareValue2_ValueInputPin_o_Receive( _Data, __Value_CompareValue2_ValueInputPin__ );
}

void PinCallerReceive1::Notify( void *_Data )
{
  Declarations::Instances::RGBWColorMultiMerger1.InputPins_o_Receive( 0, _Data );
}

void PinCallerReceive2::Notify( void *_Data )
{
  Declarations::Instances::AnalogToUnsigned1.InputPin_o_Receive( _Data );
  Declarations::Instances::CompareValue1.InputPin_o_Receive( _Data );
  Declarations::Instances::CompareValue2.InputPin_o_Receive( _Data );
}

void PinCallerReceive3::Notify( void *_Data )
{
  Declarations::Instances::TArduinoNeoPixelsColorPixelGroup1.IndexInputPin_o_Receive( _Data );
}

void PinCallerReceive4::Notify( void *_Data )
{
  Declarations::Instances::TArduinoNeoPixelsColorPixelGroup1.ColorInputPin_o_Receive( _Data );
}

void PinCallerReceive5::Notify( void *_Data )
{
  TypeConverters::Converter0.Convert( _Data, PinCallerConverterReceive1 );
}

void PinCallerConverterReceive1( void *_Data )
{
  Declarations::Instances::ColorValue1.ClockInputPin_o_Receive( _Data );
}
void PinCallerReceive6::Notify( void *_Data )
{
  TypeConverters::Converter1.Convert( _Data, PinCallerConverterReceive2 );
}

void PinCallerConverterReceive2( void *_Data )
{
  Declarations::Instances::ColorValue2.ClockInputPin_o_Receive( _Data );
}
void PinCallerReceive7::Notify( void *_Data )
{
  Declarations::Instances::RGBWColorMultiMerger1.InputPins_o_Receive( 1, _Data );
}

} // PinCalls

namespace ComponentsHardware
{
void SystemUpdateHardware()
{
}
} // ComponentsHardware

//The setup function is called once at startup of the sketch
void setup()
{
  BoardDeclarations::ElapsedSystemMicros.SystemStarted();
  BoardDeclarations::Instances::ArduinoDigitalChannel_3.SystemInit();
  Declarations::Instances::NeoPixels1.SystemInit();

  Interrupts::Pin3.Init( Interrupts::Handler3 );

  BoardDeclarations::Instances::ArduinoDigitalChannel_3.SystemStart();
  Declarations::Instances::UltrasonicRanger1.SystemStart();
  Declarations::Instances::TArduinoNeoPixelsColorPixelGroup1.SystemStart();
  Declarations::Instances::RampToValue1.SystemStart();
  Declarations::Instances::CompareValue1.SystemStart();
  Declarations::Instances::CompareValue2.SystemStart();
}

// The loop function is called in an endless loop
void loop()
{
  unsigned long AElapsedMicros = BoardDeclarations::ElapsedSystemMicros.GetNewElapsedTime();
  BoardDeclarations::Instances::ArduinoDigitalChannel_3.SystemLoopBegin();
  Declarations::Instances::UltrasonicRanger1.SystemLoopBegin();
  Declarations::Instances::TArduinoNeoPixelsColorPixelGroup1.SystemLoopBegin();
  Declarations::Instances::RampToValue1.SystemLoopBeginElapsed( AElapsedMicros );
  Declarations::Instances::RampToValue1.SystemLoopBegin();
  Declarations::Instances::NeoPixels1.SystemLoopEnd();
}

That's very unlikely to produce any useful results.

That's an awful lot of code for something so simple.

Just reposted the code where I found the project

Hi @jdkitzmiller1988

Why do you need another US sensor?
Will they be in the same direction?
Remember that the irradiation and return of a sound emission is like a cone.
Depending on how the assembly is done, one US can interfere with the other US.

The code you posted looks to be machine-generated code, not really the sort of thing anyone would write.

Yup, from web page linked by OP:

If you want to modify that Visuino project you will have to do it in the Visuino environment.

Add a second "Ultrasonic Ranger(Ping)" component. Connect the Trigger and Echo pins to two Arduino pins. Connect the output to some variable. Use that variable to control something.

If you want to learn how to do the same thing with Arduino programming instead of Visuino programming, you will have to start with an Arduino example.

1 Like

This is autogenerated code by Visuino.
It is automatically highly optimized to reduce memory usage by using many different advanced tricks such as using single bits between bytes for different components, and is also designed to handle multiple sensors simultaneously.
Modifying such code by hand is very complex, and requires deep knowledge of how to optimize code to reduce memory usage (Takes a lot of source code to actually reduce binary footprint).
It is best to use Visuino to modify the original project. It is easy and free for what you are trying to achieve.

each sensor would be place 180 degrees from one another

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.