porting from basic

Hi all,
I am new to Arduino and am trying to port some old PIC Basic code to Arduino.
I include three working snippets. The first sketch uses goto's and is similar to the original basic version.
The second sketch uses a do..while loop. The third uses a function which is what I want to use.
Am I going in the right direction or can it be done smarter??
Thanks in advance!

/*
  File: wait4Key.ino
  sketch ported from Basic using goto's
  determines button key number
  tested ok
 */

const int BENTER = 11;  // ENTER BUTTON
const int BDOWN = 12;   // DOWN BUTTON
const int BUP = 13;     // UP BUTTON

int key, i;

void setup() {
  pinMode(BENTER, INPUT_PULLUP);
  pinMode(BUP, INPUT_PULLUP);
  pinMode(BDOWN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop(){
  
  wait4Key:                               // Capture button key
  key = 0; 
  if (digitalRead(BUP) == LOW) key = 1;  // Up button pressed
  if (digitalRead(BDOWN) == LOW) key = 2;
  if (digitalRead(BENTER) == LOW) key = 3;
  if (key == 0) goto wait4Key;
  
  debounce:                               // Wait for button release and debounce
  if (digitalRead(BUP) == LOW) i = 0;
  if (digitalRead(BDOWN) == LOW) i = 0;
  if (digitalRead(BENTER) == LOW) i = 0;
  i++;
  delay (10);
  if (i < 10) goto debounce;
  
  Serial.print("key = "); Serial.println(key);
}
/*
  File: wait4key2.ino
  sketch now using do...while instead of goto's
  determines button key number
  tested ok
 */

const int BENTER = 11;  // ENTER BUTTON
const int BDOWN = 12;   // DOWN BUTTON
const int BUP = 13;     // UP BUTTON

int key, i;

void setup() {
  pinMode(BENTER, INPUT_PULLUP);
  pinMode(BUP, INPUT_PULLUP);
  pinMode(BDOWN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop(){
  do {
    key = 0; 
    if (digitalRead(BUP) == LOW) key = 1;  // Up button pressed etc
    if (digitalRead(BDOWN) == LOW) key = 2;
    if (digitalRead(BENTER) == LOW) key = 3;
  } while (key == 0);
  
  do {
    if (digitalRead(BUP) == LOW) i = 0;
    if (digitalRead(BDOWN) == LOW) i = 0;
    if (digitalRead(BENTER) == LOW) i = 0;
    i++;
    delay (10);
  } while (i < 10);		// Wait for button release and debounce   
   
  Serial.print("key = "); Serial.println(key);
}
/*
  File: wait4Key3.ino
  sketch now using a function
  determines button key number and debounce
  tested ok
 */

const int BENTER = 11;  // ENTER BUTTON TO PIN 11
const int BDOWN = 12;   // DOWN BUTTON
const int BUP = 13;     // UP BUTTON

int key;

void setup() {
  pinMode(BENTER, INPUT_PULLUP);
  pinMode(BUP, INPUT_PULLUP);
  pinMode(BDOWN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop(){
    wait4Key();
    Serial.print("key = "); Serial.println(key);
}

int wait4Key() {
  int i;
  do {
    key = 0; 
    if (digitalRead(BUP) == LOW) key = 1;  // Up button pressed
    if (digitalRead(BDOWN) == LOW) key = 2;
    if (digitalRead(BENTER) == LOW) key = 3;
  } while (key == 0);
  
  do {
    if (digitalRead(BUP) == LOW) i = 0;
    if (digitalRead(BDOWN) == LOW) i = 0;
    if (digitalRead(BENTER) == LOW) i = 0;
    i++;
    delay (10);
  } while (i < 10);    // Wait for button release and debounce
   
}

If I understand your code correctly you could push 2 buttons in at the same time?
Why not make a simple function in which you pass the button pin number as a variable and test if it is pressed. If not return false.

/*
alternative key press routine 

*/


bool testKeyPressed(int  key) {
  do {
    if (digitalRead(key) == LOW) {
        //
        // key is pressed so wait for release
        //
        while(1) {
            //
            // wait 100 mSecs 
            //
            delay(100);
            //
            // test if still pressed
            //
            if (digitalRead(key) == HIGH) {
                //
                // key is released so we are done
                //
                break;                
            }
        } 
        //
        // key was pressed
        //
        return true;
    } else {
        //
        // key was not pressed
        //
        return false;
    }
}

And in your program:

/*
  File: wait4Key3.ino
  sketch now using a function
  determines button key number and debounce
  tested ok
 */

const int BENTER = 11;  // ENTER BUTTON TO PIN 11
const int BDOWN = 12;   // DOWN BUTTON
const int BUP = 13;     // UP BUTTON

int key;

void setup() {
  pinMode(BENTER, INPUT_PULLUP);
  pinMode(BUP, INPUT_PULLUP);
  pinMode(BDOWN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop(){
    if (testKeyPressed(BENTER) {
        Serial.println("key = BENTER"); 
    }
}

@Nico

be aware that your code blocks as long as the key is pressed and even if released and pressed again at the right timing it would fail to return.
as most of the time the endless loop is in the blocking delay and only a few uSec it is testing so there is a chance of 1 in 1000 or less to hit the HIGH.

bool testKeyPressed(int  key) {
  do {
    if (digitalRead(key) == LOW) // key is pressed so wait for release
    {   
        while(1) {
            // wait 100 mSecs 
            delay(100);
            // test if still pressed
            if (digitalRead(key) == HIGH) {
                // key is released so we are done
                break;                
            }
        }

alternative code, will not block if key is kept pressed.

bool testKeyPressed(int  key) 
{
  if (digitalRead(key) == LOW) 
  {
    unsigned long starttime = millis();
    while (millis() - starttime < 100) // 100 millisec  debounce time, adjust if needed
    {
        // optional do something while waiting 
    }
    if (digitalRead(key) == LOW) return true // key is still pressed so we have a real press
  }
  return false;
}

Given the fact that a digitalRead() takes X uSec (to be measured) one can return true if the pin is LOW for Y uSec. (bouncing is over)

bool testKeyPressed(int  key) 
{
  if (digitalRead(key) == LOW) 
  {
    unsigned long X = 4;  // to be measured
    unsigned long Y = 250;  // to be defined 
    unsigned long count = 1;
    unsigned long starttime = millis();
    while (millis() - starttime < 100) // max 100 millisec  debounce time, adjust if needed
    {
        if (digitalRead(key) == LOW) count += X;
        else count == 0; 
        if (count > Y) return true;  
    }
  return false;
}

Thanks guy's.
I will study your code and test it.
Hans