Code adaptation

Hello everyone … I am a novice in the development and adaptation of codes … I hope someone is willing to help me … I have this code from the internet, which is a RGB Led lamp and I would like to add an ultrasonic sensor HC-SR04 to the project, I wanted the
Led to be activated at a distance of 30cm and that when I finished lighting all the colors there would be an interval of 2min for it to be activated again, I want to put it in my daughter’s room. Thanks for suggestions.

This is the code


  • Projeto 20 – Efeito RGB usando 3 leds (criando cores do arco-íris)


int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup()
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

void loop()
setColor(255, 0, 0); // vermelho
setColor(255, 127, 10); // laranja
setColor(255, 255, 0); // amarelo
setColor(0, 255, 0); // verde
setColor(0, 0, 255); // azul
setColor(75, 0, 130); // indigo
setColor(143, 0, 255); // roxo
setColor(255, 255, 255); // branco
setColor(255, 0, 255); // magenta
setColor(0, 255, 255); // ciano
setColor(0, 0, 0); // preto
setColor(255, 0, 127); // rosa brilhante
setColor(255, 105, 180); // rosa forte

void setColor(int red, int green, int blue)
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

Welcome to the forum.

Unfortunately, that sketch uses delay(). This is a function used for very simple examples but it stops extending sketches without changing large parts of the code. delay() stops the execution of the code and wastes processing cycles. During delay() no other code will be executed.

Have a look at the following example

File → Examples → 02.Digital → BlinkWithoutDelay

This will show you how to blink an LED without stopping everything else.

Next you could move the LED code to another function and use a number parameter to call the color. Use if-else or switch-case to display the color according the number.

For numbers there is a operator called modulo that you can use to create repeating number sequences. e.g.

nextColorNumber = ( nextColorNumber + 1 ) % NUMBER_OF_COLORS;

This should give you enough to get you started. Post your code with the modifications and/or ask some specific question if something is unclear.

Good night,
I asked for help because I don’t know anything about Arduino programming, you can do this modification. I thank

I would rather help you learn than do the work for you. That is what Arduino is all about. Learning and doing things yourself instead of buying a finished product.

Did you already play with some examples?

Good afternoon
I don’t know anything about programming, I just do what the YouTube videos show

Well, would you like to learn how to program? Do you have a bit time in your day to learn something new? Looks like you are busy.

Good afternoon
my time and only on weekends

OK lets try this.

In your code is a function called setColor it has 3 parameters of type int called red, green and blue. You can see how you call that function from loop.
Note: loop() and setup() are functions too. setup() is called once at the beginning and loop() is called again and again automatically for you. They do not have parameters.


  • create a new function with just one parameter of type int
  • move all your code from loop in there and use a switch-case statement to select a color
  • only use the setColor function 13 times and remove the delay()

Have a look at the following documentation

  • then use a for() loop to call your new function, as many times as you have colors, inside loop() use the following documentation (for now you can use delay inside the for() loop)

At the end your sketch should do exactly what it does now but only one call of delay() inside the for() loop. We will deal with that next.

Post your code and any questions you might have.