Keypad 4x4 return to loop() only when a button is pressed

Hi. I am trying to get the keypad to break a certain loop in my program. I encountered few problems where I couldn’t get the codes to work properly

  1. I am to enter the inputs which is required for the program to work. I realise that using getKey function will be a non-stop function and will continuously runs the code unless if I have a code that stop it which is
    :
    do {
    key = keypad.getKey();
    } while (key == NO_KEY);

However, I want to know how can I code it in a way if NO_KEY is input, the code returns back to program for it to re run the UserTorque>LoadTorque or LoadTorque<UserTorque and
if key == (’#’)
the program should enter to Reset() function

  1. I realise that once it enters to reset function, it doesn’t return 0 once the program ends. I would prefer it to go back to loop(), but I have no idea how to do it hence I used return 0. But it ends up going back to program() instead.

I appreciate any help and would examine any suggestions given to improve my codes. However I am deeply sorry for not being able to share to codes fully due to some personal reasons. Much appreciated

KEYPAD Problems.txt (3.41 KB)

KEYPAD_Problems.ino (3.41 KB)

I want to know how can I code it in a way if NO_KEY is input, the code returns back to program

void someFunction()
{
  key = keypad.getKey();
  if (key = NO_KEY)
  {
    return;
  }
  //code here to deal with the key input
}

UKHeliBob:

void someFunction()

{
  key = keypad.getKey();
  if (key = NO_KEY)
  {
    return;
  }
  //code here to deal with the key input
}

I tried out this function. Below is the example I used
void control()
{
lcd.clear();
lcd.print ("# to stop");

key = keypad.getKey();
delay (2000);
if (key == NO_KEY)
{
return;
}
if (key == '#')
{
Reset();
}
}

I spammed # and it didn't enter to Reset() but returned to the previous function

Post a complete program rather than just a snippet

Why is that delay there ?
Have you tried printing the value of key to see what it is before testing it ?

UKHeliBob:
Post a complete program rather than just a snippet

Why is that delay there ?
Have you tried printing the value of key to see what it is before testing it ?

Below I attached the codes. My problem would be the function at line 577 transferring to the function at 635

With 697 lines of code it is difficult to fathom what is going on. I suggest that you write a much smaller program that illustrates your problem and post it here

okay I will make the code simple and resend it here. Thanks for your time. Probably take me a few hours since I am new to coding :slight_smile:

My experience is that you will often spot the cause of the problem whilst trying to write a simplified version

UKHeliBob:
My experience is that you will often spot the cause of the problem whilst trying to write a simplified version

I have tried to simplify it and also examined a few more methods. The function Program is supposed to run continuously until # is pressed. My problem is that I still can’t get the keypad to jump from Program to Reset due to the non stopping function of the keypad. If I use the keypad as a function to jump to Reset, the software will execute the codes continuously non-stop unless if a stopping function is placed (which will defeat the purpose of wanting Program to run continuously). So here, I changed the lines at 346, 351, 382 and 387 from key=kpd.getkey into Serial.Read whereby an input from the keyboard of my laptop is taken it enters to the functions I want

Is there any possible way I can use the keypad instead of my keyboard?

I have another idea whereby I could use a push button to replace this action I want to do. However due to time constraints I might have to go for keyboard option and only upgrade into push button if keypad couldn’t execute this function

Also btw, thank you so much for helping me! Appreciate it dearly

Stepper_Motor_Coding_Simplified.ino (8.5 KB)

When I suggested a simplified program I meant seriously simplified, which this isn't. The simplified program simply needs to show the principle of what you are trying to do.

The first thing that I noticed about your simplified program was that the Program() function called the Program() function. Whilst it is not impossible for this to work it is highly unlikely to do so.

I am afraid that I stopped looking at that point.

‘while’ and ‘do’ should never be used by new people.

UKHeliBob:
When I suggested a simplified program I meant seriously simplified, which this isn't. The simplified program simply needs to show the principle of what you are trying to do.

The first thing that I noticed about your simplified program was that the Program() function called the Program() function. Whilst it is not impossible for this to work it is highly unlikely to do so.

I am afraid that I stopped looking at that point.

Welp. Anyways I got the root of the problem soooo I guess that's fine. Was just hoping for a simpler suggestion on using the 4x4 keypad. Its alright if you can't help with that :slight_smile:

larryd:
‘while’ and ‘do’ should never be used by new people.

Well I used to study C++ two years ago although I may be rusty in it. However, the stopping function was searched online based on Arduino Keypad 4x4 due to it being a non-stopping function once its pressed. So I just copied the example a little bit online to make my Keypad work. But other than that, I've been using lots of if