SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By MichaelL65
#193979
I am working on a project involving a 20x4 serial LCD display and a ProMicro (both from Sparkfun). The intent is to be able to send ascii strings via RS-232 to the ProMicro and display them on the LCD. Since this requires a second serial port, I used the SoftwareSerial library.

I had it working, then ran into a problem when having it run on it's own power - it wouldn't run the program unless I gave it a reset. After some troubleshooting I realized it had the same problem when powered through the USB port as well (It ran fine after an upload or a reset, but not on power-up).

So I tried running the same code with a normal Uno and it works fine. I tried it on other SparkFun ProMicros and it has the same issue. (Since the ProMicro is a variation on the Leonardo, I'll see if I can find one in my shop and try on it for my next step.)

This is the first time I have had any issues with the ProMicro. I've used it for several other projects and they are running just fine, so this caught me a little off guard (This was the first to try to use the ProMicro with the SoftwareSerial though).

Is there an issue with the SoftwareSerial library working with the ProMicro? I did search the forums, but it was inconclusive as questions were mostly unanswered or abandoned (the original poster either gave up or got it working without saying how). Any help would be appreciated!

Michael



PS: FWIW, Here's the code: Like I said, it works perfectly on an UNO, and will work on the ProMicro, but I have to reset it in order to get it going. (NOTE: once this was running on it's own power I intended to switch Serial to Serial1 and use a MAX232, but it never got that far)
Code: Select all
#include <SoftwareSerial.h>

int lcdPin = 8;                       // Use pin 8 to communicate to display
int dline;                            // number to select display line 
int bufflen;                          // length of the input text
int port = 1;                         // define the port for this board. Future versions will probably have DIP switches

String serInput;                      // String to hold out input
char buff[20];

SoftwareSerial lcd(lcdPin+1,lcdPin);  // Create an instance of the LCD


void line(int x){
  switch (x){
    case 1:
      lcd.write(254);                   // go to line 1
      lcd.write(128);
      break;
    case 2:
      lcd.write(254);                   // go to line 2
      lcd.write(192);
      break;
    case 3:
      lcd.write(254);                   // go to line 3
      lcd.write(148);
      break;
    case 4:
      lcd.write(254);                   // go to line 4
      lcd.write(212);
      break;
  }
}

void clear(){
  line(1);                            // go to line 1
  lcd.write("                    ");  // Clear Line 1
  lcd.write("                    ");  // Clear Line 2
  lcd.write("                    ");  // Clear Line 3
  lcd.write("                    ");  // Clear Line 4
}

void setup() {
  Serial.begin(9600);                 // initialize main serial port
  while(!Serial){}
  lcd.begin(9600);                    // initialize LCD Port
  delay(1000);
  clear();
  line(1);
  lcd.write("    Model LCD232    ");  // Display the model number
  line(2);
  lcd.write("Serial Display Panel");  // Display the model name
  line(3);
  lcd.write("   Revision 0.1.0   ");  // Display the revision info
  line(4);
  lcd.write("      (C) 2017      ");  // Display Copyright info
  delay(5000);                        // display for 5 seconds
}

/* With each new string received, we check the very first character to see what port it
 * is meant to go to. If it isn't us, we ignore it.
 * Next we check for the line number. If it is valid (1 - 4), we point the curser to that line 
 * and send the remainder of the string to the display
 */

void loop() {
  if(Serial.available()){             // see if serail data is available
    serInput = Serial.readString();   // Get the string of data from the serial port
    if(serInput.startsWith(String(port,HEX))){ // reads the port control characer (skip if not for us)
      serInput.remove(0,1);           // remove the port control character
      dline = int(serInput.charAt(0)-48); // reads the line number control character
      serInput.remove(0,1);           // remove the line number control character
      if((dline>=1)&&(dline<=4)){     // only continue if line number is valid
        bufflen=serInput.length()+1;
        if(bufflen > 20){
          bufflen = 20;
        }
        serInput.toCharArray(buff,bufflen);
        line(dline);                  // go to the line indicated
        lcd.write(buff);          // put the string on the indicated line
        for(int x = bufflen; x<=20; x++){
          lcd.write(" ");
        }
//        Serial.println(serInput);
      }
//      else {
//        Serial.print("Bad Line number: ");
//        Serial.println(dline);
//      }
    }
//    else {
//      Serial.print("Not mine: ");
//      Serial.println(serInput);
//      }
  }
}
By MichaelL65
#193987
I did get to try this on an Arduino Pro Mini (It worked fine) and an Arduino Leonardo (Same problem as SparkFun ProMicro)

So for whatever reason it doesn't like to run automatically on the Atmel ATMega 32u4 (but will after a reset).
By MichaelL65
#194004
Well, this feels a bit silly really.
Code: Select all
  while(!Serial){}
I misunderstood what this was doing, and as a result it was hanging the program. This is used to detect an open serial connection before continuing. It pretty much always returns true on most Arduino boards because the serial connection to the USB controller is always there. But on chips like the 32U4, it detects a connection to a virtual serial port, which isn't always there. Oddly enough, a hardware reset on the ProMicro will usually get it past this for some reason. On the Leonardo, it doesn't.

TLDR: Don't use the
Code: Select all
while(!Serial){}
unless you need the sketch to wait for you to open a serial connection!