ciao a tutti sto realizzando una macchinetta che viene comandata da pc ed ha due motori in continua.
così ho creato un driver motore e sto utilizzando arduino e la shield ethernet più due servi per far girare la webcam..
ho provato a fare il codice ma non vuole funzionare..
il codice è il seguente
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,239);
EthernetServer server(272);
void setup()
{
myservo.attach(9);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
// boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '8') {
for(pos = 0; pos < 180; pos += 1)
{ // 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(c=='2') {
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
}
}
}
}
delay(1);
// close the connection:
client.stop();
}
in poche parole lo sketch non mi da errore ma quando apro il programma in java inserisco l'ip e il server e il servo non si muove tramite i comandi che gli do.. non fa nulla..
mi potreste aiutare ? grazie
Il primo consiglio è ... scrivi il codice in modo ORDINATO con le giuste indentature :
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,0,239);
EthernetServer server(272);
void setup()
{
myservo.attach(9);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
// boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '8') {
for(pos = 0; pos < 180; pos += 1)
{ // 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(c=='2') {
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
}
}
}
}
delay(1);
// close the connection:
client.stop();
}
... oltretutto c'è uno strumento che te lo sistema a automaticamente nell'IDE : Tools -> Auto Format
Poi ... quando ti connetti specifichi, oltre l'indirizzo, anche la porta giusta (ho visto che ne usi una particolare) ?
Altra cosa da fare è usare la Serial per mandare dei messaggi sul monitor seriale e guardare cosa veramente ricevi ...
ad esempio, dopo
char c = client.read();
mettere una bella
Serial.println(c, HEX);
... così ti stampa, in esadecimale, il carattere ricevuto. Ho messo l'opzione esadecimale perché, se il carattere ricevuto non è un carattere standard, non lo vedresti ... almeno così sai il suo codice esadecimale
Non funziona NON significa molto ...
... cosa non funziona ? Cosa vedi sul monitor seriale ? Arrivano i caratteri ? la scheda risponde al ping ? la vedi in rete ? ecc. ecc.