I have a servo (pin 9) connected to an Arduino Mega which is connected to an Ethernet port and routed off to my PC. My desire is that when I send a page request through the Ethernet shield, I can set the servo to point in a specific direction. I am having problems getting the servo and Ethernet code on the Arduino to work together, and I don't know why. If you have any example code that combines the Ethernet protocol and a servo command, I'd greatly appreciate it
The command for where the laser is to point is tacked on to the URL on the page request sent to the Arduino (characters 10-12). At the end of reading and replying to a packet, the servo is set to a specific position. However, I find that after the first attempt to point the laser (after the first page request) I can no longer get a response from the Arduino (it hangs, as if the server.available() method never triggers the page request). If I comment out the command myservo.write(deg); the code will function normally (the Arduino responds to three successive page requests), although the laser will not move. I thought this might have to do with how long the myservo.write(deg); takes to execute, but it only takes 12ms at most (including debug print statements to the terminal) and the code continues to work with the command myservo.write(deg); replaced with a delay(50);
I have tried this code with the Servo connected to pin 9 as well as connected to pin 3, but I encounter the same problem both times.
My only thought is that there must be some conflicting code between the Ethernet and Servo classes, so when the Servo class is called, it interrupts the functions of the Ethernet class, preventing it from detecting future page calls, but this doesn't make sense to me since I call the Servo class in the initialization method, meaning that if this were the case, I'd never be able to get the Ethernet to work so long as I left that intiial call uncommented, so I am at a loss as to how to fix this problem. Thank you for your assistance.
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 42 };
//pins
int servoPin=9;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
Servo myservo;
int laserPos = 90;
int motorControl=0;
int motorDist=0;
void setup()
{
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
myservo.attach(servoPin);
laserServo(laserPos);
}
void zeroVars()
{
laserPos = 0;
motorControl=0;
motorDist=0;
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
zeroVars();
int index=0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
index++;
if(index>=10&&index<=12)
{
int temp=c-48;//char to int
//Serial.println(laserPos);
laserPos+=temp*(index==10?100:index==11?10:1);
}
if(index==18)
{
motorControl=c-48;
}
if(index>=24&&index<=26)
{
int temp=c-48;//char to int
motorDist+=temp*(index==24?100:index==25?10:1);
}
// 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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(analogChannel));
client.println("
");
}
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;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.flush();
client.stop();
Serial.println("A: ");
Serial.println(laserPos);
laserServo(laserPos);
Serial.println("B");
}
}
void laserServo(int deg)
{
if(deg>=0&°<=179)
{
Serial.println("C: ");
Serial.println(millis());
myservo.write(deg);
Serial.println("D: ");
Serial.println(millis());
}
}