scrolling colors with two buttons up and down (RGB LED) HELP

I have red, green, and blue LEDs and I want to be able to combine them every time I press the switch. Help, tnx.

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


#define BUTTON 4

int val = 0;        // val will be used to store the state of the input pin

int old_val = 0;    // this variable stores the previous value of "val"

int state = 0; 

unsigned long startTime = 0;  // when did we begin pressing?

void setup() 
{ 
  pinMode(3, OUTPUT);     //tell Arduino LED 3,5,6 
  pinMode(5, OUTPUT);     // are outputs          
  pinMode(6, OUTPUT);               
    
  pinMode(4, INPUT);      // are inputs
  
}

void loop()
{ 
  val = digitalRead(4);  // read input value and store it // yum, fresh
  delay(10);                      
// check if there was a transition 
if ((val == HIGH) && (old_val == LOW))

{
   state = 1- state;
  startTime = millis();
  delay(10);   
}
old_val = val;
if (state == 1)
{ 
  analogWrite(3, 255);   // turns the LED on
  analogWrite(5, 0);    // turns the LED off
  analogWrite(6, 0);
}

val = digitalRead(4);  // read input value and store it // yum, fresh
    
  if ((val == HIGH) && (old_val == LOW))
  
{
  analogWrite(3, 0);   // turns the LED on
  analogWrite(5, 255);    // turns the LED off
  analogWrite(6, 0);
}
val = digitalRead(4);  // read input value and store it // yum, fresh
   
  if ((val == HIGH) && (old_val == LOW))

{
  analogWrite(3, 0);   // turns the LED on
  analogWrite(5, 0);    // turns the LED off
  analogWrite(6, 255);
 }
}/code]

Define "combine"

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


#define BUTTON 4

Such nice names. Why not use them?

If I press the BUTTON REDPIN turns on, if I press again GREENPIN on, if I press again GREENPIN and BLUEPIN turns on, etc.. etc

If I press the BUTTON REDPIN turns on, if I press again GREENPIN on, if I press again GREENPIN and BLUEPIN turns on, etc.. etc

Then you need to count how many times the button was pressed, don't you. The modulo function might then be of interest.

Hey, thank you, but I do not know all the codes maybe if you can help me out a little with that I think I can go from there. I'll want to do something with two buttons to scroll up and down colors, the sketch I have now is jumping from red to green but is not doing blue and is controlled by one button.

Any help is very much appreciated, I'm going crazy frustrated trying to work this out on my own. Thanks. Martin

I'm going crazy frustrated trying to work this out on my own.

You need to approach this differently, then. Forget about the code for a while. Write down exactly what you want to do. Once you have that defined, converting that into code is easy.

What to do

if button pin is pressed once do this if button pin is pressed twice do that

Some code snippets of interest:

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

btnState = digitalRead(btnPin);
if(btnState == HIGH && lastBtnState == LOW) // Switch is pressed and was not
   btnPressCount++; // Count this press

btnPressCount = btnPressCount % 5; // Keep count in the range 0 to 4 

switch(btnPressCount)
{
   case 0:
     // Do this
     break;
   case 1:
     // Do that
     break;
   // Remaining case statements
}

Ok, thanks is getting there, I'll add more cases later. there is one problem now, every time I press the switch all three LEDs light up and then changes state (sometimes) I guess I need a de-bouncing delay or something.

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

#define BUTTON 4

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


void setup() 
{ 
  pinMode(3, OUTPUT);     //tell Arduino LED 3,5,6 
  pinMode(5, OUTPUT);     // are outputs          
  pinMode(6, OUTPUT);               
    
  pinMode(4, INPUT);     
   
}

void loop()
{ 
  
btnState = digitalRead(BUTTON);
if((btnState == HIGH) && (lastBtnState == LOW)) // Switch is pressed and was not
   btnPressCount++; // Count this press

btnPressCount = btnPressCount % 5; // Keep count in the range 0 to 4 

switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 0);
     break; 

   case 1:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 255);    // turns on the LED 
  analogWrite(BLUEPIN, 0);
     break;
   // Remaining case statements
}
}/code]

there is one problem now, every time I press the switch all three LEDs light up and then

Since you never set the blue pin to anything other than 0, this indicates hardware problem.

I guess I need a de-bouncing delay or something.

Possibly. Add delay(10); at the end of loop.

Ok I added the BLUEPIN case and the delay, still no progress. How about millis?

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

#define BUTTON 4


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


void setup() 
{ 
  pinMode(3, OUTPUT);     //tell Arduino LED 3,5,6 
  pinMode(5, OUTPUT);     // are outputs          
  pinMode(6, OUTPUT);               
    
  pinMode(4, INPUT);     
  pinMode(2, INPUT);
}

void loop()
{ 
  
btnState = digitalRead(BUTTON);
if((btnState == HIGH) && (lastBtnState == LOW)) // Switch is pressed and was not
   btnPressCount++; // Count this press

btnPressCount = btnPressCount % 5; // Keep count in the range 0 to 4 

switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 0);
     break; 
    
   case 1:
  analogWrite(REDPIN, 0);   
  analogWrite(GREENPIN, 255);    // turns on the LED 
  analogWrite(BLUEPIN, 0);
     break;
   
    case 2:
  analogWrite(REDPIN, 0);   // turns the LED on
  analogWrite(GREENPIN, 0);   
  analogWrite(BLUEPIN, 255);
     break; 
     
     delay(10);
}
}
/code]

lastBtnState is always LOW, isn't it?

Ok got it, so you meant something like lastBtnState = btnState;
yup, it is working now, I added more cases and how can I add a second button to go up and down cases?

TNX

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

#define BUTTON 4


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(4, INPUT);     
  pinMode(2, INPUT);
}

void loop()
{ 
  
btnState = digitalRead(4);
if((btnState == HIGH) && (lastBtnState == LOW)) // Switch is pressed and was not
   btnPressCount++; // Count this press

btnPressCount = btnPressCount % 7; // Keep count in the range 0 to 6

lastBtnState = btnState;

switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
    
   case 1:
  analogWrite(REDPIN, 255);   
  analogWrite(GREENPIN, 0);    // turns on the LED 
  analogWrite(BLUEPIN, 0);
     break;
   
    case 2:
  analogWrite(REDPIN, 0);  
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 0 );     // on
     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; 
     
  
}
}

Can I add a second int btnState for a second pushbutton and use the same formula to go down ? something like

btnState = digitalRead(2); // another pushbutton

if((btnState == HIGH) && (lastBtnState == LOW)) // Switch is pressed and was not

   btnPressCount -- ; // Count this press going down

/code]

Can I add a second int btnState for a second pushbutton and use the same formula to go down ?

The compiler would have answered this question for you...

You could rename btnState to btnStateUp, rename lastBtnState to lastBtnStateUP, and add btnStateDn and lastBtnStateDn, with code like what you showed.

int btnStateUp = LOW, btnStateDn = LOW;
int lastBtnStateUp = LOW, lastBtnStateDn = LOW;
int bntPressCount = 0;

void loop()
{
   btnStateUp = digitalRead(2);
   if((btnStateUp == HIGH) && (lastBtnStateUp == LOW))
     btnPressCount++;

   btnStateDn = digitalRead(3);
   if((btnStateDn == HIGH) && (lastBtnStateDn == LOW))
     btnPressCount--;

   // Use btnPressCount

   lastBtnStateUp = btnStateUp;
   lastBtnStateDn = btnStateDn;
}

AWOL: Define "combine"

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

define BUTTON 4


Such nice names. Why not use them?

OMG..literally defeat the purpose of define... LOLz...a programmer can refresh his mind in this forum very easily.. :grin:

Ok it its working, is going up over and over again but I can't make it to pass case #0 when going down.

void loop()
{ 
 btnStateUp = digitalRead(4);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
  
   btnPressCount++; // Count this press
   
  btnPressCount = btnPressCount % 7; // Keep count in the range 0 to 6

     // Use btnPressCount
    lastBtnStateUp = btnStateUp;
   lastBtnStateDn = btnStateDn;

  
 btnStateDn = digitalRead(2);

if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
  
   btnPressCount--; // Count this press
   
    btnPressCount = btnPressCount % 7; // Keep count in the range 0 to 6

    // Use btnPressCount
    lastBtnStateUp = btnStateUp;
   lastBtnStateDn = btnStateDn;
  
switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
   /code]

Can you pick an indentation and bracketing style and stick to it please? Your code is hard to read.

Is this better?

void loop()
{ 
 btnStateUp = digitalRead(4);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;                                 // Count this press
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6

    lastBtnStateUp = btnStateUp;                   // Use btnPressCount
   lastBtnStateDn = btnStateDn;

  
 btnStateDn = digitalRead(2);
 if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
   btnPressCount--;                                  // Count this press
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6

   lastBtnStateUp = btnStateUp;                     // Use btnPressCount
   lastBtnStateDn = btnStateDn;
  

switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; 
   /code]

No.

void loop()
{ 
 btnStateUp = digitalRead(4);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;                                 // Count this press
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6

Here, it looks like "btnPressCount = btnPressCount % 7;" is conditional, when it isn't (but could be)

Ok this is how I have it now, and is doing the same thing it does not want to pass case #0 going backwards.

void loop()
{ 
 btnStateUp = digitalRead(4);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;                                 // Count this press
  
 btnStateDn = digitalRead(2);
 if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
   btnPressCount--;                                  // Count this press
  
   btnPressCount = btnPressCount % 7;               // Keep count in the range 0 to 6
   lastBtnStateUp = btnStateUp;                     // Use btnPressCount
   lastBtnStateDn = btnStateDn;
  
switch(btnPressCount)
{
   case 0:
  analogWrite(REDPIN, 255);   // turns the LED on
  analogWrite(GREENPIN, 255);   
  analogWrite(BLUEPIN, 255);
     break; /code]

If you press ctrl-t in the IDE,it will auto-format for you.