Issues with OTG cable to Arduino

I have an app that talks to an Arduino over serial/usb/OTG. I used a serial monitor app on the phone running my app to verify that the Arduino code works, however, the Arduino only reacts correctly when getting the message from my app if it is run while the serial monitor app is running in the background and the Arduino hasn't been unplugged since being tested with the serial monitor app. I have tested using the serial built into AI2 as well as the SerialOTG extension and have gotten the same results. I have tried to use the notifier to help me narrow down where the issue is, but from what I can tell, the phone is sending the message just fine. Any help would be appreciated


blocks (2)




otgservo.ino (961 Bytes)

Here's your code, for those of us on their phones:

#include <Servo.h>

Servo myServo;  // Create a Servo object
const int servoPin = 9;  // Pin connected to the servo

void setup() {
    Serial.begin(9600);  // Start serial communication
    myServo.attach(servoPin);  // Attach servo to pin 9
    myServo.write(0);  // Start at 0 degrees

      pinMode(LED_BUILTIN, OUTPUT);

   for (int i = 0; i < 3; i++) {
    digitalWrite(LED_BUILTIN, HIGH); 
    delay(500);                      
    digitalWrite(LED_BUILTIN, LOW);   
    delay(500);                       
  }
}

void loop() {
    if (Serial.available() > 0) {  // Check if data is available
        int receivedNumber = Serial.parseInt();  // Read input as an integer

        if (receivedNumber == 9) {  // If received 9, turn servo
            Serial.println("Servo moved to 90 degrees");
            myServo.write(90);  // Move servo to 90 degrees
            delay(500);
            myServo.write(0);
            
        }
        
            }
}

This part of your problem:

void loop() {
    if (Serial.available() > 0) {  // Check if data is available
        int receivedNumber = Serial.parseInt();  // Read input as an integer

        if (receivedNumber == 9) {  // If received 9, turn servo
            Serial.println("Servo moved to 90 degrees");
            myServo.write(90);  // Move servo to 90 degrees
            delay(500);
            myServo.write(0);
            
        }

Your sketch is expecting a character '9'.

Here is how you try to send that '9':

That blue 9 might be a decimal byte value of 9 from the decimal byte range 0-255, not a character '9' you would get from a text block.

What does the tool tip for that WriteSerial block say about its expectation of that data input?

1 Like

I also question the first test in

Did you mean logical AND instead of '=' for that if/then test?

If both sides of that '=' are false, it would return TRUE.
Is that what you want?

1 Like

That was an error on my part, it was supposed to be an AND instead of =, but even with that fixed, it still get TRUE for that condition. The documentation for the call Serial1. WriteSerial data block says it wants a string/text, however replacing the blue 9 with a pink 9 makes no difference. I also swapped the WriteSerial block and the PrintSerial block, which expect the same inputs and it made no difference from what I can tell. I'm still confused as to why the Arduino responds as expected to my app right after testing with a 3d party serial monitor app, but not when I unplug and re-plug the Arduino in and try again. I'm wondering if there is some setup/permissions that the serial monitor app is setting up that my app isn't

(stumped)

Dear @Calvin_Stu , just a couple of comments:
PrintSerial appends a \n character to the string sent, therefore your Arduino receives a newline as 'string' terminator; while Serial.Write sends the raw data without appending the \n.
So, as @ABG has already said, please take care of what Arduino is looking for: a character or a byte ?
Because the \n character received by Arduino can foolish the search for the equalitity : '9'\n isn't '9' (character) nor 9 (numerical).

As far as the Arduino disconnection-reconnection is concerned (i.e. Arduino power off/on), most probably when the power returns 'on' you have also to restore the communication on App side. For example, when I use a BT communication between apps and Arduino, to avoid the "stuck on disconnected" situation, I use an handshake method. This means that every X seconds Arduino sends an 'alive' character (for example, a '$') to the app, the app looks for that character every 2*X seconds. If the charater is not there, this means that the communication has ceased, therefore the app decides to retry a connection. There is already a topic on that matter and you can find it in the FAQ (credits to @ABG ) here:

image
Maybe it's not the solution to your issue but I believe it's worth to have a look.
Best wishes.