Salve a tutti
Sto cercando di controllare il movimento di un servomotore con le coordinate di un mouse. Vi posto le sketch di arduino e processing che uso:
PROCESSING
import processing.serial.*;
Serial port;
int p;
void setup()
{
println(Serial.list());
size(1200,200);
port = new Serial(this,"/dev/tty.usbserial-A70061cJ",9600);
}
void draw()
{
p=mouseX;
port.write(p);
println(p);
}
ARDUINO
int servoPin1 = 9; // Control pin for servo motor
int minPulse = 600; // Minimum servo position
int maxPulse = 3000; // Maximum servo position
int pulse1 = 0; // Amount to pulse the servo
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
int sread=0;
void setup() {
pinMode(servoPin1, OUTPUT); // Set servo pin as an output pin
pulse1 = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0)
{
sread=Serial.read();
Serial.println(sread);
pulse1 = map(sread,0,180,minPulse,maxPulse); // convert the analog value
// to a range between minPulse
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime)
{
digitalWrite(servoPin1, HIGH); // Turn the motor on
delayMicroseconds(pulse1); // Length of the pulse sets the motor position
digitalWrite(servoPin1, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
}
Tutto funziona, il servo motore si muove etc. Il mio problema è che mi servirebbe coprire tutta l'asse x , cioè inviare le coordinate da 0 a 1280. Da quanto ho capito attraverso il sistema port.write -> SerialRead posso inviare solo parole da 1 byte e quindi avere un valore massimo di 256. Come posso leggere da arduino per esempio il valore 1000? (sono abbastanza ignorante in materia).
grazie, Filippo