Arduino Mega 2560 Serial not working?

Hi,

I have a Mega2560, i have just added a bluetooth module from Sparkfun: https://learn.sparkfun.com/tutorials/using-the-bluesmirf?_ga=1.218309661.584573556.1417027436
However I have done exactly as the tutorial shows however I am not receiving any data on the serial, this was true even before I added the bluetooth module. For example if i just upload a simply program with a line saying Serial.print etc i still do not have anything show up in the serial monitor. I also had a program which was supposed to read from the serial monitor however this also didnt work.

I was just wondering whether there is something simple I am missing out here?

Thanks in advance for any replies, Jack.

Hello and welcome :slight_smile:

Did you wire it correctly (RX to TX0-3, TX to RX0-3), bauds set correctly in serial monitor? And post your code.

kcajlliw:
For example if i just upload a simply program with a line saying Serial.print etc i still do not have anything show up in the serial monitor.

Is this happening when nothing is connected to any of the Mega pins? - in which case that is the first issue to resolve before doing anything about bluetooth.

On the other hand if it only happens when the bluetooth module is connected I suspect you are trying to use the Bluetooth with pins 0 and 1 (which belong to the USB connection) rather than connecting the bluetooth module to Serial1 which uses pins 18 and 19.

...R

Thanks for both of your replies. Sorry for the delay.

I have got the serial working properly now. The only problem is that the character being read from the bluetooth is always showing as 255.
I am using pins 0 and 1, should i change these to 18 and 19?

(Sorry I started another topic as I didnt realise they were related)

Thanks again, Jack.

Sorry, Here is the code:

#include <SoftwareSerial.h>
const int RX_PIN = 0;
const int TX_PIN = 1;
const int bluetoothVCC = 30;
const int bluetoothGND = 31;
SoftwareSerial serial(RX_PIN, TX_PIN);
char commandChar;
const int leftForward = 39;
const int leftReverse = 49;
const int rightForward = 27;
const int rightReverse = 45;
const int echopin = 24;
const int trigpin = 22;
bool lf = false;
bool lr = false;
bool rf = false;
bool rr = false;
int distpoint = 10;
int distance;
byte incomingByte;
void setup()
{
        Serial.begin(9600);
	pinMode(leftForward, OUTPUT);
	pinMode(rightForward, OUTPUT);
	pinMode(leftReverse, OUTPUT);
	pinMode(rightReverse, OUTPUT);
        pinMode(echopin, INPUT);
        pinMode(trigpin, OUTPUT);
        pinMode(bluetoothVCC, OUTPUT);
        pinMode(bluetoothGND, OUTPUT);
        digitalWrite(bluetoothVCC, HIGH);
        digitalWrite(bluetoothGND, LOW);
  //andomSeed(analogRead(0))
        
}

void loop()
{
        checkserial();
        distance = checkdistance();
        if (distance < 1)
        {
         //Something strange, lets ignore it.
        }
        else if (distance < distpoint){
          //TOO CLOSE, LETS REVERSE
            Serial.println("REVERSE");
            lf = false;
            rf = false;
            rr = true;
            lr = true;
            drive(); 
            delay(1500);
            //TURN RIGHT A SMIDGE
            rr = false;
            lr = false;
            lf = true;
            rf = false;
            drive(); 
            delay(500);
            rr = false;
            lr = false;
            lf = true;
            rf = true;
            drive();
        }  
        else {
           //Serial.println(distance);
           //Keep going forward
           drive();
        }      
      //KEEP IT SLOW  
       delay(500);
}

int checkdistance()
{
        digitalWrite(trigpin, LOW);
        delayMicroseconds(2);
        digitalWrite(trigpin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigpin, LOW);
        distance = pulseIn(echopin, HIGH);
        distance = distance/58;
        return distance;
  
}

void drive()
{
        if (lf){
        digitalWrite(leftForward, HIGH);
        digitalWrite(leftReverse, LOW);
        }
        else if(lr){
        digitalWrite(leftForward, LOW);
        digitalWrite(leftReverse, HIGH);
        }
        
        if (rf){
        digitalWrite(rightForward, HIGH);
        digitalWrite(rightReverse, LOW);
        }
        else if(rr){
        digitalWrite(rightForward, LOW);
        digitalWrite(rightReverse, HIGH);
        }
}

void checkserial()
{
  //IS THIS NEEDED?
  if (Serial.available() > 0) {
		// read the oldest byte in the serial buffer:
		incomingByte = Serial.read();
                Serial.println("DATA RECIEVED");
                Serial.println(incomingByte);
		//SELECT COMMAND FROM LIST
	                 if (incomingByte == 'Q'){ 
                          lf = true;
                          rf = false;
                          rr = false;
                          lr = false;
                         }
                        else if (incomingByte =='A'){
                          lf = false;
                          rf = false;
                          rr = false;
                          lr = true;
                        }
                        else if (incomingByte =='F'){ 
                          lf = true;
                          rf = true;
                          rr = false;
                          lr = false;
                        }
                        else if (incomingByte =='B'){ 
                          lf = false;
                          rf = false;
                          rr = true;
                          lr = true;
                        }
                        else if (incomingByte =='P'){
                          lf = false;
                          rf = true;
                          rr = false;
                          lr = false;
                        }
                        else if (incomingByte =='L'){
                          lf = false;
                          rf = false;
                          rr = true;
                          lr = false;
                        }
                        else if (incomingByte =='y'){
                          lf = true;
                          rf = false;
                          rr = true;
                          lr = false;
                        }
                        else if (incomingByte == 'T'){
                          lf = false;
                          rf = true;
                          rr = false;
                          lr = true;
                        }
			else 
                        {
                        Serial.println("Unknown Command");
                        }	
		}  
}
1 Like
SoftwareSerial serial(RX_PIN, TX_PIN);
void checkserial()
{
  //IS THIS NEEDED?
  if (Serial.available() > 0) {

Serial1 is not the same as serial1 - c/c++ is case sensitive

And why are you using SoftwareSerial on a mega?

Mark

Oh - you also need a Serial1.begin()

M

Thanks for your reply.

Okay thanks. Sorry but just wondering why you have a symbol on the end of serial?

On the SoftwareSerial note - I am very novice with the arduino and this is what a tutorial suggested, but the tutorial was designed for an Arduino Uno. I am planning on moving the bluetooth system onto an Arduino uno when i have got it all working as it is going to be integrated into an electric car. Does the mega have an alternative?

Thanks again, Jack.

You can't use Software Serial on the same pins (0 and 1) that Hardware Serial uses. And on a Mega you can't use it on pins 14 to 19 either - they are used by the 3 other Hardware Serials. And with all those Hardware Serials there is no need to use Software Serial at all on a Mega.

...R