I finally got a moment to test out my Ethernet shield. First issue was that I have a rev.3 shield, and a rev.2 Uno ... ah so it has 4 extra pins. Covered them up and plugged it in. Voila.
I was able to bring the shield up through DHCP, no problem. Next I configured it as a server and tried a simple query and response sketch. Loaded up the IP in my browser and sure enough, there's a response being sent back. So far so good. Now on to what I really need to do, which involves a servo.
I connected the servo, wrote a function that sweeps it, and I called the function from within the main loop. The idea is that while the servo is sweeping back and forth, I can load it up in a browser and it tells me at what angle the servo is at (at the moment the browser sent the request.) I can hit refresh and watch the angle change. Great ... except, I noticed it's blocking the sweep. Every time I hit refresh, the servo stops for a brief moment, then continues.
I can't wrap my head around this one ... I need to be able to request the servo position without affecting the servo itself. I know it's not the servo.read() call. When I comment it out, I still notice the brief pause. So that tells me it's the process of sending data back to the browser that causes the blocking. Is there a way to prevent that? Ultimately I'm going to be reading two servos, probably at 10Hz resolution, and I don't want to block the servos from what they're doing.
Suggestions? Here's the mandatory code:
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x03, 0x00, 0x10 };
EthernetServer server = EthernetServer(80);
// Initialize Servo
Servo myServo;
int pos = 0;
long prevMillis;
int dir = 1;
int pause = 5;
void setup() {
Serial.begin(9600);
myServo.attach(9);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
Serial.print("IP: ");
Serial.println(Ethernet.localIP());
server.begin();
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
servoSweep();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
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();
client.print("servo is at: ");
client.print(myServo.read());
client.println(" degrees.");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
void servoSweep(void) {
if (millis() - prevMillis > pause) {
if (dir) {
if (pos < 180) {
pos += 1;
pause = 5;
} else {
dir =! dir;
pause = 1000; // Pause for 1 second
}
} else {
if (pos > 0) {
pos -= 1;
pause = 5;
} else {
dir =! dir;
pause = 1000; // pause for one second
}
}
myServo.write(pos);
prevMillis = millis();
}
}
PS: That code generates 15340 bytes. Talk about big ... doesn't leave much room for other stuff I need to be doing, like adding Wire support to talk to another controller for starters. And there will be more libraries to be added ...