How to program a button to star a RGB Led with a servo motor

Hi can someone can help me devolping this code:

  • The servomotor is in the initial position, zero degrees. The RGB led is in color

  • When the button is pressed the first time, the motor rotates 180 degrees and returns, to
    high speed. The Led is orange as a warning signal.

  • When the button is pressed the second time, the motor rotates 180 degrees and returns to
    average speed. The RGB Led is in magenta color as a signal of

  • When the button is pressed the third time, the motor rotates 180 degrees and returns to
    low speed. The RGB led is in cyan color as a low risk signal.

  • When pressed the fourth time, the system is in initial condition.

In this way the elements are distributed in the arduino:

I would be very grateful for your help.

Welcome to the forums. What have you tried? This forum is for helping people with problem in their code, not writing code for them.

Have you tried the servo examples? I would start with those.

You can have a counter that keeps track of how many times the button is pressed and adjust some sort of delay variable you use to control the speed of the servo.

Give it a shot. If/When you get stuck, post your best effort, along with any error messages and people will help.

Hi, yeah This is what I have so far but I don’t really know how to program the servomotor so that when I turn it 180 degrees it gives me the colors I need.

#include <Servo.h>

Servo gol;
long distance;
int red = 3;
int green = 5;
int blue = 6;

void setup() {
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);

void loop() {
distance = 100;
distance = 20;

void top_open() {
if(distance < 30) {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
else {
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);

Hello and welcome to the forum, the best help I can give you is some suggestions on how to help yourself, here are some things you will need to be able to do before you can begin this project:

  1. Learn the basics of the Arduino IDE and the board you will be using, look at and try the Examples that come with the IDE.

  2. Learn how to use the push button to control an LED (hint its one of the examples).

  3. Learn how to DIM an LED (see the above hint)

  4. Learn how to control a servo

  5. Learn how to use serial print to debug and see what is going on

  6. Learn how to count button presses and how to use the count to decide the action

  7. Learn how to use PWM from dimming LED above to control your 3 color LED (you will need to know what level each color needs to be to mix the color you want)

Once you have learned these things you will be able to start building your program, some where in this forum there is a very good tutorial about building a program try searching for that it covers a lot of what you are looking for.

Final hint and comment, firstly thank you for using a topic heading that makes sense and say what you need help with. You will get a lot more help when you can show you are willing to search for yourself and when you can show you are trying code.

The servo is unlikely to ever control the LED.
YOU need to do both at the appropriate times in the control sequence.
As mentioned above, work on each small piece of the challenge separately.

I’m sure many on here could have this running the way you want it within a couple of hours, but if we do it, you’ll learn nothing, so catch a breath, and settle in for the learning curve.

For a stone cold newbie to make this work reliably will probably take a week if you work on the basics and understand why you’ve done specific things.

Start with several small sub-projects.

  • Detecting switch states.
  • Controlling LEDs (RGB is three LEDs)
  • Controlling an RC servo.
  • millis() timing.

They all need to work together.

Have a look at the "useful Links" post at the top of the programming questions page, almost everything you need to know is there...