NewPing library doesn't work with other part of my codes

Hello all, I have here 4 Ultrasonic Sonar Sensors. My codes is in this link (the forum won't accept such long codes)

Anyway here's my story:
I use NewPing library and I think sensor reading hinders my data transfer from pc to arduino via telnet:

Check the start of the void loop()
I have commented the part that apparently hinders the data transfer.

Thanks
Glenn

I can't access rubbish bin, and wouldn't even if I could. You CAN attach your code to the forum, using the Additional Options link below the new post window or the Reply window (but not the useless Quick Reply window).

 #include <NewPing.h>
    //====== ADAFRUIT WIFI CODES
    #include <Adafruit_CC3000.h>
    #include <String.h>
    #include <SPI.h>
    #include "utility/debug.h"
    #include "utility/socket.h"
    // These are the interrupt and control pins
    #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
    // These can be any two pins
    #define ADAFRUIT_CC3000_VBAT 5
    #define ADAFRUIT_CC3000_CS 10
    // Use hardware SPI for the remaining pins
    // On an UNO, SCK = 13, MISO = 12, and MOSI = 11
    Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
    SPI_CLOCK_DIVIDER); // you can change this clock speed
    #define WLAN_SSID "ClintGlenn" // cannot be longer than 32 characters!
    #define WLAN_PASS "john3:16"
    // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
    #define WLAN_SECURITY WLAN_SEC_WPA2
    #define LISTEN_PORT 23 // What TCP port to listen on for connections.
    Adafruit_CC3000_Server chatServer(LISTEN_PORT);
    String cmd;
    #define MAX_CMD_LENGTH   25
    //============== END OF ADAFRUIT WIFI CODES ==============//
     
     
    //============sensor codes
    #define SONAR_NUM     4// Number or sensors.
    #define MAX_DISTANCE 350 // Max distance in cm.
    #define PING_INTERVAL 33 // Milliseconds between pings.
     
    unsigned long pingTimer[SONAR_NUM]; // When each pings.
    unsigned int cm[SONAR_NUM]; // Store ping distances.
    uint8_t currentSensor = 0; // Which sensor is active.
     
    NewPing sonar[SONAR_NUM] = { // Sensor object array.
      //trigger,pin,maxdist
      NewPing(22, 24, MAX_DISTANCE),
      NewPing(26, 28, MAX_DISTANCE),
      NewPing(30, 32, MAX_DISTANCE),
      NewPing(34, 36, MAX_DISTANCE)
      };
     
     
    void setup(void)
    {
     
    Serial.begin(115200);
     
    //====sensors codes
        pingTimer[0] = millis() + 75; // First ping start in ms.
        for (uint8_t i = 1; i < SONAR_NUM; i++)
          pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
     
    //===end of sensors
     
     
     
    //  Serial.println(F("Hello, CC3000!\n"));
    //  Serial.print("Free RAM: ");
      //Serial.println(getFreeRam(), DEC);
     
      /* Initialise the module */
    //  Serial.println(F("\nInitializing..."));
      if (!cc3000.begin())
      {
    //    Serial.println(F("Couldn't begin()! Check your wiring?"));
        while(1);
      }
      if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    //    Serial.println(F("Failed!"));
        while(1);
      }
    //  Serial.println(F("Connected!"));
    //  Serial.println(F("Request DHCP"));
      while (!cc3000.checkDHCP())
      {
        delay(100); // ToDo: Insert a DHCP timeout!
      }
      /* Display the IP address DNS, Gateway, etc. */
      while (! displayConnectionDetails()) {
        digitalWrite(13,LOW);
        delay(1000);
      }
      /*********************************************************/
      /* You can safely remove this to save some flash memory! */
      /*********************************************************/
    //  Serial.println(F("\r\nNOTE: This sketch may cause problems with other sketches"));
    //  Serial.println(F("since the .disconnect() function is never called, so the"));
    //  Serial.println(F("AP may refuse connection requests from the CC3000 until a"));
    //  Serial.println(F("timeout period passes. This is normal behaviour since"));
    //  Serial.println(F("there isn't an obvious moment to disconnect with a server.\r\n"));
      // Start listening for connections
      chatServer.begin();
      digitalWrite(13,HIGH);
    //  Serial.println(F("Listening for connections..."));
     
     
    //----- passing to arduino UNO serially
    //Serial.begin(115200);
     
     
    }
    void loop(void)
    {
     
      //================= START SENSOR (NewPing) CODES ==========//
      // --------- ERROR STARTS FROM HERE I GUESS... Arduino doesn't receive
      // --------- the data from the telnet client because of these pieces of codes.
      // --------- If these are deleted, program works 100% smoothly.
     
            for (uint8_t i = 0; i < SONAR_NUM; i++) {
        if (millis() >= pingTimer[i]) {
          pingTimer[i] += PING_INTERVAL * SONAR_NUM;
          if (i == 0 && currentSensor == SONAR_NUM - 1)
            oneSensorCycle(); // Do something with results.
          sonar[currentSensor].timer_stop();
          currentSensor = i;
          cm[currentSensor] = 0;
          sonar[currentSensor].ping_timer(echoCheck);
        }
      }  
    //====================== END SENSOR (NewPing) CODES ===========//  
      // Try to get a client which is connected.
      Adafruit_CC3000_ClientRef client = chatServer.available();
      if (client) {
     
        client.flush();
        // Check if there is data available to read.
        if (client.available() > 0) {
          // Read a byte and write it to all clients.
          char thisChar = client.read();
          if (thisChar == '\n')
          {
    //        Serial.println(cmd);
     
            char cmdArray[1024];
            cmd.toCharArray(cmdArray,cmd.length()+1);
            if(strcmp(cmdArray,"W")==0)
            {
    //          Serial.println("XXX - arduino to arduino serial monitor"); // do whatever you want
              //          chatServer.println("Equal - XXX arduino to server");
              //          chatServer.println(cmdArray);
             // Serial.write("W");
              Serial.print("W"); // send W signal to UNO
              digitalWrite(44,HIGH);
            }
            if(strcmp(cmdArray,"S")==0)
            {
    //          Serial.println("S");
     
              //         chatServer.println("OK GOT IT not XXX arduino to server");
              //         chatServer.println(cmdArray);
             // Serial.write("S");
              Serial.print("S");
              digitalWrite(44,LOW);
            }  
     
            cmd = "";
          }
     
          else
          {
            cmd += thisChar;
          }
     
     
        }
      }
    }
     
    //=========== DO NOT MIND THE CODES BELOW.... DEFAULT ADAFRUIT WIFI CODES ================//
     
     
    /**************************************************************************/
    /*!
     @brief Tries to read the IP address and other connection details
     */
    /**************************************************************************/
    bool displayConnectionDetails(void)
    {
      uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
      if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
      {
    //    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
        return false;
      }
      else
      {
    //    Serial.print(F("\nIP Addr: "));
     
        cc3000.printIPdotsRev(ipAddress);
    //    Serial.print(F("\nNetmask: "));
        cc3000.printIPdotsRev(netmask);
    //    Serial.print(F("\nGateway: "));
        cc3000.printIPdotsRev(gateway);
    //    Serial.print(F("\nDHCPsrv: "));
        cc3000.printIPdotsRev(dhcpserv);
    //    Serial.print(F("\nDNSserv: "));
        cc3000.printIPdotsRev(dnsserv);
    //    Serial.println();
        return true;
      }
    }
     
    //=============sensors codes
    void echoCheck() { // If ping echo, set distance to array.
      if (sonar[currentSensor].check_timer())
        cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
    }
     
    void oneSensorCycle() { // Do something with the results.
      for (uint8_t i = 0; i < SONAR_NUM; i++) {
       chatServer.print("sensor");
       chatServer.print(i);
       chatServer.print("=");
       chatServer.print(cm[i]);
       chatServer.print("cm ");
      }
     chatServer.println("");
    }
    //==========end of sensors codes

I have contacted the author of the NewPing via email and he replied:

My guess would be one of two things. First, it could simply be that you're not setting the serial speed in the Arduino compiler to 115200 as it's being set in the sketch. Many people set this speed to much slower and don't notice that I'm setting it to 115200. These must match or you won't get output (or it will be corrupt).

The other possibility is that there's a conflict with a timer interrupt. To get around this, use v1.6 beta of NewPing and change the define that turns off the timer functions (as well as turning off the timer interrupts). You'll have to read the library header which tells you exactly what the define is (I'm not at a computer to get the define name).

Tim

I just don't quite know what he's talking about.
G