Arduino IDE serial communication

Hi,

I have been trying this simple programme to send data from an arduino due to an atmega328:i was using the following code:
Arduino:

void setup() {
   Serial.begin(9600); 
   
}
void loop()
{
  Serial.println("hello"); 
  Serial.println('h'); 
}

atmega:

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte,DEC);
}
}

while this worked perfectly, when i modified the code a little as follows, i did not get the expected results:
arduino:

void setup() {
   Serial.begin(9600); 
   
}
void loop()
{
  Serial.println('a');  
  Serial.println('b');  
}

atmega:

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

if(incomingByte =='a')
 {
    Serial.println("a");
 }
else if (incomingByte =='b')
 {
    Serial.println("b");
 }
else
 {
    Serial.println("d");
 }
}

Please help!!!!!!! :~

Please help!!!!!!!

With what????????????????????????????????????????????????????????????????

i did not get the expected results:

But, I'm not going to tell you what I got, or what I expected. Please help!!!!!!!!!!!!!!!!!!!!!!!!! anyway. Bullpoop.

First of all, when you do such a test, use some delays in the transmitter.
You are now overflowing the buffers.
You could for example transmit once or twice a second the word "Hello".

The Serial.println() prints a string followed by carriage-return and line-feed.
To transmit a single character, use Serial.write().
http://arduino.cc/en/Serial/write

The Serial.println() prints a string followed by carriage-return and line-feed.
To transmit a single character, use Serial.write().

I simplified the code and tried this:
arduino:

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}
 
void loop()
{
  Serial.write('a');
}

atmega:

void setup() {
   Serial.begin(9600); 
   
}
void loop()
{
  if(Serial.available()>0)
  {
    Serial.println('y');
  }
}

But there is no serial data transmission happening. As of now I have the RX/TX of the atmega conneected to RX3/TX3 of the arduino.

koelsinha:
As of now I have the RX/TX of the atmega conneected to RX3/TX3 of the arduino.

I don't know if that's the case, but RX must be connected to TX3, and TX to RX3.

And also, for the Mega code, you must use Serial3.begin and Serial3.available.

I don't know if that's the case, but RX must be connected to TX3, and TX to RX3.

The RX/TX are connected correctly i.e. RX-TX3 and TX-RX3.

And also, for the Mega code, you must use Serial3.begin and Serial3.available.

Its an arduino due

@koelsinha, can you post a diagram showing how everything is connected.

How are you checking the values that are being received by the Atmega328? It seems to be sending the data back to the Due but the Due is not listening.

Someone said you need to put some delay in the transmission code but you have not done so. Add a 1 second delay and see what happens.

void loop()
{
  Serial.write('a');
  delay(1000);
}

...R

can you post a diagram showing how everything is connected.

I have attached the diagram with this post(img.png)

How are you checking the values that are being received by the Atmega328?

I have the atmega 328 placed in an arduino uno board. So I am checking the values on the serial monitor of the arduino uno.
Also, I have noticed 2 things:

  1. when I power on the arduino due when the serial monitor of the UNO is already open, the letter Y gets printed(as in the code for the atmega), although when I try print the value that I am passing from the arduino due, random values get printed.
    for example:
    arduino:
void loop()
{
  Serial.write('a');
  delay(1000);
}

atmega:

void loop()
{
  if(Serial.available()>0)
  {
    Serial.println('y');
    char ch=Serial.read();
    int n= Serial.read();
    Serial.print("n=");
    Serial.println(n);
    Serial.print("ch=");
    Serial.println(ch);
    delay(1000);
  }

I have attached the output that i get(output.png).

  1. When i put on the serial monitor for the atmega when the arduino is already powered, I get no output whatsoever.

output.JPG

  if(Serial.available()>0)
  {
    Serial.println('y');
    char ch=Serial.read();
    int n= Serial.read();

If there is at least one byte to read, read two of them. Does that seem reasonable?

If there is at least one byte to read, read two of them. Does that seem reasonable?

I am very new to this and am still learning.
I have been trying different things.
When i try the following:

void loop()
{
  if(Serial.available()>0)
  {
    Serial.println('y');
    int n= Serial.read();
    Serial.print("n=");
    Serial.println(n);
    //Serial.print("ch=");
    //Serial.println(ch);
    delay(1000);
  }

  
 }

the same thing happens.
When i start the serial monitor of the atmega when the arduino is already powered, there is not output.and when i start the serial monitor of the atmega first and then plug in the arduino, i get random ouput. i hve attached an img on the ouput(img1.png)

img1.JPG

when i start the serial monitor of the atmega first and then plug in the arduino, i get random ouput.

Are those two green lines the only connection between the two Arduinos?

If so, I suggest that you connect the grounds together. You can't send data from one to another without a complete circuit, and you can't have a complete circuit without the grounds connected.

Are those two green lines the only connection between the two Arduinos?

Yes they were.

If so, I suggest that you connect the grounds together. You can't send data from one to another without a complete circuit, and you can't have a complete circuit without the grounds connected.

I have a ground connection between the arduino and the atmega as in the image attached.(Is that the only other connection i need to make or am i missing something else too). This time, on repeating the same procedure, i get no output in both cases.

The output from the one Arduino is connected to the Serial3 instance on the other Arduino. The code you posted suggests that you are trying to read the Serial3 data using the Serial instance. It is up to you to prove otherwise.

You seem to be trying to use the hardware serial port on the Uno for two things at a time - to communicate with the PC (via USB) and to communicate with the DUE. I'm not surprised it is not working. I am suprised you think it should work.

Use SoftwareSerial on the Uno to communicate with the DUE using some pins apart from 0 and 1.

OR, power the Uno with a battery (or wall wart) and use SoftwareSerial and an FTDI cable to communicate with the PC rather than the standard USB connection.

...R

You seem to be trying to use the hardware serial port on the Uno for two things at a time - to communicate with the PC (via USB) and to communicate with the DUE. I'm not surprised it is not working. I am suprised you think it should work.

I use the hardware serial port on my arduino to both communicate with the pc and another arduino with no problem. Below is some simple test code I've used to communicate between two arduinos and read the sent data on their respective serial monitors.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

zoomkat:
I use the hardware serial port on my arduino to both communicate with the pc and another arduino with no problem.

Interesting. I thought they would confuse each other at the electrical level.

And at the code level how to you determine whether Serial.print() goes to the IDE or to the other device? Or does it go to both and you don't care?

...R

zoomkat:

You seem to be trying to use the hardware serial port on the Uno for two things at a time - to communicate with the PC (via USB) and to communicate with the DUE. I'm not surprised it is not working. I am suprised you think it should work.

I use the hardware serial port on my arduino to both communicate with the pc and another arduino with no problem. Below is some simple test code I've used to communicate between two arduinos and read the sent data on their respective serial monitors.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 

//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin.
//Connect the arduino grounds together.
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

//expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

This is OT.
I do not have the hardware to check this, but sure would like to hear from you how you manage to switch the IDE between two COM ports. Just curious.
I am trying to cobble something so I can send test data directly to the port. Getting tired to type the test string into monitor. Got it running but need to adjust the timing so it looks as a real ASCII character as far as baud rate goes.
Since I know the character send I'll adjust the timing until the received character matches. Sounds easy, but...
Cheer
Vaclav

Robin2:

zoomkat:
I use the hardware serial port on my arduino to both communicate with the pc and another arduino with no problem.

Interesting. I thought they would confuse each other at the electrical level.

And at the code level how to you determine whether Serial.print() goes to the IDE or to the other device? Or does it go to both and you don't care?

All the pc and arduino tx do is change voltage levels on a wire and the rx just monitors for changes in the voltage levels on the wire. If two devices are both trying to control the line voltage at the same time, there can be conflicts. If the communications are managed to avoid conflicts, then there usually aren't issues. The below previous discussion might be worth a revisit.

http://forum.arduino.cc/index.php?PHPSESSID=d80voinehla2srvgne47dv0fl6&topic=175851.msg1305132#msg1305132

Thanks everyone for your inputs.
I made a few corrections to my code. now the code is as follows:
Arduino due:

void setup() {
   Serial3.begin(9600); 

}
void loop()
{
  Serial3.flush();
  Serial3.write('<');
  Serial3.write('1');  
  Serial3.write('>');
}

Atmega:

  int n=0;
  int c=0;
void setup() {
   Serial.begin(9600); 
   
}
void loop()
{
  if(Serial.available()>0)
  {
  
    c= Serial.read();
    if(c==60)                       // if the character < is encountered, store next incoming byte in n
    {
      n=Serial.read();         
    }
    if else(c==62)
    {
      n=n;                         //If the character > is encountered, maintain existing value in n
    }
    else
    {
      n=n;                         // maintain existing value of n
    }
    
    delay(1000);
  }
   
 }

Mostly the code is working, except when:

  1. I first put on the serial monitor connected to the atmega, I encounter about 18-20 random values before the correct value(in this case 49) starts transmitting.
  2. I close the serial monitor connected to the atmega and open it again (while the atmega is powered), I encounter random values and as opposed to the first case the actual value is never transmitted.
void loop()
{
  Serial3.flush();
  Serial3.write('<');
  Serial3.write('1');  
  Serial3.write('>');
}

Block until all pending output has been sent, then send 3 characters. Repeat as fast as possible. Why?

  1. I first put on the serial monitor connected to the atmega, I encounter about 18-20 random values before the correct value(in this case 49) starts transmitting.

Hand-shaking is CLEARLY needed.

  1. I close the serial monitor connected to the atmega and open it again (while the atmega is powered),

Resetting the atmega twice...

  if(Serial.available()>0)
  {
  
    c= Serial.read();
    if(c==60)                       // if the character < is encountered, store next incoming byte in n
    {
      n=Serial.read();         
    }

If there is at least one character to read, why are you INSISTING that there will be two to read?