Question about USB SERIAL read

In that case, how to reset the arduino via the usb port? xD

A reset would not help? You could try something like:

Serial.begin(115200);
Serial.write(""); (Edited)
Serial.flush();

Serial.flush
"Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)"

I don't understand what it would do for me. I would like to find out from the person who created the app inventor if he can or can reset the entire arduino or whether it is possible to remove this MCU Atmega328P reset

It has nothing to do with App Inventor really, but there must be a trick that we can use, hence my suggestion.

Serial.begin(115200);
Serial.write("");
Serial.flush();

I did it, I don't know if it's right, but it's still the same. When connecting (clicking connect) I immediately get "Welcome �����"

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define CE_PIN   9
#define CSN_PIN 10

const byte Address[5] = {'N','e','s','s','y'};

RF24 radio(CE_PIN, CSN_PIN);

String username = "";
String dataInput;
char dataToSend[32];
char dataReceived[32];


void setup() {
    Serial.begin(115200);
    Serial.write("");
    Serial.flush();
    Serial.println("Enter username...");
    
    radio.begin();
    radio.setPALevel(RF24_PA_MAX);
    radio.setDataRate(RF24_1MBPS);
    radio.setRetries(3, 5);
    radio.openWritingPipe(Address);
    radio.openReadingPipe(1, Address);
}


void loop() {
  // set username
  while (username == "") {
    if ( Serial.available() ) {
      username = Serial.readStringUntil('\n');
      Serial.print("Welcome ");
      Serial.println(username);
    }
  }
  // listen for radio data
  radio.startListening();
   if ( radio.available() ) {
    // read data from radio
    radio.read( &dataReceived, sizeof(dataReceived) );
    Serial.println(dataReceived);  
}
    if( Serial.available() ) {
      // stop listening on radio
      radio.stopListening();
     
    // get serial input
    dataInput = "[" + username + "] " + Serial.readStringUntil('\n');
    Serial.println(dataInput);
    dataInput.toCharArray(dataToSend, 32);

    // send data
    radio.write( &dataToSend, sizeof(dataToSend) );  
    }
}

 
  

  

Right - where is the input for 'username' coming from? Your phone via the App?

username = Serial.readStringUntil('\n');

Was the input of the username five characters? What is the actual username that was typed-in? I think nothing was typed, but the read is executed and doesn't stop until '\n' - so memory is read, unrecognised characters, hence the ��

Edit: In other words, the code is reading User Input before the User has typed-in a name.

Concerning your App Inventor project, the Clock Timer is setup to constantly read from the moment the App is opened. It is also reading every 1 millisecond? You have one serial channel for read and write? That means you need an event for read and an event for send to ensure the two streams do not coincide.

From the documentation of the extension, file SerialOTG_1.4.pdf:

“Some adapters requires RTS=1 (and/or DTR=1) to communicate. This is true for Arduino Pro Micro, 32U4 chip implementing CdcAcm.

Dtr is used to reset Arduino.”

Normally you can reset an Arduino if you toggle the DTR signal. I don’t know the exact timing for an Arduino Mega.

Isn't that going to flush the existing Sketch?

No, it resets to the bootloader that waits for a new sketch. After a timeout it restarts the previous loaded sketch. (the same thing happens if you press the reset button, or starts the Arduino serial monitor)

Try the attached App Inventor Project. It isn't necessarily going to fix the sending of text to the terminal on first connection but it should prevent the App freezing from trying to send and receive at the same time.

A1_Test1.aia (193.5 KB)

OK - can we use:

SetDTR

to reset?

"Server error: could not upload project. Please try again later!"

I already have 20 minutes, I guess

edit:

i cant upload to the server, error all the time

I just uploaded to App Inventor without issue.

Make sure App Inventor is the 'King' of the Browser - no other Websites (or other computers) are eating your Broadband.

Only use FireFox -there is a glitch in the latest Chrome v100.0.4896.127 which could cause an issue with your computer locking up @ 100% memory used.

What do you know, Google have updated Chrome to v101.0.4951.54, hopefully fixes their issue.

Still the same, it seems to me that opening the show or initializing is still sending something. Or the arduino doesn't reset completely. When "clicking connect", it asks for permissions each time the program is restarted.


'Allow the application to access the device null'

So modifying the program, how will I learn XD Because I would have to understand how EXACTLY the serial works. Then I would know what to modify. I was thinking about isAlphaNumerical () yet, but I can't program yet XD

The App (Serial OTG extension) isn't sending anything as far as I can tell, but the Sketch seems to run the loop prematurely. That's either a fault of the Sketch code or the chip bug mentioned before.

As Rolf Kardell (rkl099, extension developer) has said, Dtr can be used to reset the Arduino without flushing the existing Sketch. I don't know how to do this via the App, but the extension does have a Dtr function.

Try this Project (note, untested here as I do not have the hardware):
A1_Test2.aia (194.4 KB)

I'll check back soon, thanks so much for your help.