I need a little help with a sketch using Arduino IDE

Is there anyone that could help me with a problem I am having with an ESP32 / ESP-Now project I am working on?

I have stitched together a couple different sketches, and although they both work separately, when I put them in the same sketch it hangs up. I have been beating my head against the wall trying to figure out what is wrong, but still can’t figure it out. I am new to Arduino IDE and coding.

1 Like

The first thing to do with a program that hangs is to add plenty of console debug statements to the code in order to identify exactly which part of the code is executing and the point at which it is stopping.

Then you need to identify what is causing the halt.

A typical procedure for debugging this type of problem would be to make small changes, testing at each step. You could start with one of the sketches that is working, and add the configuration for the extra sketches (in turn), confirming that the original still works as expected. Then add the setups and reconfirm. If the setups are complex, add them in parts. Then identify parts of the remaining code that make sense on their own, and add them. Or, if you get as far as including the complete setups, it might be worth creating new test or demonstration routines for the components used in the additional sketches, and adding them and testing, before replacing them with the actual code. The point at which it stops working gives you the clue to the location of the problem.


Thanks Jeff.
I have created tons of debug statements, and have an idea where the code is hanging up, but I’m not sure how to fix the hang-up or why it is hanging up. If I remove the line that directs it to the other tab in IDE the original code runs fine. The introduction of the notification code causes a hang-up I don’t understand.

Post the code in the vicinity of the point where it hangs.

What was the result when you started adding small segments of the other sketches? How far did you get before it hung?

What do you mean by “the line that directs it to the other tab”? Is this other tab a library?

The other tab is additional code that sends a notification message to CallMeBot. It hangs up the original code after the message is sent. Is it ok to share github link here?

Why is it in another tab? I had understood you were stitching together code from a couple of sketches. It sounds more like you are using libraries.

I don’t think it is another library. I just added it as a tab, as I couldn’t get it to run within the main sketch. I’m a nubie :nerd_face:

I think it is a simple fix, but I don’t have a background in code. So I’m just limping along.
This is my project.

I think that you are actually adding some new routines (in the tabs) to an existing program, rather than trying to stitch a couple of programs together. You don’t have enough debug statements to isolate the problem. I think that the area it is failing is the sequence send_message_start() which ends by calling message_start_to_signal() which ends by calling postData_2(). What is the last debug statement you see in that sequence?

Note that you are opening the Serial device more than once, which is not recommended, although I don’t think it is connected to the problem.

Thanks Jeff
I’ll add some more debug statements in that sequence and see if that points me in the right direction.

It seems to get hung up at line 43 of the main code.

I’m not sure what you mean about opening the serial device more than once. . . :thinking:

I know it’s a mess, I’ve been pasting it together from other examples, etc.
It sort of works, but I have to manually restart the board with a wifi plug to clear the alarm condition.

The serial port is opened in setup(), as normal, and then again at the beginning of send_message_start() and send_message_open().

Do you mean that “void Flash Red LED complete” is the last console message you see? That does not indicate an issue at line 43, but at line 112, so that’s where the debug statements should be. As the previous call to ledcWriteTone() must have worked in order to get to that point, and the only difference between the two calls is the frequency argument, is it possible that using a frequency of 0 causes the hang?

Oh. OK Thanks . . I removed the serial begin from the other two tabs.

I changed the 0 to a 1 but it didn’t like that.
The buzzer was stuck buzzing without a pause, and i got this:

E (19382) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=10000000

I was wrong, it doesn’t get stuck at line 43.

But it doesn’t seem to look for the closed gate again, just keeps cycling through flashing the red led and buzzer.
How do I get it to go back to the void loop to look for the high or low gate signal?

That implies that there is a minimum frequency it can set to, but a frequency of 0 means to turn the timer off. That’s not documented for the method, but seems likely.

I tried 5 as well, but same result.

I just tried changing while to if (Gate Open) but that didn’t help either. Just won’t recognize that the gate/door closed. If I comment out the line: send_message_open(); the board resets when the gate/door closes. . . That’s why I think it must be something in that messageopen tab

Getting late here.
Thanks for all your help.

Are you referring to the variable ‘GateOpen’? If the code is not detecting that the value of that variable is changing, then you need to look at the code that changes it: OnDataRecv(). That’s where the next set of debug statement needs to be. You need to confirm the function is being called, and you need to look at the values in the data structure.

But that part all works fine until I add the send_message_open(): line.
I’ll try some debug statements there tomorrow.

Here is what I get when I run the serial monitor. Don’t know if it helps.

I assume that the problem of looping and not changing when the new message is received has gone away, and the problem is now that it simply hangs after that last debug statement.

That listing reveals a lot, but not perhaps what you expect. Why does the text “void Flash Red LED complete” occur twice, one second apart? There is only one statement that does that print: the second line should not be there. That suggests either that there is a memory fault, or that memory is getting corrupted. If I had to guess (and it’s only a guess) I would say that the send_message_open() call is triggering a response that is getting processed at OnDataRecv() (that takes one second) and something is going stupid at that point. You need debug statements in OnDataRecv() to see what was received and how it updates myData. You might need to write a little routine to print out all of incomingData in hex.

The Red LED flashes hi/lo every 100ms during an alarm state. The void Flash Red LED complete shows the end of that process.

I can’t tell why it doesn’t go back to looking for the OnDataRecv()

Don’t know how to write a routine to print out incomingData in Hex. Guess I will look that up. Should be receiving the data, but doesn’t seem to be doing anything with it … .

Yes, but the number of times it occurs depends on duration (which you should be printing in the debug message) and in any case not with a 1 second delay between messages. Something is happening between that first and second message that is causing a long delay and which does not have any debug messages. Probably the state of GateOpen is changing, and causing the delay loop to pause for longer than the delay specified, and that means that OnDataRecv must have fired, so that’s where you need to be looking. If that’s working OK then I suspect the problem is that second delay function is never returning because of the value of GateOpen when it is called. However that doesn’t explain how that last debug message can print, which is why I suspect corruption. You need many more debug messages in that area, and you need to be printing the values of relevant variables along with each message.

FWIW I never trust compound conditional statements without explicit bracketing. But in any case you need to know the values being used at each occurrence.

1 Like