May 12, 2009

Scripting

Acabo de escribir las primeras pruebas para los scripts del lightPlotter. No las he probado, pero se supone que permiten controlar la rotación del eje del servo mediante las teclas A y S.

Esta es la parte de Processing, que envía a Arduino una A si se está pulsando la letra A y una S si se está pulsando la S:
import processing.serial.*;

Serial myPort;

/* --- uncomment this part to check Arduino's serial port's number
(bracketed value, usually in COM8) ----
println(Serial.list());
*/



void setup() {
myPort = new Serial(this, Serial.list()[0], 9600); //Serial constructor
}

void draw() {
if (keyPressed) {
if (key == 'A') {
myPort.write(65);
}
if (key == 'S') {
myPort.write(97);
}
}
}



Y esta es la de Arduino, que escucha lo que le dice Processing sobre la tecla pulsada y gira el eje un número de grados igual al valor "gap" hacia un lado u otro según la que sea:
/*theLightPlotter: simple servo control
------------------
Listens to the serial port and waits for a Processing signal that controls the rotation of a servo
('A' key rotates one position clockwise; 'S' key rotates one position counter-clockwise). */

#include

#undef int
#undef abs
#undef double
#undef float
#undef round

Servo lampServo;
int trigger = 0; // 'trigger' takes the value Processing is emitting
int servoPos = 0; // current absolute position of the servo's axis
int gap = 3; // rotation step

void setup() {
lampServo.attach(9); // servo attached on PIN 9
Serial.begin(9699); // sets serial frequency in bauds
}

void loop() {
trigger = Serial.read();

if (trigger == 'A') {
servoPos = servoPos + gap; // axis' position incremented by gap
lampServo.write(servoPos);
delay(500);
}

if (trigger == 'S') {
if (servoPos >= gap) { // works only if current position equals or is higher than 'gap'
servoPos = servoPos - gap; // axis' position decremented by 'gap'
lampServo.write(servoPos);
delay(500);
}
}
}



El código está adaptado a partir de este tutorial y de las documentaciones de Arduino y de Processing.

No comments:

Post a Comment