2 arduino MICRO serial communication?!

HI,
i have two arduino micros, and i want micro A is sending data to micro B

Im trying now for 2nd day and it's driveng me nuts!!!

If i connect USB cable to micro A (this one is sending data),
i can see the data in serial monitor and the RX led lights when the data is sending...
BUT
When i disconect the usb cable and power arduino micro A with a battery...
noting works... it dosent send the data
i have managed to debug that it hangs on
while(!Serial);

The code from micro A
The code is simpyfied, but in reality i read rotary encoder and want to send the data to micro B

int i;
void setup(){
  Serial.begin(9600);
  while(!serial);
}
void loop(){
 Serial.print('#');
 Serial.print(i);
 Serial.print('

The code from micro B (simlyfied)

int incomingByte = 0;
void setup(){
  Serial.begin(9600);
  while(!Serial);
}
void loop(){
if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
}

HELP?);
i++;
}


The code from micro B (simlyfied)

§DISCOURSE_HOISTED_CODE_1§


HELP?

I have alsow tryed with

Serial1
//insted of
Serial

Dosen't work

Post your actual code using Serial1, plus a picture or drawing of all the electrical connections (including power supplies), plus a description of what you expected to happen and what actually happened.

Ok, if we go back to basics.....
Lets say i have 1 arduino micro from who i want to send data via UART(serial) pin0 and pin1
and no USB cable attached, just a battery.... it just dosent work

If i understand correct
if i have only one arduino micro that is sending data to Serial1, the Tx LED should blink,
if i load the code like this right?
And eaven if i don't connect anything to tx/rx, right??

int i;
void setup(){
  Serial1.begin(9600);
}
void loop(){
 Serial1.print('#');
 Serial1.print(i);
 Serial1.print('

);
i++;
}

How do you have the wires between the two boards hooked?

board 1 RX => board 2 TX
board 1 TX => board 2 RX
board 1 GND => board 2 GND
board 1 5V => board 2 5V

On the Micro, doesn't the Rx & Tx LEDs indicate transfers over the USB port? Not the UART serial port?

Ales:
Ok, if we go back to basics.....

So you're sending data, as far as we can guess - where is that data expected to go to? What evidence do you have to show whether or not it was received there?

Get rid of while(!Serial);

In your sending machine, put a bit of a pause between the Serial.print() statements.

in your receiving machine it looks like you are acknowledging receipt by just sending back to the sending machine. We need to see some indication that it was received (maybe blink light 3 times, or if you have an LCD display on there, send it a message.

Ok now i made simples communication possible and it's still not working
Sender code

//sender COM27
int i = 0;
void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  delay(3000); 
}
void loop(){
  digitalWrite(13,HIGH);
  Serial.println(i);
  delay(500);
  digitalWrite(13,LOW);
  delay(500);
  i++;
  if(i > 3) i = 0;
}

Reciver code

//reciver com23
int incomingVal;
void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  delay(3000);
}
void loop(){
  delay(2);
  if(Serial.available()){
    incomingVal=Serial.read();
    if(incomingVal=='2')
      digitalWrite(13,HIGH);
    else
      digitalWrite(13,LOW);
  }  
}

And yes... arduino micro dosent light up RX/TX LED if no USB cable is attached...

IF i send via terminal (from PC) data like number 2 in my case,
yes everithing works like it should, but i want to arduinos to comunicate via serial (no PC)
Any idea why this is not working?

I have changed reciver code to this, to test if serial port is initializet…
and NOW it dosent… it hangs on while(!Serial)

Then i have tested and hooked up and Arduino DUE to be sender…
Then the below code works for setup…so the serial gets initialized, but dosent recive no data…

Did i just found a bug in arduino MINI?

void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  while (!Serial) ;
  for( i=0; i < 10; i++){
    digitalWrite(13,HIGH);
    delay(200);
    digitalWrite(13,LOW);
    delay(200);    
  }
}

I have alsow tryed SoftwareSerial on reciver end

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 4
#define ledPin 13
int incomingVal,i;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;
void setup()  {
  Serial.begin(9600);
  while(!Serial);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  mySerial.begin(9600);
  delay(3000);
  for( i=0; i < 3; i++){
    digitalWrite(13,HIGH);
    delay(200);
    digitalWrite(13,LOW);
    delay(200);    
  }  
}
void loop() {
    incomingVal=mySerial.read();
    if(incomingVal > 0){
      Serial.println(incomingVal);
      if(incomingVal=='2')
        digitalWrite(13,HIGH);
      else
        digitalWrite(13,LOW);
    }
}

And still get nothing throughs serial

NOW WHAT?

I have now managed to send data trought serial from arduino DUE via Serial1
and recive it via Serial1 on arduino Micro,
if i upload the sending code to another Micro....still dosen't work, so im pritty shure that
Arduino Micro have a serial sending issue....

That is great. Can you share your two sketches that work, and the pin connections you use.

Thanks, Jack

jackwp:
That is great. Can you share your two sketches that work, and the pin connections you use.

Thanks, Jack

This is on DUE - is sending data

//sender COM26

int i = 0;
void setup(){
  pinMode(13,OUTPUT);
  Serial1.begin(9600);
  delay(3000);
 while(!Serial); 
}
void loop(){
  digitalWrite(13,HIGH);
  Serial1.println(i);
  delay(500);
  digitalWrite(13,LOW);
  delay(500);
  i++;
  if(i > 3) i = 0;
}

This is on micro that is reciveing via serial

//reciver COM23
int incomingVal,i;
void setup()  {
  Serial1.begin(9600);
  while(!Serial1);
  delay(3000);
  for( i=0; i < 3; i++){
    digitalWrite(13,HIGH);
    delay(200);
    digitalWrite(13,LOW);
    delay(200);    
  }  
}
void loop() {
    incomingVal=Serial1.read();
    if(incomingVal > 0){
      if(incomingVal=='2'){
        digitalWrite(13,HIGH);
        delay(500);
      }else
        digitalWrite(13,LOW);
    }
}

And its not that great, becouse i wanted to use 2 micros not 1 micro and 1 DUE :smiley:

So how do you know is working??
On sender LED13 is constantly blinkink,
if the connection is ok on Reciver LED13 is alsow blinkig in 1:3 ratio, meaning sender blinks 3x reciever 1x

I wired the thing like

Sender  Reciver
RX1  =   TX
TX1  =   RX
GND =   GND

It is great that is working. If you want to test with leonardo boards, try this link
http://www.instructables.com/id/EC81OWMHK482OMF/ Test: arduino to arduino serial using softwareSerial library

Ales:
And its not that great, becouse i wanted to use 2 micros not 1 micro and 1 DUE :smiley:

Did you ever get it working with two Micros?

Note: the Micro uses "Serial1" for TTL communications. The "Serial" is used for USB communications.
Quote from http://arduino.cc/en/Main/ArduinoBoardMicro

Note that on the Micro, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

You can try this, it works when I tested it.
Sending:

int led = 13;
int i = 0;

void setup() {
  Serial1.begin(9600);
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(13,HIGH);
  Serial1.print(i);
  delay(500);
  digitalWrite(13,LOW);
  delay(500);
  i++;
  if(i > 3) i = 0;
}

Receiving:

int led = 13;
int incomingVal;

void setup() {
  Serial1.begin(9600);
  pinMode(led, OUTPUT);     
}

void loop() {
  delay(2);
  if(Serial1.available()){
    incomingVal=Serial1.read();
    if(incomingVal=='2')
      digitalWrite(13,HIGH);
    else
      digitalWrite(13,LOW);
  }
}