I am attempting to port code from the Arduino mini over to the Pro Micro (5V 16MHz) and having trouble with serial communications.
Substituting serial1.print (for Arduino serial.print) works fine. However, serial input to the device does not.
Example:
void serialEvent()
{
while (Serial1.available())
{
// get the new byte:
char inChar = (char)Serial1.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == ‘\n’)
{
stringComplete = true;
}
}
}
This function compiles OK, but does not work. inputString remains empty, and stringComplete remains false.
I know the serial port hardware is different with the ATMEG32U4 chip on the ProMicro so there might be something in SETUP() that I do not know to include (beyond Serial1.begin(9600)
I have verified that the serial data is getting to the Rx pin in the proper format with the oscilloscope.
Post ALL the code, using code tags, so we can see what you have done wrong.
Use of Strings is a bad idea with Arduino, as they cause memory problems and program crashes. See forum.arduino.cc/index.php?topic=382944.msg2640860#msg2640860
Use C-strings (zero terminated character arrays) instead.
Well, in this case the situation is so basically simple, I’m not convinced the rest of the code would do anything but open me up to additional criticism for my code style. The application involves the device waiting for a very short serial command and then doing something about it.
BACK TO PROBLEM AT HAND:
For the benefit of others that may encounter this oddity with SERIAL1, it appears that some of the Arduino based macros don’t function properly (e.g., Serial1.read(), etc.) with the Pro-Micro. Instead, I had to tickle register values directly to get it to function:
void serial1_Check() // serial port poll routine - called frequently from loop() (if stringComplete != TRUE)
{
if (UCSR1A & 0x80) // in register: UCSR1A, RXC1 = receive complete, input-byte is waiting
{
++cmdLEN; // increment length of command
char inChar = (char)UDR1; // UDR1 is the register with received byte from Serial1, move to single char
inputString += (char)inChar; // add to string
if (inChar == 13) // if(inChar==‘\n’) DOES NOT WORK on serial1 ← I’m looking for a carriage return
{
stringComplete = true; // starts the other process
cmdLEN = 0;
return;
}
if (cmdLEN >= 10)
{
cmdLEN = 0;
cmdERR = true;
inputString = “”;
return;
}
}
}
Yes, an interrupt driven routine would be more efficient and so would a string array based on NULL characters, but I was just trying to get it to work first. I’ll do the other homework now that I know how to get SERIAL DATA into the separate hardware interface of the Pro Micro (using register values directly). I guess we need to be cautious with the use of Arduino macros for the Pro-Micro. I wonder what other oddities I will encounter(?)
“It has been my experience that” The SparkFun ProMicro will not receive data via the hardware serial(1) port unless the following register tickle statement appears in this exact order:
Serial1.begin(9600); // Serial1 is hardware serial port on ProMicro
UCSR1B = (1<<RXEN1) | (1<<TXEN1); // enable transmitter and receiver USART via registers