Trying to duplicate functions in one sketch - issues

Hey Team,

Relatively new to arduino. I am a technical analyst but not a software dev. I tend to do things via pre-prepped examples and have enough on my plate that I don't try to heavily extrapolate a skill as a coder from these examples. My goal is to hybridise form pioneers, finish a project and move on.

I am looking for a two button, two rgb LED cycling solution.

Have it working well on one LED and button but can't successfully clone to a second set. I assume I am repeating things that are one call only but can't land it. I've just figured out we can only call serial.begin once so I've put those together, and have just tried setting the arrays both upfront too as clutching at straws, but after having no success cloning block by block I've tried to run each light/button code sequentially.

You should be able to see the original code, and how I've prefixed a copy, but it's still not working. I am sure it's something basic as a result of my burn no learn code sampling, but I'd appreciate some advice as to what's going wrong:

// Pin assignement
#define BWButtonPin 13
#define BWRedPin 10
#define BWGreenPin 11
#define BWBluePin 12


#define MCButtonPin 6
#define MCRedPin 5
#define MCGreenPin 4
#define MCBluePin 3

enum BWfcnMode { 
OFF, 
BWRedCase, 
BWGreenCase, 
BWNBSTATE
}; // OFF = 0 and BWNBSTATE=7
int BWledState1 = LOW,BWledState2 = LOW,BWledState3 = LOW;             // ledState used to set the LED
unsigned long BWbuttonState = 0;
int BWfuncState=0;


enum MCfcnMode { 
MCOFF, 
MCRedCase, 
MCGreenCase, 
MCNBSTATE
}; // OFF = 0 and NBSTATE=7
int MCledState1 = LOW,MCledState2 = LOW,MCledState3 = LOW;             // ledState used to set the LED
unsigned long MCbuttonState = 0;
int MCfuncState=0;

/******************************************************************/

void setup(){
Serial.begin(9600); // initialize serial port
pinMode(BWButtonPin,INPUT_PULLUP);
pinMode(BWRedPin,OUTPUT);
pinMode(BWGreenPin,OUTPUT);
pinMode(BWBluePin,OUTPUT);
pinMode(MCButtonPin,INPUT_PULLUP);
pinMode(MCRedPin,OUTPUT);
pinMode(MCGreenPin,OUTPUT);
pinMode(MCBluePin,OUTPUT);

}
/*******************************************************************/
void loop(){
buttonPressed();
setMode();
}
/******************************************************************
* SUBFUNCTIONS 
\******************************************************************/
void buttonPressed() {
BWbuttonState = pulseIn(BWButtonPin,HIGH,1000000);
if (BWbuttonState > 50){
BWfuncState += 1;
Serial.print("Button state n: ");
Serial.println(BWfuncState);
}
BWfuncState=BWfuncState%BWNBSTATE;
}
void setMode() {
// All Off
digitalWrite(BWRedPin,LOW);
digitalWrite(BWGreenPin,LOW);
digitalWrite(BWBluePin,LOW);

Serial.print("Function : ");
Serial.println(BWfuncState); 
switch(BWfuncState){
case OFF:
break;
case BWRedCase:
digitalWrite(BWRedPin,HIGH);
break;
case BWGreenCase:
digitalWrite(BWGreenPin,HIGH);
break;
// set the LED with the ledState of the variable:
digitalWrite(BWRedPin, BWledState1);
}
}


/*******************************************************************/
void MCloop(){
MCbuttonPressed();
MCsetMode();
}
/******************************************************************
* SUBFUNCTIONS 
\******************************************************************/
void MCbuttonPressed() {
MCbuttonState = pulseIn(MCButtonPin,HIGH,1000000);
if (MCbuttonState > 50){
MCfuncState += 1;
Serial.print("MCButton state n: ");
Serial.println(MCfuncState);
}
MCfuncState=MCfuncState%MCNBSTATE;
}
void MCsetMode() {
// All Off
digitalWrite(MCRedPin,LOW);
digitalWrite(MCGreenPin,LOW);
digitalWrite(MCBluePin,LOW);

Serial.print("Function : ");
Serial.println(MCfuncState); 
switch(MCfuncState){
case MCOFF:
break;
case MCRedCase:
digitalWrite(MCRedPin,HIGH);
break;
case MCGreenCase:
digitalWrite(MCGreenPin,HIGH);
break;
// set the LED with the ledState of the variable:
digitalWrite(MCRedPin, MCledState1);
}
}
BWbuttonState = pulseIn(BWButtonPin,HIGH,1000000);

That's going to be a problem. It will wait up to 1000 seconds (16 2/3 minutes) for the button pin to pulse HIGH. That is NOT a good way to see if the button was pressed since nothing else can happen in those 16+ minutes, including checking the other button.

See the State Change Detection example to see how you would determine that a button had just been pressed.

Wow, ok. That's crazy. I was using this guide FWIW:

Sounds like I might be able to tweak it, but there are some simpler approaches for RGB lights I've seen elsewhere that might be easier to extrapolate on.

eg: https://www.hackster.io/gol73/innovation-lab-5-push-button-rgb-led-color-change-6f4453