Ghost Music box Sketch

I need help with the code for my Ghost music box!!!!

I whould like for the Motoer to start when the sensor detects an object.
also would like for the RGB to on when the motor start and for the color to go from Green (farther away and Red when close by.

Can anyone help me with this Please

Hello. If you want help with your code, you'll first have to show us your code (properly formatted with the handy <CODE/> tool shown in the toolbar of the reply box), and tell us what it isn't doing that you think it should be doing. We can't read minds, so if you don't show us, we can't help.

Oh, and we're not a code writing service if that's what you're expecting. We'll help you fix your code; we won't write it for you.

PS on the hardware side, there's no way you can drive a motor with an unaided Arduino output pin. It doesn't have the current capacity, and it may well die trying.

Here is what I have so far I can get the Sensor to detect items and show on the display. I am not sure how to write in the RGB?? As for the moter I can eliminate that for now..Use code tags to format code for the forum

#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;



void setup()

{

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  lcd.init();

  lcd.backlight();

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Simple  Circuits");

  delay(2000);

  lcd.clear();

  lcd.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0

  lcd.print("Distance:");//Print Message on First Row

  lcd.setCursor(0,1);

  lcd.print("Distance:");

}



void loop()

{

  long duration, distance;

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration/2) / 29.1;

  distanceInch = duration*0.0133/2;



  

  

  lcd.setCursor(9,0);   

 

  


  lcd.print("                         "); //Print blanks to clear the row

  lcd.setCursor(9,1);   

  lcd.print(distanceInch);

  lcd.print(" inch");  //Print your units. 

  

  delay(200); //pause to let things settle

}

It is connected to pins 9, 10, 11. So you can use digitalWrite() on one or more of those pins. Do not forget to set the pins as OUTPUT in setup().

I can see from your diagram that the RGB led is the common anode type. Therefore you must write LOW to the pin to light it up and HIGH will switch it off.

Do you want the led to change suddenly between green and red at a set distance? In that case you will need to use an if() statement and you can use digitalWrite() to set the colour.

But if you want the colour to change gradually between red and green, via yellow, as the distance changes, then you will need to use analogWtite() and map() functions.

"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": {}
}

And... if you want six tones accompanying the six RGB colors...

// 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
byte redPin = 9, grnPin = 10, bluPin = 11; // for RGBLED
byte motPin = 13; // motor pin - do not connect motor to Arduino pin
byte buzPin = 3;

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(buzPin, OUTPUT); // passive buzzer

  pinMode(trigPin, OUTPUT); // HC-SR04
  pinMode(echoPin, INPUT); // HC-SR04

  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Simple  Circuits");
  lcd.setCursor(0, 1);
  lcd.print("Distance:   inch");
}

void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceInch = duration * 0.0133 / 2; // inch

  lcd.setCursor(9, 1);
  if (distanceInch < 100) lcd.print(" ");
  if (distanceInch < 10) lcd.print(" ");
  lcd.print(distanceInch);

  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); tone (buzPin, 440); break; // red - common Anode, color values inverted
    case 3: colorLED(0, 0, 255); tone (buzPin, 392); break; // yel
    case 4: colorLED(255, 0, 255); tone (buzPin, 349); break; // grn
    case 5: colorLED(255, 0, 0); tone (buzPin, 330); break; // cyn
    case 6: colorLED(255, 255, 0); tone (buzPin, 294); break; // blu
    case 7: colorLED(0, 255, 0); tone (buzPin, 262); break; // mag
    default: colorLED(0, 0, 0); noTone(buzPin); break; // wht  < 6 and > 24 inches
  }

  // debug printing steps
  // if (step != stepOld) {// if step value changes...
  //   stepOld = step; // store new value
  //   Serial.print(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);
}