Question about USB SERIAL read

A1.aia (191.7 KB)

I haven't used the Arduino Mega, so I can't test.

In your code we see that you are using Arduino and a module with NRF24L01. I made a similar assembly but with Arduino UNO and Bluetooth.
https://community.appinventor.mit.edu/t/radio-frequency-modules-arduino-bluetooth-hc-06-fm/24346/10

How are your devices connected?

2 Likes

But that's not a problem, just app sending some data on connection. I know that you can modify the program on arduino, but I prefer the line to be "clean" from the beginning because it can be very useful.

9 ce
10 csn
vcc 5v
mosi 11
miso 12
sck 13
gnd gnd

How is your mobile connected to the Arduino?
(I guess otg cable + usb cable)

How you have connected your Arduino to the PC to see the information of the Serial Monitor. I assume you only have the receiver connected to the PC.

I have 2x Arduino 2xNRF24l01 (one arduino is arduino mega but it does not matter, the connection of the pins is only different) the programs on both arduino are THE SAME. According to your tutorial, I can send data via arduino with nrf and then bluetooth or vice versa using additional TX and RX ports on the arduino MEGA.

Have you tried my code (changing pin number pin in NRF) using OTG instead of Bluetooth?

It will definitely be the second function, but first I would like to deal with the cable connection. I would like to remove these characters when connecting,
Here's something, but I don't know in the end .. my English is too weak and the translator doesn't always translate well. Re: Data Input demo sketch - #12 by Robin2 - Programming Questions - Arduino Forum

Re your link to Arduino Forum:

The ATmega16U2 has a bug in firmware. When you open the COM port without resetting the MCU Atmega328P the ATmega16U2 send a pulse to the Atmega328P RX line (and also to the USB lines but it’s ignored, I think) that is interpreted as data depending on baud rate. If you use 9600 is ignored but starting from about 38400 it appears as character.

I doesn’t appear if you use another chip as serial/USB converter, for example clones that use CH340.

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?