New in Arduino, I need help for my code!

Hi, I’m new to Arduino and I am building a moving head with a rgb light inside it. I find two codes; one for the light an another for the servo’s. I want to combine them. I tried lots of ways but it doesn’t work. Please help me :slightly_frowning_face: . This is the code I wrote:

#include <Servo.h>

int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer

int potPin2 = 0;
int potVal2 = 0;

int potPin3 = 0;
int potVal3 = 0;

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9;   // Red LED,   connected to digital pin 9
int grnPin = 10;  // Green LED, connected to digital pin 10
int bluPin = 11;  // Blue LED,  connected to digital pin 11

// Program variables
int redVal = 0;   // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;

Servo myservo;

Servo myservo2;



void setup()
{
  
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT);
  myservo.attach(8);
  myservo.attach(7);
}
void loop() 
{ 
  {
    potVal = analogRead(potPin);   // read the potentiometer value at the input pin

  if (potVal < 341)  // Lowest third of the potentiometer's range (0-340)
  {                  
    potVal = (potVal * 3) / 4; // Normalize to 0-255

    redVal = 256 - potVal;  // Red from full to off
    grnVal = potVal;        // Green from off to full
    bluVal = 1;             // Blue off
  }
  else if (potVal < 682) // Middle third of potentiometer's range (341-681)
  {
    potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255

    redVal = 1;            // Red off
    grnVal = 256 - potVal; // Green from full to off
    bluVal = potVal;       // Blue from off to full
  }
  else  // Upper third of potentiometer"s range (682-1023)
  {
    potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255

    redVal = potVal;       // Red from off to full
    grnVal = 1;            // Green off
    bluVal = 256 - potVal; // Blue from full to off
  }
  analogWrite(redPin, redVal);   // Write values to LED pins
  analogWrite(grnPin, grnVal); 
  analogWrite(bluPin, bluVal);  
  }

  {  
  potVal2 = analogRead(potPin2);     
  potVal2 = map (potVal2, 0, 766, 0, 180);     
  myservo.write(potVal2);                                           

  potVal3 = analogRead(potPin3);            
  potVal3  = map(potVal3 , 0, 766, 90, 180);    
  myservo2.write(potVal3);                  
  
 delay(150);    
 }                       
}

This is the code I use for my RGB LED:

/*
* Code for making one potentiometer control 3 LEDs, red, grn and blu, or one tri-color LED
* The program cross-fades from red to grn, grn to blu, and blu to red
* Clay Shirky <clay.shirky@nyu.edu> 
*/

// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9;   // Red LED,   connected to digital pin 9
int grnPin = 10;  // Green LED, connected to digital pin 10
int bluPin = 11;  // Blue LED,  connected to digital pin 11

// Program variables
int redVal = 0;   // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;

void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 
}

// Main program
void loop()
{
  potVal = analogRead(potPin);   // read the potentiometer value at the input pin

  if (potVal < 341)  // Lowest third of the potentiometer's range (0-340)
  {                  
    potVal = (potVal * 3) / 4; // Normalize to 0-255

    redVal = 256 - potVal;  // Red from full to off
    grnVal = potVal;        // Green from off to full
    bluVal = 1;             // Blue off
  }
  else if (potVal < 682) // Middle third of potentiometer's range (341-681)
  {
    potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255

    redVal = 1;            // Red off
    grnVal = 256 - potVal; // Green from full to off
    bluVal = potVal;       // Blue from off to full
  }
  else  // Upper third of potentiometer"s range (682-1023)
  {
    potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255

    redVal = potVal;       // Red from off to full
    grnVal = 1;            // Green off
    bluVal = 256 - potVal; // Blue from full to off
  }
  analogWrite(redPin, redVal);   // Write values to LED pins
  analogWrite(grnPin, grnVal); 
  analogWrite(bluPin, bluVal);  
}

This is the code I use for my servo:

#include <Servo.h>

Servo myservo;

int potpin = 0;
int val;

Servo myservo2;

int potpin2 = 1;
int val2;

void setup()
{
  myservo.attach(9); 
  
  myservo2.attach(8);
}

void loop() 
{ 
  val = analogRead(potpin);           
  val = map(val, 0, 766, 0, 180);     
  myservo.write(val);                                           

  val2 = analogRead(potpin2);            
  val2  = map(val2 , 0, 766, 90, 180);    
  myservo2.write(val2);                  
  
 delay(150);                           
}

PLEASE COULD SOMEBODY HELP ME?

Apart from anything else you will not be able to use PWM output on pins 9 and 10 if you use the servo library.

See Servo - Arduino Reference

You'll have to fix this:

 myservo.attach(8);
  myservo.attach(7);

This simple merge demo may give you some ideas.

...R