State Machine not working properly...

I need some keen programming eyes to look at this, and please disregard that the program as of now looks a bit messy. I’m trying to merge 2 programs into one.

They both work individually, but when I merge them, something goes haywire, and I can’t see why.

See, when I start up the program, “manual mode” starts up and it works just fine. Then, when I press ‘#’ to enter the “enterDelay” routine, it writes what is in case 1 on the LCD just fine. But it isn’t responding to keypad presses - and that is what I need help with - where is the problem and why?? (setting a variable by keypad worked fine by itself when I tried it earlier in a simpler program). Well, seems like it never continues to case 2…

I am aware that I should change the layout of the program and make it more easy on the eyes - but I haven’t gotten to that yet.

Anyone have any suggestions?

keypaddelay.ino (9.8 KB)

void loop()
{
  char key = keypad.getKey();

Why are you reading the keypad here? Don't you suspect that that just might interfere with the callback routine?

When enterDelay() is called, and no key is pressed, which would be the usual situation, what happens?

Well, if I remove that line from my loop, I enter manual mode upon startup and can’t go anywhere from there… and if no key is pressed in ‘enterDelay’ routine, it should just wait for a key to be pressed. If a key is pressed, go to case2, else, go to case1.

Still lost…

I can't figure what your program is supposed to do or how it is supposed to work.

Can you provide a non-code description of what it is all about ?

I think you are interleaving code that gets values from the keypad with code that displays stuff. That is not making proper use of the speed of the Arduino. I would completely separate the code that gets user data from the code that displays that data. That should greatly simplify both pieces of code.

I wonder is there anything useful in planning and implementing a program ?

...R

Well, if I remove that line from my loop, I enter manual mode upon startup and can't go anywhere from there...

Since you never use the value read from the keypad, that makes no sense.

and if no key is pressed in 'enterDelay' routine, it should just wait for a key to be pressed.

Nonsense. The getKey() method does NOT block, waiting for a key to be pressed.

Paul, nevertheless thats what happens. And if getkey is the wrong thing to do, what would you suggest?

And if getkey is the wrong thing to do, what would you suggest?

It's not the wrong thing to use. It is expecting getKey() to wait for a key press that is wrong.

If the idea is to have the user enter a delay time, and you don't want to do anything until the user has done that, you need one approach. If the idea is to have the user enter a delay time, and you want to do other stuff while the user is entering the value, you need another approach. It is not clear to me what you want.

Thanks for explaining that. I'll try my best to tell you what it's all about. This program is meant to control 3 relays. It is supposed to have a manual mode and an automatic mode. I toggle between those 2 by pressing '#'.

However, now I want it so that when I start in manual mode, and press '#' the user should be prompted to select a running time for the 3 relays in automatic mode. After entering the delay time, user should be prompted to confirm his entry and then automatic mode should start, and each relay in turn should run for the amount of time the user has decided.

At any given moment, user should be able to press '#' again to enter go back to manual mode. Needless to say, manual mode allows the user to turn on/off any of the 3 relays of his choice.

Hope you can make sense of that...?

So far the part where I toggle between auto and manual mode works just fine. I can program the timing in the arduino environment and it works. But implementing the "enter delay time" stuff causes me some problems. If you want, I can add the working program, that doesn't bother with allowing the user to enter a running time?

After entering the delay time, user should be prompted to confirm his entry and then automatic mode should start

So, once the user presses the # to start the time entry process, the Arduino should do nothing except wait for the time to be entered?

Exactly! But preferably still be able to recognize if '#' is pressed again, to go back to manual mode. Dunno if that's possible...

But preferably still be able to recognize if '#' is pressed again, to go back to manual mode. Dunno if that's possible...

Of course it is.

Is there some part of writing blocking code to collect user input, and breaking out of the (while) loop if the # is pressed, that seems challenging? I even included a hint there...

I suppose you want me to create a while loop, to keep me in the time entry routine until finished. Seems like a good idea.. now its just a matter of how to do it with this code. :slight_smile:

Whooops... seems like there's been so much back and forth in this thread, that the main problem got left out. I still have the problem in my code, that the keypad is unresponsive when trying to enter a delay time in the 'enterDelay' routine.

I am this close to just deleting the sketch out of frustration, as I have no idea why... still really hoping for some proper guidance. :slight_smile:

You have serial debug statements in the enterDelay() function. What do they show?

I still have the problem in my code, that the keypad is unresponsive when trying to enter a delay time in the 'enterDelay' routine.

You have been changing your code. You expect us to keep track of stuff we can't see?

aarg> nothing… program compiles just fine and there are no errors as such, and that makes it a bit tricky for me to debug.

Paul> I haven’t added any new code in this thread. Still just the one in the 1st post.

Paul> I haven't added any new code in this thread. Still just the one in the 1st post.

So this:

I suppose you want me to create a while loop, to keep me in the time entry routine until finished. Seems like a good idea.. now its just a matter of how to do it with this code.

was just blowing smoke?

cajodk:
aarg> nothing... program compiles just fine and there are no errors as such, and that makes it a bit tricky for me to debug.

Paul> I haven't added any new code in this thread. Still just the one in the 1st post.

Really? No output at all to serial?

PaulS:
So this:was just blowing smoke?

No it wasn't... but I doubt that keeping the time entry in a while loop won't really make any difference, as the keypad is not even responding in that routine. Nothing happens at all when I press any of the buttons.. I tested with a Serial.println(key); also. Nothing at all...

But if you mean that keeping it in a while loop will solve the problem, would you care to explain why?

aarg:
Really? No output at all to serial?

Can you define that a bit for me? I wrote a reply to Paul describing the problem...