Example 19 ‐ Buzzer tone in relation to distance - GSTCH/SketchMadeEASY GitHub Wiki

Buzzer tone frequence in relation to distance measured by an Ultrasonic Sensor. If Sketch Made EASY is installed, it can be opened via menu: "File:/Example/SketchMadeEASY/Examples/18-StepperAngleControlledByJoystick".

Schematic

Code

#include <Easy.h>

//****************************************************************
#define ECHO_PIN 30
#define TRIGGER_PIN 31

#define MEASURE_INTERVAL_MSEC 1000
#define BUZZER_FREQUENCY_MIN 20
#define BUZZER_FREQUENCY_MAX 1000
#define BUZZER_PIN 4

void setup()
{
  //((*** Initialize: Configure your sketch here....
  //** Define Input
  Input* distanceSensor = new UltrasonicRangefinder(ECHO_PIN, TRIGGER_PIN, MEASURE_INTERVAL_MSEC); 
  
  //** Define Actuators:
  Actuator* buzzer = new Buzzer(BUZZER_PIN, BUZZER_FREQUENCY_MIN, BUZZER_FREQUENCY_MAX);

  //** Define logic with condition and relation
  Relation* relationBuzzerOn = new Relation1to1(NULL, buzzer, distanceSensor);
  // ***))

  // Initialize control
  ControlManagerFactory::GetControlManager()->Setup();
}

//*****************************************************************
void loop() {
  //*** Run: No additional code is required
  ControlManagerFactory::GetControlManager()->Loop();

  // Depending on Arduino it needs a short delay. Do not add any other delays!
  delay(1);
}
⚠️ **GitHub.com Fallback** ⚠️