How to interrupt custom loops?

I'm trying to make a program that turns my 10 LEDs on and off in a pattern. Here are some pictures of my project:

The program should switch to Blink mode 1 when I press button 1, to 2nd mode when I press the 2nd button and to the 3rd mode when I press the 3rd button. I also programmed it to write to the serial monitor which mode is activated. Only there is always "Mode 1" and "Mode 2", and I have no idea why. Here is the code (it's not finished !!!):

int mode = 1;
int button1state = 0;
int button2state = 0;
int button3state = 0;

const int button1Pin = 0;
const int button2Pin = 1;
const int button3Pin = 2;

int speed = 250;

//Mode 1
int x = 3;
int y = 2;
int z = 0;

//Mode 2

//Mode 3

void setup() {
  pinMode(3 , OUTPUT);
  pinMode(4 , OUTPUT);
  pinMode(5 , OUTPUT);
  pinMode(6 , OUTPUT);
  pinMode(7 , OUTPUT);
  pinMode(8 , OUTPUT);
  pinMode(9 , OUTPUT);
  pinMode(10 , OUTPUT);
  pinMode(11 , OUTPUT);
  pinMode(12 , OUTPUT);
  pinMode(button1Pin , INPUT);
  pinMode(button2Pin , INPUT);
  pinMode(button3Pin , INPUT);
  Serial.begin(9600);
}

void loop() {

  button1state = digitalRead(button1Pin);
  button2state = digitalRead(button2Pin);
  button3state = digitalRead(button3Pin);

  if (button1state == HIGH) {
    mode1();
  } else {
    loop();
  }
  //if (button2state == HIGH) {
  mode2();
} else {
  loop();
}
if (button3state == HIGH) {
  mode3();
} else {
  loop();
}
}

void mode1() {
  digitalWrite(x, HIGH);
  digitalWrite(y, LOW);
  x = y + 2;
  y = x - 1;
  delay(speed);
  if (x > 12) {
    x = 3;
    y = 2;
    z = 0;
    digitalWrite(12, LOW);
  }
  Serial.println("Mode 1");
}

void mode2() {
  Serial.println("Mode 2");
  delay(speed);
}

void mode3() {
  Serial.println("Mode 3");
  delay(speed);
}

I think to fix it it should interrupt "void Mode1" when the second button is pressed...But why is there no "Mode 3"?!?

} else {[i][/i]
    loop();[i][/i]
  }

do no call loop() from within loop. In fact, structure your program in a way that you should never call loop ever. Your code should loop inside loop and never leave.

byte mode = 1;
byte button1state;
byte button2state;
byte button3state;

const byte Button1Pin = 0;// bad idea to use pin 0 and 1 and use them for serial
const byte Button2Pin = 1;
const byte Button3Pin = 2;

int speed = 250;

//Mode 1
int x = 3;
int y = 2;
int z = 0;

//Mode 2

//Mode 3

void setup() {
  pinMode(3 , OUTPUT);
  pinMode(4 , OUTPUT);
  pinMode(5 , OUTPUT);
  pinMode(6 , OUTPUT);
  pinMode(7 , OUTPUT);
  pinMode(8 , OUTPUT);
  pinMode(9 , OUTPUT);
  pinMode(10 , OUTPUT);
  pinMode(11 , OUTPUT);
  pinMode(12 , OUTPUT);
  pinMode(Button1Pin , INPUT);
  pinMode(Button2Pin , INPUT);
  pinMode(Button3Pin , INPUT);
  Serial.begin(9600);
}

void loop() {
  button1state = digitalRead(Button1Pin);
  button2state = digitalRead(Button2Pin);
  button3state = digitalRead(Button3Pin);
  if (button1state == HIGH && button2state == LOW && button3state == LOW) mode = 1;
  else if (button1state == LOW && button2state == HIGH && button3state == LOW) mode = 2;
  else if (button1state == LOW && button2state == LOW && button3state == HIGH) mode = 3;
  if (mode == 1) mode1();
  else if (mode == 2) mode2();
  else if (mode == 3) mode3();
}
...

change int to byte if range needs to be 0-255 (for pins and modes).
use UpperCamelCase for constants and lowerCamelCase for variables (just a style suggestion).
You read 3 buttons, so you should evaluate all 3 buttons.
Using "else if" means the program will skip the else conditional checks if on of the prior conditions is true.

Serial uses pins 0 and 1 so you might want to use other pins for your buttons.

Perehama:

byte mode = 1;

byte button1state;
byte button2state;
byte button3state;

const byte Button1Pin = 0;// bad idea to use pin 0 and 1 and use them for serial
const byte Button2Pin = 1;
const byte Button3Pin = 2;

int speed = 250;

//Mode 1
int x = 3;
int y = 2;
int z = 0;

//Mode 2

//Mode 3

void setup() {
  pinMode(3 , OUTPUT);
  pinMode(4 , OUTPUT);
  pinMode(5 , OUTPUT);
  pinMode(6 , OUTPUT);
  pinMode(7 , OUTPUT);
  pinMode(8 , OUTPUT);
  pinMode(9 , OUTPUT);
  pinMode(10 , OUTPUT);
  pinMode(11 , OUTPUT);
  pinMode(12 , OUTPUT);
  pinMode(Button1Pin , INPUT);
  pinMode(Button2Pin , INPUT);
  pinMode(Button3Pin , INPUT);
  Serial.begin(9600);
}

void loop() {
  button1state = digitalRead(Button1Pin);
  button2state = digitalRead(Button2Pin);
  button3state = digitalRead(Button3Pin);
  if (button1state == HIGH && button2state == LOW && button3state == LOW) mode = 1;
  else if (button1state == LOW && button2state == HIGH && button3state == LOW) mode = 2;
  else if (button1state == LOW && button2state == LOW && button3state == HIGH) mode = 3;
  if (mode == 1) mode1();
  else if (mode == 2) mode2();
  else if (mode == 3) mode3();
}
...



change int to byte if range needs to be 0-255 (for pins and modes).
use UpperCamelCase for constants and lowerCamelCase for variables (just a style suggestion).
You read 3 buttons, so you should evaluate all 3 buttons.
Using "else if" means the program will skip the else conditional checks if on of the prior conditions is true.

Thanks for your help!

One thing to add about buttons, they need to be pulled up or down with resistors to work properly. Otherwise you have 'floating' pins which will basically just get random results all the time. I can't tell in the pic (on my phone) if you added your own or not, so if you did disregard this. If you don't have pullup or pulldown resistors setup in your circuit it's an easy fix since the arduino has built in pullup resistors for the pins.

Quick explanation of pullup resistors:
-The circuit will be pulled HIGH constantly.
-You wire one side of the button to the Arduino input pin you're using, and wire the other side of the button to ground.
-When the button is pressed the circuit will go LOW. (ie: if you digitalRead() the pin it will return LOW when the button pressed, HIGH when the button is not pressed).

It's a quick fix in the code:

#define IS_PRESSED LOW //I would add these defines so your code makes more sense to read
#define IS_NOT_PRESSED HIGH

//Setup all your button pins like this, with INPUT_PULLUP instead of just INPUT. This will
//use the built in pullup resistors in the Arduino
pinMode(button1Pin , INPUT_PULLUP);

//Wherever you digitalRead your buttons change it to this
if (digitalRead(Button1Pin) == IS_PRESSED)
{
  //do stuff for a pressed button
} else {
  //do stuff for a button that isn't pressed
}

Barbeerian:
It's a quick fix in the code:

Barbeerian makes a good point about the need for pull-up resistors. However, his code doesn't address the need to debounce the switches. In the basic code with just a digitalRead(), the switch may read HIGH and LOW several times over the course of a few milliseconds until the button stabilizes into the pressed or unpressed state. It does not affect this code now because there is no state change if all buttons return LOW, but if there was a mode 0 when no buttons are pressed, the code would bounce through this state several times for a few milliseconds. If that is not desirable, the following code can be added for each button and called as a conditional check because it returns a true or false.

// This is your pushbutton function. For each pushbutton, you must copy and paste this function, renaming two things noted to be unique
bool button() {// rename the function to be unique from other pushbuttons
  static int lastButtonState;
  static unsigned long buttonDebounceTimestamp;
  int buttonState = digitalRead(ButtonPin);// change "ButtonPin" the pin that is your button
  if (buttonState != lastButtonState && buttonState == LOW) {// transition is HIGH to LOW
    if (millis() - buttonDebounceTimestamp >= 250UL) {// 250UL = 250ms = time for debounce
      lastButtonState = buttonState;
      buttonDebounceTimestamp = millis();
      return true;
    }
    else {
      lastButtonState = buttonState;
      return false;
    }
  }
  else {
    lastButtonState = buttonState;
    return false;
  }
}