I want to use the GSM shield on the MEGA which uses pin 10 and pin3 (&pin7). But i also would like to use this GPS module( GPS shield - Elecrow), documentation of the GPS module shows that i have to use software serial and I have to use pin D0-7.
both shields use a serial port. So don't "sandwich shields up" on top of your MEGA, connect the Tx/Rx pins to Hardware Serial ports of your mega and drive the communication correctly. (might means you need to rewrite some of those libraries which are designed for software serial)
//at 9600 bps 8-N-1
//Computer is connected to Arduino/Crowduino
//SoftSerial Shield is connected to the Software UART:D2&D3
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(6, 7);
unsigned char buffer[256]; // buffer array for data recieve over serial port
int count=0; // counter for buffer array
void setup()
{
SoftSerial.begin(9600); // the SoftSerial baud rate
Serial.begin(9600); // the Serial port of Arduino baud rate.
}
void loop()
{
if (SoftSerial.available()) // if date is comming from softwareserial port ==> data is comming from SoftSerial shield
{
while(SoftSerial.available()) // reading data into char array
{
buffer[count++]=SoftSerial.read(); // writing data into array
if(count == 256)break;
}
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero
}
if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook
SoftSerial.write(Serial.read()); // write it to the SoftSerial shield
}
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++)
{ buffer[i]=NULL;} // clear all index of array with command NULL
}
Would it be possible to just use the TX2 PIN 16 and RX2 PIN17 and use command like;
void serialEvent2(){
}
instead of using the softwareserial lib to make use of D0-D7