combining two sketches help

I have two separate sketches one is to turn on an off the led's and the other one is to change between cases, both are working but i'm having problems to put them together.
The only thing that the program is doing is turning on the LED's I think i'm having problems whit the curly brackets.

#define REDPIN 3    // Red LED connected to pin # 3
#define GREENPIN 5 //Green LED connected to pin # 5
#define BLUEPIN 6  // Blue LED connected to pin # 6

// Pushbuttons must be momentary tactile pushbuttons.
// 5 volt from the Arduino goes connected to one side of the momentary switch
// and the other side goes connected to the numbers from the Arduino and 
//to ground also with a 10k pullup resistor.
#define BUTTON 4  brightness++  and state up    // This is going to do two funtions, but I'm worried  
                                                // changing state for now
#define BUTTON 2  brightness--  and state down  // This also is going to do two functions

#define BUTTON 7 on/of    // this momentary pusbutton is going to turn the LEDs on and off

// and the Arduino is going to have a separate kill switch.

// this integers are for the on/off sketch
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;      // 0 = LED off and 1 = LED on

//this integers are for the switching up and down cases
int btnPressCount = 0;
int btnStateUp = LOW; 
int lastBtnStateUp = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;

void setup(){  
  pinMode(REDPIN, OUTPUT);     //tell Arduino LED 
  pinMode(GREENPIN, OUTPUT);     // are outputs          
  pinMode(BLUEPIN, OUTPUT);               
    
  pinMode(7, INPUT);      // and BOTTOMs 7,4,2  
  pinMode(4, INPUT);      // are inputs
  pinMode(2, INPUT); 
}
void loop(){             // this part of the sketch turns the LEDs on
  val = digitalRead(7);  // read input value and store it // yum, fresh
 
 delay(10);           //de-bouncing delay, (my idea), it works better 
                      //with this than the second de-bouncing delay alone
      
if ((val == HIGH) && (old_val == LOW))    // check if there was a transition 

 {
  state = 1 - state;    // change the stae from off to on or vice-versa
    
 delay(10);       //second de-bouncing delay     
 old_val = val;         // val is now old, let's store it
  if (state == 1) 
 
  //here starts the changing cases sketch 
{ 
 btnStateUp = digitalRead(4);
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
   btnPressCount++;                                 // Count this press
   lastBtnStateUp = btnStateUp;                     // Use btnPressCount
 
 btnStateDn = digitalRead(2);
 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; 
    }
    
 }
 
else 
{ 
  analogWrite(REDPIN, 0);  //this part of the program makes the LEDs
  analogWrite(GREENPIN, 0);  //SHUT OFF when pressing the BUTTON 7
  analogWrite(BLUEPIN ,0);  
}
}
}
/code]
#define BUTTON 4  brightness++  and state up    // This is going to do two funtions, but I'm worried  
                                                // changing state for now
#define BUTTON 2  brightness--  and state down  // This also is going to do two functions

#define BUTTON 7 on/of    // this momentary pusbutton is going to turn the LEDs on and off

The #define statement has two parts - a name and a value. All occurrences of the name (BUTTON) will be replaced with the value (4 brightness++ and state up).

Do the values you are specifying make any sense?

The IDE has this great tool for formatting your code - Tools + Auto Format... Give it a try. You will see where the braces do not match.

I see.
but I change it to this

#define BUTTON 4                              // brightness++  and state up. This is going to do two funtions, but I'm worried  
// changing state for now
#define BUTTON 2                              // brightness--  and state down. This also is going to do two functions

#define BUTTON 7                              // on/of this momentary pusbutton is going to turn the LEDs on and off
/code]

I did the format and it is compiling like before but I do not see the errors

I did the format and it is compiling like before but I do not see the errors

And the nicely formatted code is?

#define REDPIN 3    // Red LED connected to pin # 3
#define GREENPIN 5 //Green LED connected to pin # 5
#define BLUEPIN 6  // Blue LED connected to pin # 6

// Pushbuttons must be momentary tactile pushbuttons.
// 5 volt from the Arduino goes connected to one side of the momentary switch
// and the other side goes connected to the numbers from the Arduino and 
//to ground also with a 10k pullup resistor.
#define BUTTON 4                              // brightness++  and state up. This is going to do two funtions, but I'm worried  
// changing state for now
#define BUTTON 2                              // brightness--  and state down. This also is going to do two functions

#define BUTTON 7                              // on/of this momentary pusbutton is going to turn the LEDs on and off

// and the Arduino is going to have a separate kill switch.

// this integers are for the on/off sketch
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;      // 0 = LED off and 1 = LED on

//this integers are for the switching up and down cases
int btnPressCount = 0;
int btnStateUp = LOW; 
int lastBtnStateUp = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;

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

  pinMode(7, INPUT);      // and BOTTOMs 7,4,2  
  pinMode(4, INPUT);      // are inputs
  pinMode(2, INPUT); 
}
void loop(){             // this part of the sketch turns the LEDs on
  val = digitalRead(7);  // read input value and store it // yum, fresh

  delay(10);           //de-bouncing delay, (my idea), it works better 
  //with this than the second de-bouncing delay alone

  if ((val == HIGH) && (old_val == LOW))    // check if there was a transition 

  {
    state = 1 - state;    // change the stae from off to on or vice-versa

    delay(10);       //second de-bouncing delay     
    old_val = val;         // val is now old, let's store it
    if (state == 1) 

      //here starts the changing cases sketch 
    { 
      btnStateUp = digitalRead(4);
      if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // Switch is pressed and was not
        btnPressCount++;                                 // Count this press
      lastBtnStateUp = btnStateUp;                     // Use btnPressCount

      btnStateDn = digitalRead(2);
      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; 
      }

    }

    else 
    { 
      analogWrite(REDPIN, 0);  //this part of the program makes the LEDs
      analogWrite(GREENPIN, 0);  //SHUT OFF when pressing the BUTTON 7
      analogWrite(BLUEPIN ,0);  
    }
  }
}
/code]
#define BUTTON 4                              // brightness++  and state up. This is going to do two funtions, but I'm worried  
// changing state for now
#define BUTTON 2                              // brightness--  and state down. This also is going to do two functions

#define BUTTON 7

Couldn't you decide which one you wanted to be called BUTTON?

#define BUTTON 4
#define BUTTON 2
#define BUTTON 7

So, if you were to use BUTTON somewhere in your code, which of these values should BUTTON take on?

What is connected to pin 7? Do you have an external pull-down resistor with that switch? How are they wired?

Serial.begin() in setup() and Serial.print() in loop are some useful tools to learn to use.

I'm using only numbers 2,4,7 in the sketch, that's is the only way I know to make it work, if I use like this: digitalRead 2, it works.
I have three separate pushbuttons with obviously three resistors. Trust me I have these two separate sketches and they work.

if I use the word BUTTON2, BUTTON4 or BUTTON 7 It wont work

I can see why "BUTTON 7" might not work, but no reason why BUTTON2 and BUTTON4 should not.

I think I have everything right when I press button 7 goes up to case 0, it turns on but won't change to the next case or turn off.

Serial.begin() in setup() and Serial.print() in loop are some useful tools to learn to use.

I hate to repeat myself, but add some debug messages to your code.

I think I have everything right when I press button 7 goes up to case 0, it turns on but won't change to the next case or turn off.

I can understand that you are frustrated, and I can accept that English may not be your native language. I can not figure out what this is supposed to mean, though.

The switches attached to pins 2 and 4 control the up and down count. We still have no idea what the switch attached to pin 7 is doing. By the way, switches are attached to the Arduino pins. Buttons are attached to shirts.

So, pressing the switch attached to pin 7 has no affect on the up/down counter/switches, except that they don't do anything unless the switch attached to pin 7 is pressed.

Giving the pins meaningful names would really help.

#define UPPIN 4
#define DNPIN 2

      btnStateUp = digitalRead(UPPIN);
      if((btnStateUp == HIGH) && (lastBtnStateUp == LOW))
        btnPressCount++;
      lastBtnStateUp = btnStateUp;

Here, everything to do with incrementing the value has UP or Up in it's name, and it's easy to see what is happening, expecially is the switch attached to pin 4 has a label next to it that says "Up".

  val = digitalRead(7);  // read input value and store it // yum, fresh

  delay(10);           //de-bouncing delay, (my idea), it works better 
  //with this than the second de-bouncing delay alone

  if ((val == HIGH) && (old_val == LOW))    // check if there was a transition

Here, instead, we have a mysterious name for the switch, 7, and clueless names for the switch state, val and old_val.

These names tell us nothing.

So, my recommendations are to give the pin numbers meaningful names, give the switch state variables meaningful names, and add some Serial.print() statements.

Once you know what the program is doing, it is much easier to see what is wrong. If pressing the up switch does not cause btnPressCount to increment correctly, you can then isolate the software/hardware aspects of the program. Ditto if the down button does not decrement correctly.

If the buttons increment and decrement when they are supposed to, but the constraint of the press count to 0 to 7 doesn't work, we can look at that code.

If the press count all gets managed correctly, but the wrong action occurs for a specific value, we can address that.

But, it is much easier to find a problem in 10 lines of code than in 1000 lines of code. So, help us out. Narrow down exactly what is failing to happen correctly, and let us know what is not happening correctly.

Some other things that might help here: post the original two sketches that work and describe what they do, then describe what you are trying to achieve with the combined version.

Reading between the lines, I think the spec is something like:
You have three coloured leds or an RGB led.
You want to use pins 2 and 4 to adjust which of the leds are in use
the leds in use will flash on and off
pin 7 performs some function I can't fathom.
Anywhere close?

Ok, buttons to shirts and switches to Arduino :slight_smile:
So I want BUTTON7 to act like a kill switch or better yet on/off switch using the IDE. I do not want to install an on/off switch to turn the Arduino on an off, that would be too easy.
So whatever the program is doing I want BUTTON7 to shut it off any time and be able to turn it back on.
By the way I'm calling it ONOFFPIN instead.

The first part of the sketch is to turn on the LEDs, and it does

void loop(){             // this part of the sketch turns the LEDs on
  val = digitalRead(ONOFFPIN);  // read input value and store it // yum, fresh

  delay(10);           //de-bouncing delay, (my idea), it works better 
  //with this than the second de-bouncing delay alone

  if ((val == HIGH) && (old_val == LOW))    // check if there was a transition 

  {
    state = 1 - state;    // change the stae from off to on or vice-versa

    delay(10);       //second de-bouncing delay     
    old_val = val;         // val is now old, let's store it
    if (state == 1) 
/code]

these are the other changes I made thanks for the tip

 pinMode(UPPIN, INPUT);     
  pinMode(DNPIN, INPUT); 
  pinMode(ONOFFPIN, INPUT);     /code]

The next part is from the second sketch, that would be the cases

 { 
      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; /code]

Still missing the Serial.print() statements that tell you that:
Yes, the on/off switch was turned on.
Yes, the up switch was pressed.
Yes, the down switch was pressed.
Yes, the switch press count was incremented/decremented correctly.

Before we go on, though, what kind of switch are you using for the on/off switch? Momentary contact or toggle? The code assumes a toggle switch, but I suspect that you are using a momentary contact switch.

I'm using a momentary switch, I got the example from the book Getting started with Arduino, the sketches are also online.
I have never used serial print().
Do you want me to post the on/off sketch by itself and go from there step by step?
and later on add the cases sketch to the other one.
Let me know
Tnx

Your momentary switch only reads HIGH while you hold it down. The rest of your code only executes when the momentary switch is pressed. So, that explains why your sketch doesn't appear to work.

Try holding the switch down while pressing the up/down buttons, and see if the rest of the code works.

If it does (and it certainly looks like it will), then you can use the same logic as with the up/down buttons. Press once to enable (0 increments to 1) and again to disable (1 increments to 2, which causes a reset to 0).