RGB code with funny results

Hello guys! I am new to Arduino(today I received my Arduino Uno Starter Kit!), and I am really excited. I've been waiting over a year to get my hands on one of these :D.

Well I started with the easy part first, learning from a few tutorials. I blinked a led for the first time, then I tried doing it with 4 leds in a row and... it's just wonderful to see that programming can have effects in the real world.

But now I am stuck with something I wanted to do next(I'm not really a guy to respect the order of tutorials :), I basically inspired myself from a potentiometer control and a button reading tutorial to do this).

I wanted to control a RGB LED like this: pressing a button adjusts the "color channel" for the potentiometer to control.
I don't know what's wrong with my code, but I have a funny result: pressing the button has no effect(it should even blink de 13th input LED, but it stays open). The Red light is on whatever I do. Turning the potentiometer seems to light up the blue colour, and when that's maxed out, it will light the green one until all of the 3 colours are maxed out.

Here's a diagram of my experiment:

And here's the code:

/*
  AnalogReadSerial
 Reads an analog input on pin 0, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */
int ledPinIND= 13;
int Rpin= 1;
int Gpin= 2;
int Bpin= 3;
int patincrement= 0;
int Rval=0, Gval=0, Bval=0;
void setup() {
  pinMode(ledPinIND, OUTPUT);
  pinMode(Rpin, OUTPUT);
  pinMode(Gpin, OUTPUT);
  pinMode(Bpin, OUTPUT);
  pinMode(0, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(ledPinIND, LOW);
  int sensorValue = digitalRead(0);
  if(sensorValue==1){ digitalWrite(ledPinIND, HIGH);
  delay(150);
  patincrement++;  if(patincrement>2){patincrement=0;}  
  }
  int potValue = analogRead(A0);
  Serial.println(potValue, DEC);
  if(patincrement==0){
    Rval= potValue/5;}
  if(patincrement==1){
    Gval= potValue/5;}
   if(patincrement==2){
    Bval= potValue/5;}
   analogWrite(Rpin, Rval); analogWrite(Gpin, Gval); analogWrite(Bpin, Bval);
}

Also, I partly understand why we need a 10K resistor for the button, but I don't understand why do the LEDs need a 220 ohm resistor(I still used 220ohm resistors on the RGB because I am confused and don't want to break my Arduino). Lowering the voltage and current even for the other colors besides blue should not exceed 100 ohms( for 5V output, which is what Arduino supplies from what I understand).

Hi,
I'm not saying it's wrong but...
int potValue = analogRead(A0);
might need to be...
int potValue = analogRead(0);

Also, you are setting digital 0 as an input and initializing the serial port which uses the same pins.

I think your button might be installed sideways. With a 4-pin button, pairs of pins are always connected and the button press then connects the two pairs together. If your button is in sideways you have the 10K pull-down resistor connected to +5 instead of the digital input pin. This would cause the input pin to read HIGH all the time.

You should only need to use two pins on the button. Either use a pull-up resistor between the digital input and +5 and connect the button between the digital input and Ground, or use a pull-down resistor between the digital input and Ground and connect the button between the digital input and +5. The chip has internal pull-up resistors (enable with digitalWrite(0, HIGH)) so the first choice requires no external resistor. When the button is open the pin will read HIGH (because of the pull-up) and when the button is closed the pin will read LOW (because it is grounded).

analogWrite() only works on pins marked PWM (Pulse Width Modulation). Your Blue channel on pin 3 will work as expected but the other two will be HIGH for analogWrite values over 127 and LOW for analogWrite values under 128. Use pins 3,5,6,9,10,and 11 for the built-in PWM analogWrite.

The LED resistors are there to keep from burning out the LED and to keep from drawing more power from the Arduino pin than it can safely provide.

Oh, yes... What Coyote said: you can't use digital pins 0 or 1 when you use the serial port. Those are the serial data lines going to the USB-to-Serial chip. Move the button to pin 4 and the three colors to 3,5,and6.

Hey, thank you very much, it now works!

The LED resistors are there to keep from burning out the LED and to keep from drawing more power from the Arduino pin than it can safely provide.

I know, but I am intrigued why I have to use 220 ohm resistors when I could safely use, say, a 60 ohm for a blue led for example.

I'm not saying it's wrong but...
int potValue = analogRead(A0);

It isn't.

You're doing analogWrite.
Have you checked that the pins you're writing to support analogWrite?

Can I suggest that before you attempt to get three LEDs doing what you want to do, you get just one LED to do it?

Already did it with one LED :).

So, can anyone bother to answer me this?

I know, but I am intrigued why I have to use 220 ohm resistors when I could safely use, say, a 60 ohm for a blue led for example.

Zeratus:
So, can anyone bother to answer me this?

I know, but I am intrigued why I have to use 220 ohm resistors when I could safely use, say, a 60 ohm for a blue led for example.

Most electronic devices will draw as much power as they can, you need to limit the current through the LED by using a resistor. In order to determine the required resistance you use Ohms Law.

The specific equation to determine the required resistance value is the following:

R = (Vs-Vf) / I

where:
R is the required resistance
Vs is your source voltage
Vf is the forward voltage of the LED
I is the specified current allowed through the LED

As an example, let's say we are using a 5v supply, a blue LED with forward voltage of 3.2v and we require a maximum of 20mA be allowed to flow through the LED.

We use the above formula and get the following:

R = (Vs - Vf) / I = (5 - 3.2) / 0.02 = 90

Therefore you need at least a 90 Ohm resistor to limit the current to 20mA. Now, 90 Ohm is not a standard value so you round up to the next standard value which is 100 Ohms. In a case like this a standard 1/8 watt 100 Ohm resistor would be fine. In higher current applications you must also take into account the power draw (watts) and scale your hardware accordingly.

Another great resource for LED information is http://led.linear1.org/

Hope that helps.