controlling an rgb leds brightness while joystick changes it's colors

Hey, I have created a program which makes a joystick change an RGB leds colors and the program works but i want to add another feature which allows you to control the brightness of the led using a potentiometer i have tried one thing but the joystick does not work after i have added the code for the potentiometer please tell me what i have done wrong.

int R_PIN = 9; //Pin for the red portion of the LED
int G_PIN = 10; //Pin for the red portion of the LED
int B_PIN = 11; //Pin for the red portion of the LED
int JX_PIN = 0; //Pin for the Joystick X Direction (Analog)
int JY_PIN = 1; //Pin for the Joystick Y Direction (Analog)
int JSW_PIN = 2; //Pin for the Joystick Switch (Analog)
int analogPin = A3;
int val = 0;

int joyX; //Variable to store the Joystick X reading
int joyY; //Variable to store the Joystick Y reading
int joySW; //Variable to store the Joystick Switch Reading

//Setup the Arduino
void setup()
{
pinMode(R_PIN, OUTPUT); //Make the pin you used an output on the Arduino
pinMode(G_PIN, OUTPUT); //Make the pin you used an output on the Arduino
pinMode(B_PIN, OUTPUT); //Make the pin you used an output on the Arduino
}

//This function will update the RGB LED when called
void setRGB(int rLed, int gLed, int bLed)
{
analogWrite(R_PIN, rLed);
analogWrite(G_PIN, gLed);
analogWrite(B_PIN, bLed);
}

//This code will run infinitely
void loop()
{

delay(100); //Delay 100mS to slow to 10 readings per second
joyX = analogRead(JX_PIN); //Read the X position
joyY = analogRead(JY_PIN); //Read the Y position
joySW = analogRead(JSW_PIN); //Read the Switch, 255 or 0

setRGB(joyX, joyY, joySW); //Set the RGB output to the inputs from the joystick
val = analogRead(analogPin);
analogWrite(R_PIN, val / 4);
analogWrite(G_PIN, val / 4);
analogWrite(B_PIN, val / 4);

}

Usual advice is to NOT use the RGB colour-space if dimming and colour fidelity is a requirement.
Try HSV instead.

Please remember to use code tags when posting code

Is it just me or did you not initialize any of your input?

Also, are you using pin 0, 1, and 2 for analog input? Or is it a typo and you meant A0, A1, and A2? Plus, pin 0 and 1 is off limits on most arduinos.

linearity64:
Is it just me or did you not initialize any of your input?

Also, are you using pin 0, 1, and 2 for analog input? Or is it a typo and you meant A0, A1, and A2? Plus, pin 0 and 1 is off limits on most arduinos.

Nothing wrong with any of that. RT(F)M

Why do this

setRGB(joyX, joyY, joySW); //Set the RGB output to the inputs from the joystick

then microseconds later do this

analogWrite(R_PIN, val / 4);
  analogWrite(G_PIN, val / 4);
  analogWrite(B_PIN, val / 4);

Apart from anything else, val / 4 will be the same for all 3 colours

Why not read the 3 values from the joystick, read the value from the pot, adjust each of the separate values from the joystick using the value from the post then write the adjusted values to the LEDs ?

TheMemberFormerlyKnownAsAWOL:
Nothing wrong with any of that. RT(F)M

They really don't matter?
Huh. You learn something new everyday, I guess.

linearity64:
Is it just me or did you not initialize any of your input?

Also, are you using pin 0, 1, and 2 for analog input? Or is it a typo and you meant A0, A1, and A2? Plus, pin 0 and 1 is off limits on most arduinos.

Arduino pins default to INPUT

When using analogRead() the values used as parameters are interpreted as analogue pin numbers, so it is not strictly necessary to use the A* format although the intent of the sketch is clearer if you do and it also reminds you to wire the inputs to the correct pins

UKHeliBob:
Arduino pins default to INPUT

When using analogRead() the values used as parameters are interpreted as analogue pin numbers, so it is not strictly necessary to use the A* format although the intent of the sketch is clearer if you do and it also reminds you to wire the inputs to the correct pins

Gotcha. Didn't know this. Thanks!

When using analogRead() you should NOT use pinMode() on the pins.

If you use numbers (0-5) then you will be setting pinMode() on the DIGITAL pins with those numbers.

For chips like the Nano with A6 (20) and A7 (21) pins, the tables used to set pinMode don't go up that high! Reading off the end of the digital_pin_to_port_PGM table and digital_pin_to_bit_mask_PGM table can cause the wrong bit in the wrong port to get set.

I have a mega 2560 board but i heard the brightness can be set without any code too is that true?

arduino12_5:
I have a mega 2560 board but i heard the brightness can be set without any code too is that true?

I don't see how

Where did you hear that ?