Pages: [1]   Go Down
Author Topic: Serial Communication only works for UNO(not MEGA)  (Read 1124 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

The code example below have been working flawlessly on Arduino UNO for me, however, when it's put on Arduino MEGA 2560, it doesn't seem to work at all.
The code consists of two parts: the sender and the reciever. It's a quite simple setup consisting of one Arduino MEGA sending 'a' and 'b' continously through serial communication. The problem seems to occur with the other Arduino MEGA which is supposed to recieve the input and print it out in its own serial monitor.

Sender:
Code:
#include <SoftwareSerial.h>

#define rxPin 0
#define txPin 1

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello Board 2");
}

void loop()
{
  //continously send out 'a' and 'b'.
  mySerial.print('a');
  delay(100);
  mySerial.print('b');
  delay(100);
 }

Reciever:
Code:
#include <SoftwareSerial.h>

#define rxPin 0
#define txPin 1

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello Board 1");
}

void loop()
{
  // listen for new serial coming in:
  char someChar = mySerial.read();
  // print out the character:
  mySerial.println(someChar);
  delay(100);
 }

I'm using Arduino 1.0, and you can see from the example above, i'm using the SoftwareSerial library.
First time I've posted on the Arduino board, so let me know if I've let anything out.

Thanks smiley
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you using the hardware serial pins for SoftwareSerial? Why not use hardware serial?

Check the reference page for pins that you can use on a Mega. http://arduino.cc/en/Reference/SoftwareSerial
Logged

Offline Offline
Edison Member
*
Karma: 20
Posts: 2114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i've learnt not to touch pins 0/1 never... unless it's purely of no use eg LED show, use 2/3 to send and to recv 3/2 (whatever that translates into on the mega, i only have the uno and ether10)
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much, it works now smiley

I changed the the pin RX to one of those in the link you posted.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you using SoftwareSerial?
Logged

Pages: [1]   Go Up
Jump to: