Need some help figuring out erratic code behaviour

Hello there,

I'm working on a custom remote controller for a RC PowerWheels project. The remote is functioning as intended for the most part but I'm having trouble with a joystick push button and controlling a section of code with it.

The area where I'm having some trouble with is with the "Internal_Battery_Meas_State" variable that I declared. I've declared it as an int variable and set it to a LOW state. This is tied to a joystick push button which is read with a debounce timer. If the joystick button is pressed, then it pulls the input low and I've written code to then set the Internal_Battery_Meas_State to a HIGH state.

The problem I'm having is that the Internal_Battery_Meas_State is constantly going from a LOW state to a HIGH state and back. I'm not sure how it is being set to a HIGH state though and I'm trying to figure that out.

Using serial print I've confirmed that that if statement used to set it HIGH does not trigger and as such I'm unsure why this is happening. Looking forward to some help on this.

Refrences from where this variable is being called in the code:


Row 61: Declaring "Internal_Battery_Meas_State" as an integer and setting it to LOW
Row 396: Testing whether the If statement is satified and if so, then

Row 398: Setting the Internal_Battery_Meas_State to a HIGH state based on reading the joystick button press

Row 412: Setting the Internal_Battery_Meas_State to a LOW state to prevent potential clashing of variables
Row 423: Setting the Internal_Battery_Meas_State to a LOW state to prevent potential clashing of variables
Row 431: Setting the Internal_Battery_Meas_State to a LOW state to prevent potential clashing of variables

Row 567: Reading whether the Internal_Battery_Meas_State is HIGH, and if so, then execute the code int he if statement.

Row 849: Call a 5 second timer and once the 5 seconds have elapsed, then set the Internal_Battery_Meas_State low again after it was high. This function is called in the if statement when the Internal_Battery_Meas_State is read as high.

Row 913 - 920: Debugging function, reading the Internal_Battery_Meas_State and printing to the serial monitor whether it is high or low.


I've uploaded the code as well as a snapshot of the serial monitoring data. Thank you and looking forward to receiving some help to try and figure out what is happening here.

Serial Printing the Variable changes - twice a second

Custom_Remote_Control_Code.ino (32.5 KB)

You have extra semicolons here

void debugging()
{
    ...
    if (Internal_Battery_Meas_State == HIGH);
    {
      Serial.println("Internal_Battery_Meas_State is HIGH");
    }

    if (Internal_Battery_Meas_State == LOW);
    {
      Serial.println("Internal_Battery_Meas_State is LOW");
    }

There are empty clauses associated with those if() statements and the following Serial.print() block is ALWAYS executed

je ne vois d'erreur lorsque je compile ton code. voir le branchement peut etre?

Sorry. I didn't find any error when I compile the code. Maybe you can show us your wiring diagram. If youre not wired properly it can cause that type of bad behavior.

Hello blh64,

Thank you for noticing that for me. Removing those helped with the debug code but it still didn't solve the problem. It did point me in the right direction though.

The problem was that I also had another extra semicolon on the if statement checking for the Internal_Battery_Meas_State on row 567.

When I removed this, then it resolved the problem. Thanks again for being an extra pair of eyes for me.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.