Arduino Uno - code not working when using Serial.begin or Serial commands

I tried to build a remote car with multiple modes, the attached code works only when I comment out the Serial related lines...I tried to search the forum and read delay(), Serial commands interferes with ISRs, but I'm using them outside the ISR and in the loop section...can anyone help pointing out the issue. thank you.

If I comment out the Serial related commands, the menu in OLED and encoder and all other modes except "Remote mode" work. The "Remote mode" uses the serial comms to connect to an android app.

rcCar_Testing_Module_004.ino (13 KB)

If you post your code as described in the how to use this forum sticky more members will be able to see it.

groundFungus:
If you post your code as described in the how to use this forum sticky more members will be able to see it.

The file is 13 kByte, will not fit in a single post.

kprasann1979:
I tried to build a remote car with multiple modes, the attached code works only when I comment out the Serial related lines...

I'm afraid it is all a bit of a dog's breakfast. For example I can see three separate calls to moveForward() where there almost certainly only needs to be one. And in the function checkMotors() most of the code seems to be about displaying stuff.

You need to put Serial.print() statements into your program so you can follow its workings. For example, when does it get to the function modeRemote()

Rather than call moveForward() etc from lots of places you should have a variable that changes from (say) 'F' to 'B' to 'L' to 'R' to 'S' for forward, backward, left, right and stop. And the different parts of your program will set that variable.

Then write a short operateMotor() function (called from loop() ) which checks the value of the variable and causes the motor to do the appropriate thing.

Also, put all the display code into an updateDisplay() function so it does not confuse the rest of the program. A checkMotors() function should not include display code.

By compartmentalising the code it will be much easier to test. And the exact same motor code will work with the exact same logic whether it is being commanded manually or automatically.

...R

Thanks, but the minute I add the Serial.begin statement in the setup/pinSetup section, the code "freezes" that it just shows the menu and encoder is not working....without even getting into any other section and no Serial.print statements are displayed in the serial monitor...

I'm not saying that it will fix the problem but you have a lot of references to encoder0Pos throughout your program.

This is the variable that is updated by the ISR and it is a 2 byte variable (an int) so there is a risk when you read it or write to it with (for example) if (encoder0Pos >= 3) {encoder0Pos = 3;} that the value will be changed by the ISR while you are accessing it.

You should pick up the value once in loop() with interrupts briefly disabled and save it to another variable which you then use in everywhere else that the value is needed - like this

notInterrupts();
   copyOfEncoder0Pos = encoder0Pos;
   encoder0Pos = 0;   // or whatever value is appropriate
interrupts();

That probably also means that you need to redesign the program so you don't need to reset the value of encoder0Pos in several places.

To focus on what, specifically, is causing your problem you should add some Serial.print() statements so you can see exactly how far the program gets before it crashes. For example at different places insert Serial.println("here A"); and Serial.println("here B"); etc

...R

How much memory does the compiler tell you the program has available?