Use Button to switch between Layers

Hello Arduino Friends,

I have been learning Arduino for some weeks now and would like to create my first project. I have mostly everything running but am not able to implement a function I want to have.

What I basically want to achieve is to use a designated "toggle-button" to switch between the button mapping of five other buttons.
That way I can map two numbers to each of the fice buttons.

Without the "toggle-button" pressed the buttons 1-5 will give out the number 0-4 on a display. When the "toggle-button" was pressed (or is held down) the buttons 1-5 will give out the numbers 5-9 on a display.

I tried to frankenstein a code together, unfortunately without success.


I thought it may be easier to explain the concept on a simple setup. What I would like to achieve with this conceptual code is, that button1 will switch between the mapping layers of button2 and button2 will turn on/off the led1 when in mapping layer1 and led2 when in mapping layer2.

int button1Pin=13;
int button2Pin=12;

int led1Pin=3;
int led2Pin=4;

int LEDState=0;
int buttonNew;
int buttonOld=1;
int dt=100;
 
void setup() 
{
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}
 
void loop() 
{
}

This is the hardware layout:

I hope I wrote an comprehensible explenation.
Thank you for your help

Read the state of the mapping pin. If its state changes save the state in a variable
Read the input to be mapped to one of two actions
Test the mapping state previously saved and depending on its value take action 1 or 2

Where are you stuck ?

In programming terms what you are talking about is a simple State Machine. Your system has two states, the one that does 0-4 (or LED1) and the one that does 5-9(or LED2). There's a button to switch between them.

Have a look at a couple of tutorials State Machine and Gammon Forum : Electronics : Microprocessors : State machines and they should get you most of the way there.

Steve

@UKHeliBob
@slipstick

Thank you very much for your help! I will have a look at your solutions this evening and will post my results.

Ok, the solution was simpler than expected ... sometimes you just lose sight of the wood for the trees :slight_smile:

This is my solution for the conceptual LED code:

int button1Pin=13;
int button2Pin=12;

int led1Pin=3;
int led2Pin=4;

bool layerState=LOW;

bool button1new;   
bool button1old=LOW;

bool button2new;
bool button2old=LOW;
 
void setup()
{
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}
 
void loop() {
button1new=digitalRead(button1Pin);
button2new=digitalRead(button2Pin);
if(button1old==HIGH && button1new==LOW){
  layerState=!layerState;
  }
button1old=button1new;
delay (50);

if(button2old==HIGH && button2new==LOW){
  if (layerState==LOW){
    digitalWrite(led1Pin, HIGH);
    delay (300);
    digitalWrite(led1Pin, LOW);
  }
  else{
    digitalWrite(led2Pin, HIGH);
    delay (300);
    digitalWrite(led2Pin, LOW);
}
}
button2old=button2new;
delay (50);
}

I also made a version where you have to keep the "layer-button" pressed to get to the second layer.

int button1Pin=13;
int button2Pin=12;

int led1Pin=3;
int led2Pin=4;

bool layerState=LOW;

bool button1new;   
bool button1old=LOW;

bool button2new;
bool button2old=LOW;
 
void setup()
{
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}
 
void loop() {
button1new=digitalRead(button1Pin);
button2new=digitalRead(button2Pin);
if(button1old==HIGH && button1new==LOW){
  layerState=!layerState;
  }
button1old=button1new;
delay (50);

if(button2old==HIGH && button2new==LOW){
  if (layerState==LOW){
    digitalWrite(led1Pin, HIGH);
    delay (300);
    digitalWrite(led1Pin, LOW);
  }
  else{
    digitalWrite(led2Pin, HIGH);
    delay (300);
    digitalWrite(led2Pin, LOW);
}
}
button2old=button2new;
delay (50);
}

Thanks again for your help!