Need some help with common cathode led + button with state changes

I'm building a loop pedal, my pedal has three modes. record, play and overdub. each track has a led above each pedal to indicate what state the track is in red for recording and green for playing. At the moment I'm trying to have a go at changing the colour of the common cathode with one button just to help start me off but I'm a bit stuck, I used an example online but it is written different to my actual program for the loop pedal

my code for my attempt at changing colour of one common cathode led with one button.

/*  
          Set a state of a variable when you press a pushbutton ( the 
          button went from off to on ). 
          
          created 01-12-2009 by kasperkamperman.com
          based on example 'State change detection' by Tom Igoe
        */
        const int buttonPin  = 2;     // the pin that the pushbutton is attached to
        const int RedledPin     = 3;    // the pin that the LED is attached to
        const int GreenledPin     = 4;    // the pin that the LED is attached to
        
        int buttonState      = 0;     // current state of the button
        int lastButtonState  = 0;     // previous state of the button
        int stateNum         = 0;     // remember current led state
        
        void setup() {
          pinMode(buttonPin, INPUT_PULLUP);  // initialize the button pin as a input
          pinMode(GreenledPin, OUTPUT);    // initialize the button pin as a output
          pinMode(RedledPin, OUTPUT);    // initialize the button pin as a output
        }
        
        void loop() {
          // read the pushbutton input pin
          buttonState = digitalRead(buttonPin);
        
          // check if the button is pressed or released
          // by comparing the buttonState to its previous state 
          if (buttonState != lastButtonState) {
            
            // change the state of the led when someone pressed the button
            if (buttonState == 1) { 
              stateNum++; 
              if(stateNum>2) stateNum=0;      
        }
            
            // remember the current state of the button
            lastButtonState = buttonState;
          }
          
          // turns LED on if the ledState=1 or off if the ledState=0
          digitalWrite(RedledPin, stateNum);
          
          // adding a small delay prevents reading the buttonState to fast
          // ( debouncing )
          delay(20);
        }

If I could get any help, I would truly appreciate it. Because I have been at this for hours, I'm not a pro as I don't have much knowledge of coding.

What exactly do you need help with? What's your question?

Pieter

I need the LEDs to change colour depending on what pedals are pressed (what mode the pedal is in record, overdub or play) so I wanted to use an example to help start my program. I really need to start with one led and two buttons. The pedal itself actually has 10 buttons with 4 LEDs because there are 4 tracks and a pixel ring.

What's wrong with the sketch you posted?

If you need multiple buttons, an object-oriented approach is the way to go.
For example:
https://github.com/tttapa/Projects/blob/master/Arduino/LEDs%2C%20Buttons%2C%20etc./PushButton-debounce/PushButton-debounce.ino

It lights up, flashes then goes off. But I don't know how to assign it to a common cathode because the red leg and the green leg are two inputs so I don't know how to write it for one led. I'd like it so I press the button it lights up red then I press it again and it turns green. But like I say I need to define a lot of these for the modes and different pedals. It's quite confusing.

You don't have to connect any LEDs for the example I posted, it uses the internal LED. Try it, and try to understand how it works. Then you can expand it to two LEDs and more buttons.

I think you are using the variable stateNum incorrectly. It should determine whether the LED is red or green. Your code is only writing to the red pin. Try this:

void loop() {
  // read the pushbutton input pin
  buttonState = digitalRead(buttonPin);

  // check if the button is pressed or released
  // by comparing the buttonState to its previous state
  if (buttonState != lastButtonState) {

    // change the state of the led when someone pressed the button
    if (buttonState == 1) {
      stateNum++;
      if (stateNum > 2) stateNum = 0;
    }

    // remember the current state of the button
    lastButtonState = buttonState;
  }

  // state 0 => recording
  if( stateNum = 0 )
  {
    // recording
    digitalWrite(RedledPin, HIGH);
    digitalWrite(GreenledPin, LOW);
    
  } else {

    // playing 
    digitalWrite(RedledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    
  }

  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);
}

Thank you for your reply, I had a go with the program you wrote. But unfortunately, it didn't work. So I expanded on what I had already wrote and changed a lot. I now have the led off then red then green. I don't know if I have wrote the program correctly but it is kind of working the way I need it to. But I need to expand on it for the different buttons where it will get very confusing. Someone on reddit suggested for me to use switch case statements. I've never used these before, but I need a lot of lines for the state of these 10 pedals. So I don't know what way is the best to go about this? if you watch Ed sheerans performance at about 13 seconds in to 55 seconds you will see how I need the leds to light up Video

/*  
  Set a state of a variable when you press a pushbutton ( the 
  button went from off to on ). 
  
  created 01-12-2009 by kasperkamperman.com
  based on example 'State change detection' by Tom Igoe
*/
const int buttonPin  = 2;     // the pin that the pushbutton is attached to
const int RedledPin     = 3;    // the pin that the LED is attached to
const int GreenledPin     = 4;    // the pin that the LED is attached to

int buttonState      = 0;     // current state of the button
int lastButtonState  = 0;     // previous state of the button
int stateNum         = 0;     // remember current led state

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // initialize the button pin as a input
  pinMode(GreenledPin, OUTPUT);    // initialize the button pin as a output
  pinMode(RedledPin, OUTPUT);    // initialize the button pin as a output
}

void loop() {
  // read the pushbutton input pin
  buttonState = digitalRead(buttonPin);

  // check if the button is pressed or released
  // by comparing the buttonState to its previous state
  if (buttonState != lastButtonState) {

    // change the state of the led when someone pressed the button
    if (buttonState == 0) {
      stateNum++;
      if (stateNum > 2) stateNum = 0;
    }

    // remember the current state of the button
    lastButtonState = buttonState;
  }
    // state 0 => idle
  if(stateNum == 0)
  {
    // idle
    digitalWrite(RedledPin, LOW);
    digitalWrite(GreenledPin, LOW);
    
  } 
  // state 1 => recording
  if(stateNum == 1)
  {
    // recording
    digitalWrite(RedledPin, HIGH);
    digitalWrite(GreenledPin, LOW);
    
  } 
 // state 1 => playing
  if(stateNum == 2)
  {
    // playing 
    digitalWrite(RedledPin, LOW);
    digitalWrite(GreenledPin, HIGH);
    
  }
  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);
}

I re wrote my program from scratch with case statements this time. I have got my program to work, but I now want to write more buttons and leds into this. How can I do this please?

                int recordbtn = 2; // Record button as input D2
                const int Redled = 4;    // the pin that the Red LED is attached to
                const int Greenled = 5;    // the pin that the Green LED is attached to
                
                int buttonState      = 0;     // current state of the button
                int lastButtonState  = 0;     // previous state of the button
                int stateNum         = 0;     // remember current state of button
                
                void setup() {
                  pinMode(recordbtn, INPUT_PULLUP);  // initialize the record button pin as a input
                  pinMode(Greenled, OUTPUT);    // initialize the button pin as a output
                  pinMode(Redled, OUTPUT);    // initialize the button pin as a output
        
                  digitalWrite(Greenled,LOW); //set initial state as off
                  digitalWrite(Redled,LOW); //set initial state as off
                }
                
                void loop() {
                  stateNum = digitalRead(recordbtn); // read current state of the record button
                  if (stateNum == 1) {               // check if record button has been pressed
                    delay(50);                        // wait 50ms
                    stateNum = digitalRead(recordbtn); // check if record button has been pressed again
                  if (stateNum == 0) { // if it is 0 considered one press
                    buttonState = lastButtonState + 1;
        }}
                  else{ // if button has not been pressed
                  delay(100); // wait 100ms
                  }
                  switch (buttonState) { //react to button press and state
                    case 1: // if state is 1 record
                    digitalWrite(Redled,HIGH); //red LED on
                    digitalWrite(Greenled,LOW); //green LED off
                    lastButtonState = buttonState;
                    break;
                    case 2: // if state is 2 play
                    digitalWrite(Redled,LOW); //red LED off
                    digitalWrite(Greenled,HIGH); //green LED on
                    lastButtonState = buttonState;
                    break;
                    default:
                    digitalWrite(Redled,LOW); //red LED off
                    digitalWrite(Greenled,LOW); //green LED off
                    lastButtonState = 0;
                    break;
}
                  }
                  }
                }

What you are trying to do is not crystal clear to me. I get the button states that turn LEDs red / green / off, but you say that there are “10 buttons with 4 LEDs”. I think that means that only four of the buttons cycle the LED colors.

At any rate, you might concentrate on converting your code to arrays of data. As always, the place to go first is the example sketches in the IDE, look in File=>Examples=>Control=>Arrays.

You should be able to add buttons without changing code (at least for buttons that do the same action when pressed). For example, here is code to toggle LEDs:

const int NUM_LEDS = 1;

int redPins[] = {3};
int greenPins[] = {4};
;

void setup() {

  Serial.begin(9600);

  // set pin modes
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(redPins[i], OUTPUT);
    pinMode(greenPins[i], OUTPUT);

  }

}

void loop() {

  // for each LED
  for (int i = 0; i < NUM_LEDS; i++)
  {
    // red
    digitalWrite(redPins[i], HIGH);
    digitalWrite(greenPins[i], LOW);

    delay(1000);

    // green
    digitalWrite(redPins[i], LOW);
    digitalWrite(greenPins[i], HIGH);


    delay(1000);

    // off
    digitalWrite(redPins[i], LOW);
    digitalWrite(greenPins[i], LOW);

    delay(1000);

  }

}

Now to add two leds on different pins all I have to change is the arrays, all the code stays the same.

const int NUM_LEDS = 3;

int redPins[] = {3,5,7};
int greenPins[] = {4,6,8};

This is not meant to be usable code for your project, just trying to give you an idea how to structure your data and write code that will be easier to maintain. My advice is to get one button working the way you want then add the others through arrays.

The buttons don't do the same thing. It's very confussing and hard to explain. I've tried to write out to myself all the outcomes of the pedal. It all comes down to what mode the pedal is in. Record play or overdub and what pedals have been pressed. So I need to write for each pedal. There are 4 track buttons yes they can control the 4 LEDs if the track# pedal is pressed but there is also a mode and a record button that can change them. There are a couple of videos online that might help you understand what I want out of this. This is why I'm asking what is the best way to go about this. Because I'm not sure but I really appreciate the help you have given me. Managed to learn a bit, but I'll have to have a look into arrays because I haven't used them before.