Help to configure my codes: (BLDC motor is fluctuating)

Hi, especially to the mods, before you move this topic of mine, please check first. :slight_smile: I’m posting it on PROGRAMMING section because the issue here is a little bit of programming.

My codes here works 95% smoothly. Please do check it out:

as indicated in my todo:
BLDC Motor is fluctuating, probably because of the wifi codes. Will you help me to configure the codes? I’m new to millis() ← that’s probably the solution.

Additional: What I mean in fluctuating, when the CC3000 shield has the ip address already and ready to be penetrated by telnet (lol), the motor is turning in every direction for every 2 seconds? not sure. But it produces beeps continuously. Anyway, I developed a C# program for telnet-ting the arduino via wifi, and when I connect it to arduino and starts pressing ‘W’ or ‘S’, the motor runs smoothly.


(I’m posting the code in my reply, too long to post)

//--- THIS CODE controls the BLDC using CC3000 wifi shield, ESC, arduino, and a telnet program
//--- use puTTy or the developed C# program.
//--- Hold 'W' for incrementing the speed of the motor, 'S' for decrease or reverse.

//--- TO DO: Motor is fluctuating, probably because of the wifi codes.

 * Adafruit CC3000 Breakout/Shield TCP Chat Server
 * This is a simple chat server which allows clients to connect
 * with telnet and exchange messages. Anything sent by one
 * client will be written out to all connected clients.
 * See the CC3000 tutorial on Adafruit's learning system
 * for more information on setting up and using the
 * CC3000:
 * Requirements:
 * This sketch requires the Adafruit CC3000 library. You can
 * download the library from:
 * For information on installing libraries in the Arduino IDE
 * see this page:
 * Usage:
 * Update the SSID and, if necessary, the CC3000 hardware pin
 * information below, then run the sketch and check the
 * output of the serial port. After connecting to the
 * wireless network successfully the sketch will output
 * the IP address of the server and start listening for
 * connections. Once listening for connections, connect
 * to the server from your computer using a telnet client
 * on port 23.
 * For example on Linux or Mac OSX, if your CC3000 has an
 * IP address you would execute in a command
 * window:
 * telnet 23
 * Connect multiple clients and notice that whatever one client
 * sends will be echoed to all other clients. Press ctrl-] and
 * type quit at the prompt to close the telnet session.
 * On Windows you'll need to download a telnet client. PuTTY
 * is a good, free GUI client:
 * License:
 * This example is copyright (c) 2013 Tony DiCola (
 * and is released under an open source MIT license. See details at:
 * This code was adapted from Adafruit CC3000 library example
 * code which has the following license:
 * Designed specifically to work with the Adafruit WiFi products:
 * ---->
 * Adafruit invests time and resources providing this open source code,
 * please support Adafruit and open-source hardware by purchasing
 * products from Adafruit!
 * Written by Limor Fried & Kevin Townsend for Adafruit Industries.
 * BSD license, all text above must be included in any redistribution.
#include <Servo.h> //Notice how you can use the standard servo library! 

#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 "marshmallows" // cannot be longer than 32 characters!
#define WLAN_PASS "Yanabels82190"
#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 ==============//

//============== BLDC+ESC CODES ty to zoomkat ==============//
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;
//============== END OF BLDC+ESC CODES =========//

void setup(void)

  Serial.println(F("Hello, CC3000!\n"));
  Serial.print("Free RAM: "); 
  Serial.println(getFreeRam(), DEC);
  /* Initialise the module */
  if (!cc3000.begin())
    Serial.println(F("Couldn't begin()! Check your wiring?"));
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
    delay(100); // ToDo: Insert a DHCP timeout!
  /* Display the IP address DNS, Gateway, etc. */
  while (! displayConnectionDetails()) {
  /* 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
  Serial.println(F("Listening for connections..."));

//=========== BLDC + ESC config ==============//
  myservo.attach(9, 400, 2600); //servo control pin, and range if desired
//=========== END OF BLDC + ESC CONFIG =======//

void loop(void)
  // Try to get a client which is connected.
  Adafruit_CC3000_ClientRef client = chatServer.available();
  if (client) {
    // Check if there is data available to read.
    if (client.available() > 0) {
      // Read a byte and write it to all clients.
      char thisChar =;
      if (thisChar == '\n') 
            char cmdArray[1024];
                Serial.println("XXX - arduino to arduino serial monitor"); // do whatever you want
                //          chatServer.println("Equal - XXX arduino to server");
                //          chatServer.println(cmdArray);
                // for ESC+BLDC
                //         chatServer.println("OK GOT IT not XXX arduino to server");
                //         chatServer.println(cmdArray);
                // for ESC + BLDC
                (pos=pos-1); //use larger numbers for larger increments
                if(pos<0) (pos=0); //prevent negative number
                if(pos >= 400) //determine servo write method
            // END OF BLDC+ESC codes
            cmd = "";
        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;
    Serial.print(F("\nIP Addr: ")); 
    Serial.print(F("\nNetmask: ")); 
    Serial.print(F("\nGateway: ")); 
    Serial.print(F("\nDHCPsrv: ")); 
    Serial.print(F("\nDNSserv: ")); 
    return true;

Oh, there will be 4 ultrasonic sensors (I can get the readings from the 4 sensors at the same time, but with a super bit of a delay) to be read by the arduino and be passed to the C# program via wifi. Should I use interrupts(pls say no, I’m noob in arduino)?
Anyone? (1:55AM, 1/29/2015, I guess I should check this topic when I wake up)

glenn_boy13: Oh, there will be 4 ultrasonic sensors to be read by the arduino and be passed to the C# program via wifi. Should I use interrupts?

No. Use the NewPing library and follow the example that shows how to use 15 ultrasonic inputs. Scale that back to four.

wow. that's great! Thanks for this sir. :)

I want an answer in my original topic, about my whole codes. please??? up!

Hi, especially to the mods, before you move this topic of mine, please check first. :) I'm posting it on PROGRAMMING section because the issue here is a little bit of programming.

Hello moderators and administrators, is it possible to move again this topic to another threads/sections? Like in motor section or programming section? Thanks.

Which is it?

(I found this in "Project Guidance", not "Programming", which confuses me)

Hi sir, I originally posted this topic in PROGRAMMING section, and I believe someone moved it to here, PROJECT GUIDANCE.