rcvolley:
So I'm trying to delay the bridge
Why would you want to do that? In general, using delay() is a bad thing. And the way you have it here, it seems to be even worse.
rcvolley:
So I managed to get it to work a few times but it stops working after only a couple of changes
What do you mean by "a couple changes?" Code changes? Or making a few client connections to change the duty cycle value? If the latter, I'm sure it's because of the recursive nature of your client process calls.
As mentioned in one of your other threads, accepting a client connection and calling the process() function while you are already in the process() function is a really bad idea. When you get the first connection, loop() calls process(), which calls analogCommand(), which then stays there while you are in your loop making delay() calls and checking for another connection. When a connection comes in, you are still inside all of these functions and you call process() again, which calls analogCommand() again, and stays in that function in the while loop. This continues each time another connection is made: each connection adds another set of calls to these functions. So every time you make another connection, it uses up a good chunk of stack space, and it won't take too many iterations to overflow the stack and crash the application.
After just three client updates, your stack will have:
- loop(), called from main()
- process(), called from loop()
- analogCommand(), called from process()
- process(), called from analogCommand()
- analogCommand(), called from process()
- process(), called from analogCommand()
- analogCommand(), called from process()
After just three client connections, you will have 7 function call contexts on the stack. Each context will have the function return information, plus a copy of any local variables for each call - process() has a String variable, while analogCommand() will have three unsigned longs (starttime, currenttime, and elapsedtime), two integers (pin, value), and two floats (p, and DC.) That's a lot of data for each context, and you will have multiple copies of each context on the stack. Each time you make a client connection, you will add two more function call contexts and another copy of all of that local data. You are guaranteed to quickly run out of memory and crash the sketch.
I think you need to take a step back and reconsider the logic and layout of your sketch.
A general rule of thumb is that you should design things so that it doesn't spend a significant amount of time in the loop() function. When loop() is called, it should do what it needs to do, and then get out. A general rule of thumb is that you shouldn't have any lengthy() loops or delay() calls in you loop() function or in any function called from loop(). Also, you shouldn't have recursive calls where a function calls itself, either directly or indirectly, unless you are experienced and you really know what you are doing. Used with care, recursion can get you out of a difficult situation in some cases, but in general it can cause lots of problems.
Your process() function, and the helper functions it calls, like analogCommand(), should parse the incoming data, set up some variables to keep track of the requested operation, and then get out. If it's a trivial operation, like setting the state of an output pin, it can be handled directly in the helper function. But if it's a more lengthy operation like your while loop with the delay() calls, it should not be done in the helper function. Instead, the function should set a global variable with the requested duty cycle, and then return. A separate function called from the loop() function should then use that duty cycle value to control the output, preferably without the use of the delay() function, but instead using the concepts from the BlinkWithoutDelay tutorial. If you decide you simply must use delay(), then only do one iteration of the loop - the loop() function is called repeatedly from a while loop that you can't see. Rather than add your own while loop, you should make use of the fact that the loop() is already called over and over again, and you should do at most one iteration of you loop per loop() call. (It would be better if you used the BlinkWithoutDelay model and do even less per loop() call.)
Long story short: if you keep trying to process another client connection from inside an existing client connection, I don't think you will ever be able to get this sketch working properly. This is especially true if that is being done inside a while loop with the delay() calls in it. You really do need to redesign this sketch from scratch, using the advice in the previous paragraph.