From the beginning

Hoping this time you can understand what am I doing, last time it was a mess and you couldn't read my sketches.
I'm going to trow a simple sketch and maybe you guys can help me out from there.
This is Paul's idea cause what I done before was wrong.
Turn an LED on and off

#define LED 3
#define ONOFFPIN 7

int btnPressCount = 0;
int btnState = LOW;
int lastBtnState = LOW;

void setup() 
{ 
   pinMode(LED, OUTPUT);     
   pinMode(ONOFFPIN, INPUT); 

  Serial.begin(9600);
}

void loop()
{ 
  btnState = digitalRead(ONOFFPIN);
  if((btnState == HIGH) && (lastBtnState == LOW)) // Switch is pressed and was not
  {
     btnPressCount++; // Count this press
     Serial.print("count = ");
     Serial.println(btnPressCount);
  }

  btnPressCount = btnPressCount % 2; // Keep count in the range 0 to 4 
  lastBtnState = btnState;

  switch(btnPressCount)
  {
    case 0:
      analogWrite(LED,0);
      break;
  
    case 1:
      analogWrite(LED,255);
      break;
  }
}
/code]

next I added RGB LEDs, instead only one

#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6

#define ONOFFPIN 7

int btnPressCount = 0;
int btnState = LOW;
int lastBtnState = LOW;

void setup() 
{ 
  pinMode(REDPIN, OUTPUT);     //tell Arduino LED 
  pinMode(GREENPIN, OUTPUT);     // are outputs          
  pinMode(BLUEPIN, OUTPUT);             
 
  pinMode(ONOFFPIN, INPUT); 

  Serial.begin(9600);
}  

void loop()
{ 
  btnState = digitalRead(ONOFFPIN);
  if((btnState == HIGH) && (lastBtnState == LOW)) // Switch is pressed and was not
  {
     btnPressCount++; // Count this press
     Serial.print("count = ");
     Serial.println(btnPressCount);
  }
  
   btnPressCount = btnPressCount % 2; // Keep count in the range 0 to 4 
  lastBtnState = btnState;

  switch(btnPressCount)
  {
    case 0:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 0);
     break; 
     
    case 1:
  analogWrite(REDPIN, 255);  
  analogWrite(GREENPIN, 255);    
  analogWrite(BLUEPIN, 255);
     break;
  }
}/code]

Was there a question?
You don't need to set pin modes for PWM pins.

AWOL:
You don't need to set pin modes for PWM pins.

I know, but In the future I'm going to fade them

Let me give you what I have in mind.

   +--------------+
  |    on        | -------> ONOFFPIN
  +--------------+
         |
         |
         |
+-----------------+ --------> UPPIN switchcases and brightness ++ 
|       color     |
|      cases      |
|                 |
+-----------------+ --------> DNPIN switchcases and  brightness -- 
         |
         V
 +--------------+
  |     loop     |
  |   sketches   |-----------> LOOPPIN to select different sketches (not yet)
  +--------------+
         |
         |
         V
  +---------------+
  |               |
  |      off     |------------> ONOFFPIN
  |               |
  +---------------+/code]

I can help you I just don't get exactly what your problem is. State your problem and I am sure I will be able to give you a answer.

There is not problem yet, what I want to do is add the color cases sketch to the on/off sketch, which with two more switches I'm going to change colors up and down.
something like:

void setup() 
{ 
  pinMode(REDPIN, OUTPUT);     //tell Arduino LED 
  pinMode(GREENPIN, OUTPUT);     // are outputs          
  pinMode(BLUEPIN, OUTPUT);               
    
  pinMode(UPPIN, INPUT);     
  pinMode(DNPIN, INPUT);
}

void loop()
{ 
 btnStateUp = digitalRead(UPPIN);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;                                 // Count this press
   lastBtnStateUp = btnStateUp;                     // Use btnPressCount
 
 btnStateDn = digitalRead(DNPIN);
 if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
   btnPressCount--;                                  // Count this press
   lastBtnStateDn = btnStateDn;                      // Use btnPressCount
     
     
  //this part mantains the counts equal to 0 
  // so it doesn't make the btnPressCount unsigned
  if(btnPressCount < 0)  // <-- Add me
      btnPressCount = 0; // <-- And me
  
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6
                 

  switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
     
   case 1:
  analogWrite(REDPIN, 255);   // on
  analogWrite(GREENPIN, 0);    // off
  analogWrite(BLUEPIN, 0);
     break;
   
    case 2:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 0 );     
     break; 
     
    case 3:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
   
    case 4:
  analogWrite(REDPIN, 255);   
  analogWrite(GREENPIN, 255);     
  analogWrite(BLUEPIN, 0);
     break;
   
    case 5:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
     
    case 6:
  analogWrite(REDPIN, 255);  
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
    
}
}/code]

so question #1. if I join the two sketches do I need to use two int btnPressCount = 0;
or only one?
Is this right?

#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6

#define ONOFFPIN 7

#define UPPIN 4
#define DNPIN 2

int btnPressCount = 0;
int btnOnoffState = LOW;
int lastBtnOnoffState = LOW;

int btnStateUp = LOW; 
int lastBtnStateUP = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;/code]

Quote from: AWOL on 12-09-2011, 19:47:21
You don't need to set pin modes for PWM pins.

I know, but In the future I'm going to fade them

You don't need to set pin modes for PWM pins.

if I join the two sketches do I need to use two int btnPressCount = 0;
or only one?

Use one. Try compiling. Then, add another one. Try compiling again. Let us know what you find.

You don't need to set pin modes for PWM pins./quote]
Why is that? is it because I'm using serial.begin?

Why is that?

Think about it for a minute. The analogWrite() can ONLY write to a pin. So, if you can analogWrite(), you must want to write to the pin. Therefore, the analogWrite() function KNOWS that the pin must be an OUTPUT pin. So, it makes it so.

Therefore, you don't need to.

It has nothing to do with using/not using Serial.begin().

Therefore, you don't need to.

Got it.

Now, before giving you the joined sketches, I tried to use the serial.begin in the color cases sketch, and I see the numbers rolling in the serial monitor.
My question is Why the numbers are rolling in this sketch and the on/off sketch doesn't?

#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6

#define UPPIN 4
#define DNPIN 2

int btnPressCount = 0;

int btnStateUp = LOW; 
int lastBtnStateUp = LOW;

int btnStateDn = LOW;
int lastBtnStateDn = LOW;

void setup() 
{ 
  pinMode(UPPIN, INPUT);     
  pinMode(DNPIN, INPUT);
  
  Serial.begin(9600);
}

void loop()
{ 
 btnStateUp = digitalRead(UPPIN);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;       // Count this press
   lastBtnStateUp = btnStateUp;                     // Use btnPressCount
 
 btnStateDn = digitalRead(DNPIN);
 if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
   btnPressCount--;                                  // Count this press
   lastBtnStateDn = btnStateDn;                      // Use btnPressCount
     
     Serial.print("count = ");
     Serial.println(btnPressCount);  
  //this part mantains the counts equal to 0 
  // so it doesn't make the btnPressCount unsigned
  if(btnPressCount < 0)  // <-- Add me
      btnPressCount = 0; // <-- And me
  
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6
                 

  switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
     
   case 1:
  analogWrite(REDPIN, 255);   // on
  analogWrite(GREENPIN, 0);    // off
  analogWrite(BLUEPIN, 0);
     break;
   
    case 2:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 0 );     
     break; 
     
    case 3:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
   
    case 4:
  analogWrite(REDPIN, 255);   
  analogWrite(GREENPIN, 255);     
  analogWrite(BLUEPIN, 0);
     break;
   
    case 5:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
     
    case 6:
  analogWrite(REDPIN, 255);  
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
    
}
}
/code]

and I see the numbers rolling in the serial monitor.

You'll need to explain WHAT numbers are rolling, and what, if anything, you are doing.

Are all 3 switches wired the same way (except to different Arduino pins)? You are not using internal pullup resistors, so we must presume that you are using external pull-down resistors. True?

You'll need to explain WHAT numbers are rolling

Sorry, I'm assuming that you are looking in the serial monitor too.
Anyways, in the monitor I see the count = 0 rolling down, a lot of times until I press the switch and if I press goes to 1 or 2,3,4,5,6 never ending.
Now, in the on/off sketch I see the counts 1 and 2 every time I press the switch but it stops and the counts don't roll
make sense?

Yes I'm using external pull down resistors and the switches are wired the same way.

Your responses are difficult to read.
Can you please post stuff you're quoting between "quote" tags, and leave your replies in plain.
Code should go inside "code" tags.

Sorry, I'm assuming that you are looking in the serial monitor too

I'm not sure about the others, but I usually rely on the OP to run and describe their code.

In the on/off code, you only print the value when the value changes. In the other code, you print the value on every pass through loop. { and } are your friends. Use them.

Can you please post stuff you're quoting between "quote" tags,

Sorry, I deleted some brackets by accident

Ok, I joined the two of them without thinking much it may look rough but it is compiling.
It looked like it tried to change state but it stayed always on, so I rename the cases because I had two of them with the same description case 0 and case 1, and now at least is turning on and off, but with the same two switches :

 btnOnoffState = digitalRead(ONOFFPIN); 
 btnStateUp = digitalRead(UPPIN);

which they share the same :btnPressCount++; // Count this press
So here is everything :

#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6

#define ONOFFPIN 7

#define UPPIN 4
#define DNPIN 2

int btnPressCount = 0;

int btnOnoffState = LOW;
int lastBtnOnoffState = LOW;

int btnStateUp = LOW; 
int lastBtnStateUP = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;

void setup() 
{ 
  pinMode(ONOFFPIN, INPUT); 
  
  pinMode(UPPIN, INPUT);     
  pinMode(DNPIN, INPUT);
  
  Serial.begin(9600);
}  

void loop()
{ 
  btnOnoffState = digitalRead(ONOFFPIN);
  if((btnOnoffState == HIGH) && (lastBtnOnoffState == LOW)) // Switch is pressed and was not
  {
     btnPressCount++; // Count this press
        Serial.print("count = ");
     Serial.println(btnPressCount);        
  }
  
   btnPressCount = btnPressCount % 2; // Keep count in the range 0 to 4 
  lastBtnOnoffState = btnOnoffState;

  switch(btnPressCount)
  {
    case 0:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 0);
     break; 
     
    case 1:
  analogWrite(REDPIN, 255);  
  analogWrite(GREENPIN, 255);    
  analogWrite(BLUEPIN, 255);
     break;
  }

 btnStateUp = digitalRead(UPPIN);
if((btnStateUp == HIGH) && (lastBtnStateUP == LOW)) // Switch is pressed and was not
   btnPressCount++;                                 // Count this press
  lastBtnStateUP = btnStateUp;                     // Use btnPressCount
 
 btnStateDn = digitalRead(DNPIN);
 if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
   btnPressCount--;                                  // Count this press
   lastBtnStateDn = btnStateDn;                      // Use btnPressCount
     
     
  //this part mantains the counts equal to 0 
  // so it doesn't make the btnPressCount unsigned
  if(btnPressCount < 0)  // <-- Add me
      btnPressCount = 0; // <-- And me
  
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6
         Serial.print("count = ");
     Serial.println(btnPressCount);         

  switch(btnPressCount)
{
   case 2:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
     
   case 3:
  analogWrite(REDPIN, 255);   // on
  analogWrite(GREENPIN, 0);    // off
  analogWrite(BLUEPIN, 0);
     break;
   
    case 4:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 0 );     
     break; 
     
    case 5:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
   
    case 6:
  analogWrite(REDPIN, 255);   
  analogWrite(GREENPIN, 255);     
  analogWrite(BLUEPIN, 0);
     break;
   
    case 7:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
     
    case 8:
  analogWrite(REDPIN, 255);  
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
    
}
}