Any ideas on why this code would work on a Uno, but not on a micro?

Here is the code. It works fine on a Uno, but I get no output from the software serial on a micro. I’ve tested the serial port with a simple echo program, and the software serial, and both work fine. Something about this code however isn’t jiving with the Uno.

#include <SoftwareSerial.h>

SoftwareSerial toRemote(4, 5);

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
boolean buttonPushed = false;


void setup() {
  Serial.begin(19200);// initialize serial
  toRemote.begin(19200); //startup Software Serial
  // reserve 40 bytes for the inputString, it will be cleared long before this.
  inputString.reserve(40);
}

void loop() {
  if (stringComplete) { // print the string when a newline arrives:
    toRemote.println(inputString); 
    inputString = "";  // clear the string:
    stringComplete = false;
  }   
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.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 == 13) {
      stringComplete = true;
    }
  }
}

Maybe you never see a CR.

Def. seeing CRs. I ran the software serial into a serial port and read the output with a simple echo program I wrote to test, and I'm getting CRs. I've tried it with CRs and with newlines.

Does sw serial support SerialEvent?

westfw: Does sw serial support SerialEvent?

No. But the data is coming in on Serial, which does, and out (somehow) on the software serial instance.

The use of serialEvent() is just complicating your sketch for no benefit, and so is the use of the String class.

I also don't see any purpose to buffering a line of text before you output it, but since your code doesn't protect against buffer overflow, this is introducing a failure mode into your sketch.

I suggest you write a new sketch which just reads each available character as it arrives and prints it to your software serial port. Make sure the sketch prints out a message on the software serial output when it starts up so that you can prove the mechanism you're using to receive that output is working correctly. If all your assumptions are valid, that should work without any problem. If you have a reason for wanting to buffer the string, you can then add that code into a known working solution. In that case I suggest you resist the temptation to use serialEvent() and the String class.

The Arduino Micro (ATmega32u4) has both Serial and Serial1 available. Serial is the USB port and Serial1 is the hardware USART port on pins 0 and 1. You don’t need software serial. I believe Software serial depends on pin change interrupts and neither pin 4 or 5 have pin change interrupt capability.

http://arduino.cc/en/Main/ArduinoBoardMicro

I believe Software serial depends on pin change interrupts

It does.

and neither pin 4 or 5 have pin change interrupt capability.

Good thing someone looked.

Would that prevent data from being [u]sent[/u] via SoftwareSerial? If so, it suggests that the testing described in the original post wasn't actually performed as described.

PeterH: Would that prevent data from being [u]sent[/u] via SoftwareSerial? If so, it suggests that the testing described in the original post wasn't actually performed as described.

I don't see that. The original post says:

It works fine on a Uno, but I get no output from the software serial on a micro.

I'm looking at this part:

I've tested the serial port with a simple echo program, and the software serial, and both work fine.

I take that to mean that the OP has seen output from the SoftwareSerial on the target board. However, I don't know whether the OP did actually see that output.

I just had a very (exactly) similar experience! I have two software serial ports called out in my code, but I couldn't receive data on one of them.

I believe the answer lies here: http://www.arduino.cc/en/Reference/SoftwareSerial

This page states "Not all pins on the Leonardo support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI)"

I was using pins 2 and 3 initially, and then 4 and 5, and neither of those would work on the micro. The would work on the Uno however. Once I changed my software serial pins to 9 and 10 for the problem SoftwareSerial channel, viola! Problem solved. Hope this helps.