Serial port not reading unless using IDE Serial Monitor

I have some simple code that reads the data from the serial port and moves a servo based on the read serial port value.

The code works perfectly if I manually open/click the Serial Monitor icon on the IDE in top right corner.
I see the serial port values being read (properly) and the servo does what I want based on the read value.

However, if I do not open the Serial Monitor, the code does not execute properly.
By that , I mean, I believe the Arduino Uno is receiving the data on the serial port (as the instant I send data, I can see the Arduino board flash a quick led blip) - but the if statement running my servo never fires off (indicating the value read from the serial port isn’t read properly …or at all).

Again, tho, the exact same code works if I simply open the IDE Serial Monitor…it doesn’t if I don’t.

[EDIT]: I have updated with all my code, formatted for better readability based on forum guidelines and slightly edited my original post to be more clear.

Additionally, I did some experimenting and found the following:

  1. Upon booting up the RaspPi/Uno (and having already previously uploaded the Sketch from below to Uno, I can open up a command window (in raspbian) and type echo 1 > /dev/ttyACM0. The result of which is certainly captured by the Uno as I instantly see the led flash for a brief moment. However, none of the code in my sketch executes (eg the servo never moves). However, if all I do is open up the Arduino IDE and press the Serial Monitor icon in top right corner (on just a blank sketch even) - I can immediately go back into the command window and retype the echo statement above and the servo will move (as expected) with no code modifications/no sketch recompiling/uploading/etc…just simply opening the Serial Monitor on the IDE will then let my code execute for some reason. I can then even close the Serial Monitor and IDE altogether and Still the echo statement above will work and trigger the servo…indicating to me that somehow something is up with the Serial reading/COM port.

Thanks!

========CODE====

//Control Servo
#include <Servo.h>

// create servo object to control a servo
Servo servo3;

int ServoPos3 = 90;    // variable to store the servo position-0-90-180

void setup() {   // the setup routine runs once when you press reset:
  servo3.attach(3);  // attaches the servo
  servo3.write(ServoPos3);
  Serial.begin(9600);
}

byte rx_byte = 0;    // stores received byte

void loop() {

  if (Serial.available()) {  // check if byte available from USB port
    rx_byte = Serial.read();
    Serial.write(rx_byte);

    if ( rx_byte == '1' ) {
      if ( ServoPos3 == 135 ) { }
      else {
        ServoPos3 += 15;
        servo3.write(ServoPos3);
      }
    }
  } // if Serial.available

}

======CODE====

COM_value = Serial.read(); //read from the serial connection;

That reads 1 byte from the serial buffer, not the whole message.

The serial input basics tutorial may help to understand serial comms.

Read the how to use this forum-please read sticky to see how to post code.

Similar? USB serial port works but doesn't - any ideas? (solved) - Interfacing w/ Software on the Computer - Arduino Forum

@groundFungus : As noted, the code already works if I simply open the IDE Serial Monitor, so don't believe it's related to being 1 byte size ( I'm simply reading one integer and when looking at the IDE Serial Monitor window - I post debug code that shows the number is being read (and outputted via my debug prints) perfectly - except when I'm not using the IDE Serial Monitor, then code won't work) - otherwise code wouldn't work properly at all.
@gbafamily: Not the same, I'm using the Arduino IDE/Serial Monitor, not another tool.

int COM_value = 0;

OK, I did not have any way to know that the message was only 1 byte.

Please post your full sketch.

If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the forum will allow then it’s OK to add it as an attachment. If you click the “Reply” button on this forum thread, you will see an “Attachments and other options” button. Don’t put your code in some external file service like dropbox, etc. We shouldn’t need to go to an external website just to help you.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

Wasted 6+ hrs on this today :frowning:

If there's any devs of the Arduino IDE who read this....what code is behind the Monitor Serial button?
I read it refreshes the code and forces the void startup function to run-but supposedly so does the reset button - but I hit the reset button and my code still won't work....I hit the Monitor Serial button and viola...it magically works.

There's clearly something else going on behind that button (like forcing a connection to the serial/com port ? sending/receiving something on the serial/com port to enable it?)?!?

Instead of wasting 6+ hours, why not just post the code so that we can tell you what's happening?

tj314tj:
The code works perfectly if I manually open/click the Serial Monitor icon on the IDE in top right corner.
I see the serial port values being read (properly) and the servo does what I want based on the read value.

I don't think you have told us what is sending data if not the Serial Monitor. That is probably where the problem lies.

...R

Not the same, I'm using the Arduino IDE/Serial Monitor, not another tool.

"echo 1>/dev/ttyACM0" is another tool. This opens the port, sends "1\n", then CLOSES the port. This may result in the Uno discarding the input before it is processed. Try a terminal program that keeps the port open such minicom or picocom. Both can be installed using apt-get. These programs should be configured to disable Hardware Flow control and enable DTR. If the trailing newline is not wanted, use "echo -n".

@pert: Just look up, it's all in my original post.
@Robin2: I send data from the command line via echo.
@gbafamily: Thanks, I can try those.

Thanks for replies, but remember in my problem ...everything works. It all works. Typing echo 1 > /dev/ttyACMO works and my Uno code works...as long as I click the Serial Monitor button first on the IDE. If I don't, it won't.

I don't use the serial monitor, i don't type into it, i do zero with it - except press the button to show the window.

If anyone has ideas on if there's something else environment related (since I posted all the code and only type in 1 echo command - there's not much there) - maybe it's permission related, ttyACM0 related, env related, something to do with the fact that hitting the SM button is noted as forcing a refresh of the code on the Uno, etc?

I will try gbafamily's idea as it hits upon some configuration/port options that may help!

thanks

What was the solution for this problem? I am facing the same issue.
When I open serial monitor in arduino IDE then only arduino code works. Otherwise I receive the same messages back to RPi.
how to make sure code works without opening serial monitor?

Whenever the Serial Monitor is opened, the Arduino resets.

Your RPI program may need to set RTS and DTR when opening the serial port.

…R