"ghost music box" is a confusing title. I can imagine a box, but... Where is the ghost? Where is the music?
I do not know how you are using the motor in the music box, but as mentioned, you need to connect your Arduino to a DC motor driver, and supply that motor driver with power directly from a power supply.
As far as the RGBLED registering distance... the following code will use your Common Anode RGBLED connection and change colors with object distance. There is a six inch buffer near the sensor where the RGBLED will be white, then six color steps, each step is three inches wide, for a total of eighteen inches (for all six colors). Beyond twenty-four inches, the RGBLED is again white. If you add to your project an passive piezo buzzer, you can send different tones to the buzzer with each color change, making a "theremin with color" music box...
// https://forum.arduino.cc/t/ghost-music-box-sketch/1450744/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define trigPin A0 //Sensor Trig pin connected to Arduino pin A0
#define echoPin A1 //Sensor Echo pin connected to Arduino pin A1
long distanceInch, duration; // move to global variables
// long distance; // move to local variable in colorLED();
byte redPin = 9, grnPin = 10, bluPin = 11; // for RGBLED
byte motPin = 13; // motor pin - do not connect motor to Arduino pin
int stepOld; // global variable for "color" number
void setup()
{
Serial.begin(115200); // start serial communication for Serial Monitor
pinMode(redPin, OUTPUT); // add red pin
pinMode(grnPin, OUTPUT); // add grn pin
pinMode(bluPin, OUTPUT); // add blu pin
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Simple Circuits");
lcd.setCursor(0, 1);
lcd.print("Distance: inch");
// delay(2000); // not needed
// lcd.clear(); // not needed
// lcd.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0
// lcd.print("Distance:");//Print Message on First Row
}
void loop()
{
// long duration, distance; // move to global variables
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// distance = (duration / 2) / 29.1; // cm not used
distanceInch = duration * 0.0133 / 2; // inch
// lcd.setCursor(9, 0);
// lcd.print(" "); //Print blanks to clear the row
lcd.setCursor(9, 1);
if (distanceInch < 100) lcd.print(" ");
if (distanceInch < 10) lcd.print(" ");
lcd.print(distanceInch);
// lcd.print(" inch"); //Print your units.
// delay(200); //pause to let things settle
int mapdistance = map(distanceInch, 0, 155, 0, 52); // 52 divisions, 3 inches each division
distanceTOcolor(mapdistance); // show distance on RGBLED
}
void distanceTOcolor(int step) { // receive distance value
switch (step) { // 18 inches for color, 3 inches per color, 6 colors
// six inch buffer, so 0 wht, 1 wht
case 2: colorLED(0, 255, 255); break; // red - common Anode, color values inverted
case 3: colorLED(0, 0, 255); break; // yel
case 4: colorLED(255, 0, 255); break; // grn
case 5: colorLED(255, 0, 0); break; // cyn
case 6: colorLED(255, 255, 0); break; // blu
case 7: colorLED(0, 255, 0); break; // mag
default: colorLED(0, 0, 0); break; // wht < 6 and > 24 inches
}
// show current step
if (step != stepOld) {// if step value changes...
stepOld = step; // store new value
Serial.println(step); // ... print step value
}
}
void colorLED(int red, int grn, int blu) { // write color values to RGBLED pins
digitalWrite(redPin, red);
digitalWrite(grnPin, grn);
digitalWrite(bluPin, blu);
}
diagram.json for the nerds on wokwi.com
{
"version": 1,
"author": "xfpd",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-nano",
"id": "nano",
"top": 41.4,
"left": -94.5,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-hc-sr04",
"id": "ultrasonic1",
"top": -228.9,
"left": -176.9,
"attrs": { "distance": "18" }
},
{
"type": "wokwi-lcd1602",
"id": "lcd1",
"top": -147.2,
"left": -52,
"attrs": { "pins": "i2c" }
},
{ "type": "wokwi-rgb-led", "id": "rgb1", "top": -5.6, "left": 154.7, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 32.75,
"left": 86.4,
"attrs": { "value": "333" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 42.35,
"left": 86.4,
"attrs": { "value": "333" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 51.95,
"left": 86.4,
"attrs": { "value": "333" }
}
],
"connections": [
[ "ultrasonic1:GND", "nano:GND.1", "black", [ "h-1.2", "v297.6" ] ],
[ "ultrasonic1:ECHO", "nano:A1", "green", [ "v0" ] ],
[ "ultrasonic1:TRIG", "nano:A0", "green", [ "v0" ] ],
[ "ultrasonic1:VCC", "nano:5V", "red", [ "v0" ] ],
[ "lcd1:SCL", "nano:A5", "green", [ "h-19.2", "v173.1" ] ],
[ "lcd1:SDA", "nano:A4", "green", [ "h-9.6", "v173" ] ],
[ "lcd1:VCC", "nano:5V", "red", [ "h-57.6", "v220.9" ] ],
[ "lcd1:GND", "nano:GND.1", "black", [ "h-28.8", "v249.6" ] ],
[ "nano:5V", "rgb1:COM", "red", [ "h0" ] ],
[ "rgb1:R", "r1:2", "violet", [ "v0" ] ],
[ "r1:1", "nano:9", "violet", [ "v0" ] ],
[ "rgb1:G", "r2:2", "green", [ "v0" ] ],
[ "r2:1", "nano:10", "green", [ "v0", "h-19.2", "v-28.8" ] ],
[ "rgb1:B", "r3:2", "blue", [ "v0" ] ],
[ "r3:1", "nano:11", "blue", [ "v0", "h-28.8", "v-38.4" ] ]
],
"dependencies": {}
}