Hi All,
I am using an Arduino Ethernet and Adafruit Motor shield V2 on a project to control the motors over ethernet.
I have managed to get the ethernet control aspect to work, interpretting GET requests etc. and I have been controlling the on-board LED successfully.
When I connect the motor shields (I am using 2) and add in the coding to control the motors the arduino fails to connect to acquire an ethernet connection.
I have checked pin-outs etc. The motor shield uses the SDA and SCL pins of the arduino (nearest the reset button) and the ethernet capability uses pins 10-13. The motor shield communicates over I2C whereas the ethernet uses the SPI bus.
Firstly is this all correct?
Secondly can anyone suggest why these functions aren't working together
///////////////////WEB MOTOR CONTROL////////////////////
///////////////////LIBRARIES////////////////////
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
///////////////////DEFINITIONS////////////////////
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 15
///////////////////VARIABLES////////////////////
String get;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
int motor_steps = 20; // set motor steps
///////////////////ETHERNET STUFF////////////////////
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x10, 0x70 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetServer server(80);
///////////////////MOTOR STUFF////////////////////
Adafruit_MotorShield ShieldBot(0x61); // Rightmost jumper closed
Adafruit_MotorShield ShieldTop(0x60); // Default address, no jumpers
//Connect Motors
Adafruit_StepperMotor *pitch_motor = ShieldBot.getStepper(200, 1); //Attach pitch motor
Adafruit_StepperMotor *yaw_motor = ShieldBot.getStepper(200, 2); //Attach yaw motor
Adafruit_StepperMotor *roll_motor = ShieldTop.getStepper(200, 2); //Attach roll motor
///////////////////SETUP////////////////////
void setup() {
pinMode(9,OUTPUT);
// Open serial communications:
Serial.begin(9600);
Serial.println("Started");
///////////////ETHERNET///////////////////
// Start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
digitalWrite(9,LOW);
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your IP address:
Serial.print("IP address: ");
for (byte thisByte = 0; thisByte < 3; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println(Ethernet.localIP()[3], DEC);
digitalWrite(9,HIGH);
////////////////////MOTORS//////////////////
//Begin Motor shields:
ShieldBot.begin(); // Start the bottom shield
ShieldTop.begin(); // Start the top shield
// Start stepper motors
pitch_motor->setSpeed(10);
yaw_motor->setSpeed(10);
roll_motor->setSpeed(10);
}
Code won't all fit in one post, see next post for loop
///////////////////LOOP////////////////////
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
String get = HTTP_req;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
strstr(HTTP_req," H")[0] = 0;
Serial.println(HTTP_req);
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
///////////////////HTML////////////////////
// send page
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("</head>");
client.println("<body>");
client.println(" <h1>");
client.println(" Motor Control");
client.println(" </h1>");
client.println(" <p>");
client.println(" Use the buttons below to adjust the motors.");
client.println(" </p>");
client.println("<table width='100%' border='0'>");
client.println("<tr>");
client.println(" <td>");
client.println(" </td>");
client.println(" <td align='center'>");
client.println(" <a href='/?up'>");
client.println(" <button style='width:100%'>");
client.println(" <h2>");
client.println(" Up");
client.println(" </h2>");
client.println(" </button>");
client.println(" </a>");
client.println(" </td>");
client.println(" <td>");
client.println(" </td>");
client.println("</tr>");
client.println("<tr>");
client.println(" <td align='center'>");
client.println(" <a href='/?left'>");
client.println(" <button style='width:100%'>");
client.println(" <h2>");
client.println(" Left");
client.println(" </h2>");
client.println(" </button>");
client.println(" </a>");
client.println(" </td>");
client.println(" <td>");
client.println(" </td>");
client.println(" <td align='center'>");
client.println(" <a href='/?right'>");
client.println(" <button style='width:100%'>");
client.println(" <h2>");
client.println(" Right");
client.println(" </h2>");
client.println(" </button>");
client.println(" </a>");
client.println(" </td>");
client.println("</tr>");
client.println("<tr>");
client.println(" <td>");
client.println(" </td>");
client.println(" <td align='center'>");
client.println(" <a href='/?down'>");
client.println(" <button style='width:100%'>");
client.println(" <h2>");
client.println(" Down");
client.println(" </h2>");
client.println(" </button>");
client.println(" </a>");
client.println(" </td>");
client.println(" <td>");
client.println(" </td>");
client.println("</tr>");
client.println("<tr style='height:20px'>");
client.println(" <td>");
client.println(" </td>");
client.println(" <td>");
client.println(" </td>");
client.println(" <td>");
client.println(" </td>");
client.println("</tr>");
client.println("<tr>");
client.println(" <td align='center'>");
client.println(" <a href='/?ccw'>");
client.println(" <button style='width:100%'>");
client.println(" <h2>");
client.println(" CCW");
client.println(" </h2>");
client.println(" </button>");
client.println(" </a>");
client.println(" </td>");
client.println(" <td>");
client.println(" </td>");
client.println(" <td align='center'>");
client.println(" <a href='/?cw'>");
client.println(" <button style='width:100%'>");
client.println(" <h2>");
client.println(" CW");
client.println(" </h2>");
client.println(" </button>");
client.println(" </a>");
client.println(" </td>");
client.println("</tr>");
client.println("</table>");
client.println("
");
client.println("</body>");
client.println("</html>");
Serial.println("Page Sent");
//check for button presses
//}
if (strstr(HTTP_req, "GET /?up")!=0){
//What to do when button press detected
pitch_motor->step(motor_steps, FORWARD, INTERLEAVE);
digitalWrite(9,HIGH);
Serial.println("Move: Up");
}
else if (strstr(HTTP_req, "GET /?down")!=0){
//What to do when button press detected
pitch_motor->step(motor_steps, BACKWARD, INTERLEAVE);
digitalWrite(9,LOW);
Serial.println("Move: Down");
}
else if (strstr(HTTP_req, "GET /?left")!=0){
//What to do when button press detected
yaw_motor->step(motor_steps, FORWARD, INTERLEAVE);
digitalWrite(9,HIGH);
Serial.println("Move: Left");
}
else if (strstr(HTTP_req, "GET /?right")!=0){
//What to do when button press detected
yaw_motor->step(motor_steps, BACKWARD, INTERLEAVE);
digitalWrite(9,LOW);
Serial.println("Move: Right");
}
else if (strstr(HTTP_req, "GET /?ccw")!=0){
//What to do when button press detected
roll_motor->step(motor_steps, FORWARD, INTERLEAVE);
digitalWrite(9,HIGH);
Serial.println("Move: CCW");
}
else if (strstr(HTTP_req, "GET /?cw")!=0){
//What to do when button press detected
roll_motor->step(motor_steps, BACKWARD, INTERLEAVE);
digitalWrite(9,LOW);
Serial.println("Move: CW");
}
else{
Serial.println("no change");
}
req_index = 0;
get = "";
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
} // end of if client available
} // end of while client connected
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
} // end of if client
} // end of loop
There were no links in my original post, I was asking if my assumptions were correct and whether anyone had any ideas why it might not be working in principle.
I have been using one motor to test the sketch and have been powering off USB.
I think zoomkat was implying you should not power the motor(s) with the same power supply as the Arduino, and certainly not from the 5v pin. Have you tried the ethernet connection if you remove the motor and leave the motor shields connected?
I think that was the point that PaulS, in his inimitable way, was trying to make.
I can't help- my first thought was power- but I'm curious about the physical setup. Have you got the shields actually plugged in as shields?- ie an Arduino/ Ethernet shield/ Motor shield / Motor shield Dagwood?
Good point about the uncertainty of stacking shields. "Motor shield Dagwood" reminds me of the current US burger commercials, roll the eyes back and unhinge the jaws!