Pessoal, estou com uma dúvida. Tenho o seguinte esquema: página web que, por GET, envia um parâmetro para o arduino. O arduino lê o parâmetro e faz com que quatro saídas ligadas a uma ponte H, ative dois motores DC. Fiz um teste com apenas um motor, e o shield recebe a informação normalmente mesmo eu mandando em pequenos intervalos de tempo uma requisição GET. Mas quando insiro dois motores no arduino, às vezes ele trava, fica em estado de processamento e não lê a requisição enviada pelo browser.
O código fonte do arduino é esse:
#include <SPI.h>
#include <Ethernet.h>
//motor 1
int left1 = 6;
int rigth1 = 7;
//motor 2
int left2 = 8;
int rigth2 = 9;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x18, 0x7D };
byte ip[] = { 192,168,0,170 };
byte gateway[] = { 192,168,0,1 };
byte subnet[] = { 255,255,255,0 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void toHead() {
//motor 1
digitalWrite(left1, HIGH);
digitalWrite(rigth1, LOW);
//motor 2
digitalWrite(left2, HIGH);
digitalWrite(rigth2, LOW);
}
void toBack() {
//motor 1
digitalWrite(rigth1, HIGH);
digitalWrite(left1, LOW);
//motor 2
digitalWrite(rigth2, HIGH);
digitalWrite(left2, LOW);
}
void stopEngine(){
//motor 1
digitalWrite(rigth1, LOW);
digitalWrite(left1, LOW);
//motor 2
digitalWrite(rigth2, LOW);
digitalWrite(left2, LOW);
}
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
//serial Communication
server.begin();
Serial.begin(9600);
//configura sinal de saída pros motores
pinMode(rigth1, OUTPUT);
pinMode(rigth2, OUTPUT);
pinMode(left1, OUTPUT);
pinMode(left2, OUTPUT);
}
void loop()
{
// listen for incoming clients
Client client = server.available();
char command;
if (client) {
Serial.println("");
Serial.println("read client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean isFirstLine = true;
boolean getToken = false;
boolean finishConnection = false;
byte countIndex = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (isFirstLine)
{
if (c == '%')
{
getToken = false;
finishConnection = true;
}
if (getToken)
{
command = c;
}
if (c == '=')
{
getToken = true;
}
if (c == '\n')
{
isFirstLine = false;
}
}
// 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 (finishConnection) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
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);
Serial.println(command);
client.stop();
Serial.println("fecha conexão com cliente");
}
switch(command)
{
case 'w':
case 'W':
Serial.println("To Head");
toHead();
break;
case 's':
case 'S':
Serial.println("To Back");
toBack();
break;
case 'z':
case 'Z':
Serial.println("Stop Engine");
stopEngine();
break;
}
}
Alguém ae sabe oq acontece quando coloco os dois motores?
Obs.: a alimentação da ponte H, é feita por uma fonte externa!