Problems - Ultrasonic Sensor


Im new to this site and I hope I put this problem on the correct forum topic.

I’m currently building this project … /?ALLSTEPS

Everything is setup and working properly,
but I would like to add something else which I’m having issues with.

I would like to add a Ultrasonic sensor into that project. … R04-lg.jpg

I really suck at programming, I mean, I really SUCK. Hoping someone could do this for me or show me the right direction on how to accomplish this.
Basically, I just want the different lights to light up when an object is either far or close to the sensor.

Here’s my code so far(everything works except for the ultrasonic):

//pins ultrasonic
int triggerPort = A4;
int echoPort = A5;

const int RED = 9; // define digital output pins for individual red,
const int GREEN = 10; //green and blue channels
const int BLUE = 11;

int colorRed;
int colorGreen;
int colorBlue;

const int POT1 = A0; // define analog input pins for three potentiometers
const int POT2 = A1;
const int POT3 = A2;
const int POT4 = A3;

const int BUTTON = 2; // define digital input pin for the switch

int val = 0; // stores the state of the switch input pin

int FADESPEED = 0; // initiate fade speed set by potentiometer

int r = 0; // initialize the red, green and blue values
int g = 0;
int b = 0;

void setup(){
pinMode(RED, OUTPUT); // define digital pins as outputs and inputs as needed
pinMode(BLUE, OUTPUT);
pinMode(triggerPort, OUTPUT);
pinMode(echoPort, INPUT);

void loop(){
digitalWrite(triggerPort, LOW);
digitalWrite(triggerPort, HIGH);
digitalWrite( triggerPort, LOW);
long time = pulseIn(echoPort, HIGH);
long range = 0.034 * time / 2;

colorRed = (range20) % 255;
colorBlue = (range
30) % 255;
colorGreen = (range*50) % 255;
analogWrite(RED, colorRed);
analogWrite(BLUE, colorBlue);
analogWrite(GREEN, colorGreen); delay( 400 );

val = digitalRead(BUTTON); // read the input value from the toggle switch

if (val == HIGH){
// code for RGB color fade
FADESPEED = analogRead(POT4)/10; // set the fade speed by reading analog input from 4th potentiometer
// analogRead will output a number between 0 and 1023, and “delay”
// is in milliseconds, so the biggest delay you’ll get here is about
// 1/10 of a second. Divide by a different number to change the max
// fade time.
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(RED, r);
FADESPEED = analogRead(POT4)/10; // check the fade speed continuously, otherwise
// it won’t update until it’s gone through a complete cycle.
// Probably not the most efficient way to do this…
// fade from violet to red
for (b = 255; b > 0; b–) {
analogWrite(BLUE, b);
FADESPEED = analogRead(POT4)/10;
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREEN, g);
FADESPEED = analogRead(POT4)/10;
// fade from yellow to green
for (r = 255; r > 0; r–) {
analogWrite(RED, r);
FADESPEED = analogRead(POT4)/10;
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUE, b);
FADESPEED = analogRead(POT4)/10;
// fade from teal to blue
for (g = 255; g > 0; g–) {
analogWrite(GREEN, g);
FADESPEED = analogRead(POT4)/10;
else {
// code for individual RGB control with potentiometers
r = analogRead(POT3)/4; // read values from the 3 potentiometers and divide by 4 to set brightness
g = analogRead(POT2)/4; // note that analog read is 10-bit (0-1023), analog write is an 8-bit PWM
b = analogRead(POT1)/4; // signal so you need to divide this value by 4.

analogWrite(RED, r); // write analog values to red, green and blue output pins
analogWrite(GREEN, g);
analogWrite(BLUE, b);


Here’s a video of what I’m trying to accomplish

Thanks in advance!

digitalWrite(triggerPort, LOW);
digitalWrite(triggerPort, HIGH);

The pin will be low for how long?

You might want to look for the NewPing library.

A few seconds. Not long. 6-7 secs, depending where the object is on the sensor.

unXcellent: A few seconds. Not long. 6-7 secs, depending where the object is on the sensor.

Don't know where you got that guess from, but it's VERY wrong. This is the trigger pin we are talking about.

Even if you were talking about the echo pin, 6 seconds would be a return from an object about 1 km away. Don't think the sensor has that sort of range, do you?

Really. Look at the NewPing Library. You can find it listed on

A few seconds. Not long. 6-7 secs, depending where the object is on the sensor.

More like 100 nanoseconds. The pin is set LOW. Then, the correct pin to set high is determined, and set high by the next statement. Mapping the pin number you specify to the physical pin for your Arduino takes most of that time. Turning the physical pin off takes very little time.