arduino mega + wifly shield + bluetooth module

Hi

i need help urgently as i am very stuck on a code i am developing. I have a arduino mega connected to a wifly shield (connected to the main serial), i also have a Bluetooth device connected to the mega as well (connected on Serial 3). i want to be able to read client information when it is available. once the client is disconnected i want to be able to configure the wifly shield using the Bluetooth module. my code works separately however wont work when i put it together, i have tried so many combinations and nothing has worked. i will post my code so far below. if anyone can help i will be so grateful

#include <SPI.h>
#include <WiFly.h>


WiFlyServer server(2000);

int led = 31;

String readString;
char c;



void setup() {
    Serial.begin(9600);
    Serial3.begin(9600);
   SpiSerial.begin();
    WiFly.begin();
      server.begin();
      pinMode(led, OUTPUT);
      
     
}

void loop() {
WiFlyClient client = server.available();

if (client.connect()) {
      while (client.available()) {
        c = client.read();
        readString += c; //makes the string readString
    delay(5);  //slow looping to allow buffer to fill with next character
      }
  
if (readString.length() >0){
   
   int commaIndex = readString.indexOf(',');
   int secondCommaIndex = readString.indexOf(',', commaIndex+1);

String firstValue = readString.substring(0, commaIndex);
String secondValue = readString.substring(commaIndex+1, secondCommaIndex);
String thirdValue = readString.substring(secondCommaIndex+1); // To the end of the string

int x = firstValue.toInt();
int y = secondValue.toInt();
int z = thirdValue.toInt();

Serial.println(x);

if(x == 5){

digitalWrite(led, HIGH);
  
      
  }else{

digitalWrite(led, LOW);

  }
    
  }
}

 if (!client.connected() || !server.available()){

  while(SpiSerial.available() > 0) {

#if ARDUINO >= 100
    Serial3.write(SpiSerial.read());
#else
    Serial3.print(SpiSerial.read(), BYTE);
#endif
  }
  
  if(Serial3.available()) { // Outgoing data
#if ARDUINO >= 100  
    SpiSerial.write(Serial3.read());
#else
    SpiSerial.print(Serial3.read(), BYTE);
#endif
  }

}



   readString=""; //empty for next input
 }

////////

i have update my code while i have been working on it, i have gotten closer to my answer, however when i connect using my phone via bluetooth on my phone terminal i only get the first letter of my commands when i enter the command config part of my wifly device. i know it is a delay issue but cant seem to figure out where to put the delay

thank you

Looks to me like the WiFly Shield is not compatible with the Arduino MEGA 2560. It uses the SPI interface which are on different pins on the UNO and MEGA. That is why the Ethernet Shield and some other shields use the ICSP header for the SPI interface: because it is in the same place on the UNO and MEGA.

johnwasser:
Looks to me like the WiFly Shield is not compatible with the Arduino MEGA 2560. It uses the SPI interface which are on different pins on the UNO and MEGA. That is why the Ethernet Shield and some other shields use the ICSP header for the SPI interface: because it is in the same place on the UNO and MEGA.

i herd that as well but after a little digging i found out that if u attach the pins from the shield (13,12,11,10) to the mega pins (50, 51, 52, 53) they work, however could this be the issue to one of my problems once i program the board my simple code works fine

#include <SPI.h>
#include <WiFly.h>


WiFlyServer server(80);

int led = 31;

String readString;
char c;



void setup() {
    Serial.begin(9600);
    Serial3.begin(9600);
   SpiSerial.begin();
    WiFly.begin();
      server.begin();
      pinMode(led, OUTPUT);
      
     
}

void loop() {
WiFlyClient client = server.available();

if (client.connect()){
      while (client.available()) {
        c = client.read();
        readString += c; //makes the string readString
    delay(5);  //slow looping to allow buffer to fill with next character
      }
  
if (readString.length() >0){
   
   int commaIndex = readString.indexOf(',');
   int secondCommaIndex = readString.indexOf(',', commaIndex+1);

String firstValue = readString.substring(0, commaIndex);
String secondValue = readString.substring(commaIndex+1, secondCommaIndex);
String thirdValue = readString.substring(secondCommaIndex+1); // To the end of the string

int x = firstValue.toInt();
int y = secondValue.toInt();
int z = thirdValue.toInt();

Serial.println(x);

if(x == 5){

digitalWrite(led, HIGH);
  
      
  }else{

digitalWrite(led, LOW);

  }
    
  }
 
}

if (!client.connected()) {

client.stop();
  
}


readString=""; //empty for next input



 }

however as soon i pull out the usb wire and put it back in. my program stops working can you please help me in figuring out why

thank you

Pulling out the USB cable will reset the Arduino. Perhaps something is failing in your setup. You should add code to check and report the return values for any of the function calls that can return a value.