Adruino Web Server with Motors control

Hi Guys,

I'm new to Arduino and require some advise on my project.
I'm currently working on a project which require me to control varies type of motor etc(Servo, Stepper & DC motor) via web browser. But currently i had an issue that i'm only able to control 1 type a motor at a time. Once i input all 3 types of codes together, it seem not working. Im using Arduino UNO, Arduino Ethernet shield & Adafruit motor shieldV2.3 on this project. I need some advise. my source code are attached below.

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h> //Servo Motor Library
//Include Adafruit DCmotor lib
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

//Assign Motor types variable

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M3
Adafruit_DCMotor *myMotor = AFMS.getMotor(1); //DC motor on M3 port
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotorStepper = AFMS.getStepper(200, 2);//Stepper use M3 & M4

//Servo motor
Servo myservo;  // create servo object to control a servo 
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }; //physical mac address
byte ip[] = { 
  192, 168, 1, 178 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask


EthernetServer server(80); //server port
String readString="";


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection and the server:

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  //Include DCmotor setup
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(40);
  //myMotor->run(FORWARD);
  //turn on motor
  myMotor->run(RELEASE);

  myMotorStepper->setSpeed(30);//Stepper Speed

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object, Adafruit on Servo 2
}
//Main loop

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        //if (readString.length() < 100) {
        //store characters to string
        readString += c;
        //Serial.print(c);
        //}
        //if HTTP request has ended
        if (c == '\n') {
          Serial.println(readString); //print to serial monitor for debuging
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          //client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          //client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<TITLE>Max Liew Dc Motor Control Webserver </TITLE>");
          client.println("</HEAD>");

          client.println("<BODY style='background-color:lightgrey'>");
          client.println("<H1 style='color:#333399 text-align:center'>Max Liew Dc Motor Control Webserver </H1>");
          client.println("<hr />");

          client.println("<H2 style=\"text-align:center\">Arduino with Ethernet Shield & Adafruit Motor Shield</H2>");

          //DC Motor
          client.println("<h2>DC Motor</h2>");
          client.println("<input type=button value=Forward onmousedown=\"location.href='/?button2Forward'\"></input>&nbsp");
          client.println("<input type=button value=Backward onmousedown=\"location.href='/?button2Backward'\"></input>&nbsp");       
          client.println("<input type=button value='STOP' onmousedown=\"location.href='/?button2Stop'\"></input>
"); 

          //Stepper Motor
          client.println("<h2>Stepper Motor</h2>");
          client.println("<input type=button value=Forward onmousedown=\"location.href='/??FSingle'\"></input>&nbsp");
          client.println("<input type=button value=Backward onmousedown=\"location.href='/?BSingle'\"></input>");       


          //Servo Motor
          client.println("<h2>Servo Motor</h2>");
          client.println("<input type=button value=Left onmousedown=\"location.href='/?Fleft'\"></input>&nbsp");
          client.println("<input type=button value=Right onmousedown=\"location.href='/?Fright'\"></input>
");   

          client.println("</BODY>");
          client.println("</HTML>");
          delay(1);
          //stopping client
          client.stop();


          //controls the Arduino if you press the buttons
          if (readString.indexOf("?button2Forward") >0){
            delay(15);
            myMotor->run(FORWARD);//Motor will rotate forward
          }
          if (readString.indexOf("?button2Backward") >0){
            delay(15);
            myMotor->run(BACKWARD);//Motor will rotate backward
          }

          if (readString.indexOf("?button2Stop") >0){
            delay(15);
            myMotor->run(RELEASE);//Motor will STOP
          }
          //Stepper motor

          if (readString.indexOf("?FSingle") >0){
            delay(15);
            myMotorStepper->step(200,FORWARD, SINGLE);
          }
          if (readString.indexOf("?BSingle") >0){
            delay(15);
            myMotorStepper->step(200,BACKWARD, SINGLE);
          }

          //Servo
          if (readString.indexOf("?Fright") >0){
            for(pos ; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
            { // in steps of 1 degree
              myservo.write(pos); // tell servo to go to position in variable 'pos'
              delay(15); // waits 15ms for the servo to reach the position
            }
          }
          if (readString.indexOf("?Fleft") >0){
            for(pos ; pos>=1; pos-=3) // goes from 180 degrees to 0 degrees
            {
              myservo.write(pos); // tell servo to go to position in variable 'pos'
              delay(15); // waits 15ms for the servo to reach the position
            }
          } 

          readString="";//clearing string for next read
        }
      }
    }
  }
}

Your code is not formatted at all so it is almost impossible to read. Use the Auto-Format option in the Arduino IDE and post the revised version.

You probably need to remove all the delay()s. Use millis() to manage timing as illustrated in several things at a time.

I think it would make sense to use one of the examples in serial input basics (probably the 2nd one) to receive the data.

It also uses a string (small s) rather than a String (capital S) as Strings do not work well in the small memory of an Arduino.

I suggest you search for all the items in the received string and update some variables as appropriate before you try to make any of the motors do anything. Don't mix up the parsing and the action.

If you do the parsing separately you can easily print the result of the parsing to check that it is what you expect. You can test that without any motors attached.

...R
Planning and Implementing a Program
Stepper Motor Basics

Develop your motor control code using the serial monitor first. When you get the code working, then you can add the web interfacing.

Thank you Zoomkat & Robin2 for the advise.
Robin2 your tutorial are really helpful. It really give a headup to organizing my code.