Is there a solution that we can run the switch states on opposite direction

I was told to make a switch that can change different states with a LED. Cases are ON, BLINKING, BLINKING FASTER ,OFF, . and is controlled by a button connected to pin 2.

I know that in reality we could prodably have to make another swither that would switch the statements with oppositet direction like ON -> BLINK -> BLINKING FASTER -> OFF -> ON etc
And 2nd swith that can run ON -> BLINKING FASTER -> BLINK -> OFF -> ON etc

Reedit: How should I make this 2nd swith?

This is my finished code:

const int ledPin = 13;
const int buttonPin = 2;

enum possibleStates {on, blink, fastBlink, off};
const int numberOfStates = 4;
volatile possibleStates state = on;

const int interval = 1000;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(buttonPin), changeState, RISING);
  Serial.begin(9600);
}

void loop() {
  static bool ledState = LOW;
  static unsigned long lastLedChange = 0;
  
  switch (state) {
    case on:
      ledState = HIGH;
      break;
    case off:
      ledState = LOW;
      break;
    case blink:
      if (millis() - lastLedChange >= interval) {
        ledState = !ledState;
        lastLedChange = millis();
      }
      break;
    case fastBlink:
      if (millis() - lastLedChange >= interval / 10) {
        ledState = !ledState;
        lastLedChange = millis();
      }
      break;
    default:
      Serial.println("invalid State");
  }
  
  digitalWrite(ledPin, ledState);
}

void changeState() {
  state = (state + 1) % numberOfStates;
}

Do you have a question?
If not, maybe ask a mod to move this to “Exhibition”?

Oh I am sorry! Forgot to write question.

I was wondring how should I do THE 2nd button that switches to opposite direction.

I was wondering how should I do THE 2nd button that switches to opposite direction.

Have a think, take a guess, try it out and come back with another question if it doesn't work.

I tried to add a button --> buttonPin 3 and a interrupt changestates2 rising etc
and void changestate2() state - 1 as shown in my code below.

Reedit: How ever this changes worked... In a strange way ;/

const int ledPin = 13;
const int buttonPin = 2;
const int buttonPin2 = 3;

enum possibleStates {on, blink, fastBlink, off};
const int numberOfStates = 4;
volatile possibleStates state = on;

const int interval = 1000;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  attachInterrupt(digitalPinToInterrupt(buttonPin), changeState, RISING);
  attachInterrupt(digitalPinToInterrupt(buttonPin2), changeState2, RISING);
  Serial.begin(9600);
}

void loop() {
  static bool ledState = LOW;
  static unsigned long lastLedChange = 0;
  switch (state) {
    case on:
      ledState = HIGH;
      break;
    case off:
      ledState = LOW;
      break;
    case blink:
      if (millis() - lastLedChange >= interval) {
        ledState = !ledState;
        lastLedChange = millis();
      }
      break;
    case fastBlink:
      if (millis() - lastLedChange >= interval / 10) {
        ledState = !ledState;
        lastLedChange = millis();
      }
      break;
    default:
      Serial.println("invalid State");
  }
  
  digitalWrite(ledPin, ledState);
}

void changeState(){
  state = (state + 1);
  if(state == numberOfStates){
    state = 0;
  }
}

void changeState2(){
  int currentstate = state;
 state = (currentstate - 1);  
}

And I am also wondring how I should add an 3nd state "Jail"controlled by another 3nd button. When push it, it flashes very fast (should be faster then FAST BLINK) and the other to buttons shouldn't work after pressing the 3nd button.

The first thing you need to address is that interrupts are the wrong tool for detecting button pressed. Have a read of this tutorial and use what you learn to modify your code to work without interrupts

Yeah like digitalRead is the thing I also would use here. But on the question sheet it says that interrupt must be used to do the first button, and it worked fine. So second button I think it should also be made by a interrupt because a new interrupt function is the only way that comes to my head. But the 3nd "Jail" button I have no idea how to solve.

If I change numberOfStates = 5,
add an "Jail" in the enum possible states,
and case jail
it doesn't do the functionI want

What I think I know is that I should not use interrupt on the 3nd, and the question sheet allows it.

esmirehet0728:
Reedit: How should I make this 2nd switch?

const int buttonPin = 2;

attachInterrupt(digitalPinToInterrupt(buttonPin), changeState, RISING);

There is a second External Interrupt on Pin 3.