Bluetooth Proximity Sensor - Buzzer volume varying with distance

Hi,

I have been following this tutorial: Bluetooth Proximity Detection | FireBeetle ESP32 - YouTube
and have got my circuit and code running as shown (please see attached image of my circuit)

I have tried to modify the code to get a buzzer to vary its volume depending on the distance from the circuit. I am using a Firebeetle ESP32 chip and a bluetooth tag.

I am still very new to coding so I apologise if this seems a stupid way of doing it...

Here is my current code and it verifies with no problems:

 #include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>

int rssi;

// const int IO2 = 2;
const int IO5 = 5;          //(No resistor, Loudest)
const int IO13 = 13;        //(1k resistor, Medium noise level)
const int IO10 = 10;        //(5k resistor, Quietest)
const int CUTOFF = -60;

void setup() {
//  pinMode(IO2, OUTPUT);
  pinMode(IO5, OUTPUT); //Loudest
  pinMode(IO13, OUTPUT); // Medium
  pinMode(IO10, OUTPUT); // Quietest
  BLEDevice::init("");
}
void loop() 
{
 BLEScan *scan = BLEDevice::getScan();
 scan->setActiveScan(true);
 BLEScanResults results = scan->start(1);
// int best = CUTOFF;
 for (int i = 0; i < results.getCount(); i++) {
 BLEAdvertisedDevice device = results.getDevice(i);
    int rssi = device.getRSSI();
//    if (rssi > best) {
//    best = rssi;
//    }
    }    
    if (rssi >= -60 && rssi < -50) 
    {
    digitalWrite (IO5,HIGH);  
    }
    else if (rssi >= -50 && rssi < -40) 
    {
    digitalWrite (IO13,HIGH);
    }
    else if (rssi >= -40) 
    {
    digitalWrite (IO10,HIGH);
    }
 }
 }

If i have missed any information from here please dont hesitate to ask.

If you only want one of your pins active, you need to turn off the other two pins. Your current code never turns off anything.

Also, because of your design, you really don't want to turn off the other two pins since that will create a path to ground with your resistors in parallel. You really want to switch the non-used pins back to INPUT so they are in a high impedance state and don't affect your circuit.

If you follow this path, the pin that you want to turn on needs to be set as an OUTPUT as well as driven high.

Thank you, I will give that a go.