End void function if x = 1 or something

Well I was playing around with a code for an alarm clock. Now I've got a problem. I want to end the countdown if a certain variable (f in my case) hits the value 2. How do I abort the void function?
I tried

 if (f == 2) {
    return ;
  }

but that doesn't seem to work. Can anyone help me?

Can anyone help me?

With that snippet?
No.

Okay I'll post the most of my code. I didn't want to post it because it has about 600 lines.

#include <Wire.h> 
#include "Adafruit_LEDBackpack.h"  //include important libraries
#include "Adafruit_GFX.h"

Adafruit_7segment matrix = Adafruit_7segment();  
int x = 0;            
int y = 0;
int f = 0;


void Countdown (int y) {
  if (f == 2) {
    return ;
  }
  if (y == 1) {
   matrix.print( 100, DEC);
   matrix.drawColon(true);
   matrix.blinkRate(0);
   matrix.writeDisplay();
   delay (1000);
  uint16_t blinkcounter = 0;
  boolean drawDots = false;
  for (uint16_t counter =   59; counter >     0; counter --) {
    matrix.writeDigitNum(0, (counter / 1000), drawDots);
    matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
    matrix.drawColon(true);
    matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
    matrix.writeDigitNum(4, counter % 10, drawDots);
    matrix.writeDisplay();
    delay(1000); }
  }
  if (y == 2) {
   matrix.print( 200, DEC);
   matrix.drawColon(true);
   matrix.blinkRate(0);
   matrix.writeDisplay();
   delay (1000);
  uint16_t blinkcounter = 0;
  boolean drawDots = false;
  for (uint16_t counter =  159; counter >  100; counter --) {
    matrix.writeDigitNum(0, (counter / 1000), drawDots);
    matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
    matrix.drawColon(true);
    matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
    matrix.writeDigitNum(4, counter % 10, drawDots);
    matrix.writeDisplay();
    delay(1000); }
  for (uint16_t counter =   59; counter >    0; counter --) {
    matrix.writeDigitNum(0, (counter / 1000), drawDots);
    matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
    matrix.drawColon(true);
    matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
    matrix.writeDigitNum(4, counter % 10, drawDots);
    matrix.writeDisplay();
    delay(1000); }
  } 

//Usw.

void ShowTime (int x) { //subroutine to show the mins before start
  if (x == 1) {           //1 min
    matrix.print( 100, DEC);
    matrix.drawColon(true);
    matrix.blinkRate(2);
    matrix.writeDisplay();
    y = 1;
  }
  if (x == 2) {           //2 min
    matrix.print( 200, DEC);
    matrix.drawColon(true);
    matrix.blinkRate(2);
    matrix.writeDisplay();
    y = 2;
  }
  if (x == 3) {           //3 min
    matrix.print( 300, DEC);
    matrix.drawColon(true);
    matrix.blinkRate(2);
    matrix.writeDisplay();
    y = 3;
  }

//usw.

void setup() {
#ifndef __AVR_ATtiny85__ //backpack address
  Serial.begin(9600);
#endif
  matrix.begin(0x70); // 0x70 = l2c address of the 7-segment display
pinMode (10, INPUT); //set-time button
matrix.print(1000, DEC);
matrix.drawColon(true);
matrix.writeDisplay();
}

void loop() {
if (digitalRead (10)==HIGH) {
  x=x+1;
  ShowTime(x);
  delay(1000);
    if (x==11) {
    x=0;
     }
  }
if (f == 0) {
   if (digitalRead (9)==HIGH) {
     Countdown(y);
     f = 1;
  }
if (f == 1) {
  if (digitalRead (9)==HIGH) {
     f = 2;
     Countdown (f);
     ShowTime(42);
     delay(1000);
     f = 0;
 
  
  }
} 
}
}

assuming you mean the loop() function, your code works exactly as expected. It returns from loop() which the main program in the arduino IDE promptly calls again. The main program looks looks more or less like this:

int main()
{
   setup();
   while (1)
   {
       loop();
    }
}

So you see, loop() is continually executed. If you want to stop it dead use:

while (1) ;

instead of return.

Well I just replaced it, but it doesn't work.. I want to end the "void Countdown" not the void loop.

It'll work just as well in the function Countdown as in the function loop.

Is that right then?

void Countdown (int y) {
  if (f == 2) {
    while (1) ;
  }
  if (y == 1) {
   matrix.print( 100, DEC);
   matrix.drawColon(true);
   matrix.blinkRate(0);
   matrix.writeDisplay();
   delay (1000);
  uint16_t blinkcounter = 0;
  boolean drawDots = false;
  for (uint16_t counter =   59; counter >     0; counter --) {
    matrix.writeDigitNum(0, (counter / 1000), drawDots);
    matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
    matrix.drawColon(true);
    matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
    matrix.writeDigitNum(4, counter % 10, drawDots);
    matrix.writeDisplay();
    delay(1000); }
  }

//usw.

Is that right then?

Does it do what you want?
If so, it's probably "right".

That's the problem it doesn't. I've got a countdown running (as the function name says) and I want to stop it if you press the button again. So I tried changing f to 1 when I press it the first time, so it does something else (it should stop the function if you press the button again and it should set f to 0 again, so you can start another countdown). But somehow it doesn't do anything, but I can't start a new countdown. I'm too stupid to find the mistake, that's why I'm asking here. :frowning:

Okay, let's go back to what it is that you want to do.

If it's an alarm clock...

you have a set time, at that time, it does something (buzzer, radio, whatever) and starts a countdown. If you press the button, you want it to stop doing what it's doing, and abort the countdown, and go back to waiting for the next alarm event, is that correct?

your loop() function should update the display if/when minutes change. You could flash the colon at 1 second intervals by using a boolean variable for the visibility of the colon. (Say, make it visible if the current seconds value is an even number.) Whatever, the main portion of this loop just shows the time.

At the alarm set time, you can call a separate routine to do the alarm/countdown timer, but I probably wouldn't. I'd just set a starttime variable, and set a boolean flag for doing the alarm event. set the output pin to do the alarm stuff when you set the starttime variable.

If the alarm event flag is true, check for the button press. If it is, unset the alarm output pin, and unflag the alarm event. (If you have 2 buttons, and one is the snooze, you can make a separate alarm start event now.)

If the alarm event is true, alter the display to show the countdown instead of the time. (Or not. the buzzer is enough, IMO.)

if the countdown time is expired, stop the alarm event, and reset the alarm event flag.

repeat. (end the loop function)

In countdown you have a for loop doing the count down. f or what ever needs to be checked in that for loop so that you can get out of it.

But I think you should read this Demonstration code for several things at the same time - Project Guidance - Arduino Forum and then scrap and re-write your code.

Mark