Voltando a brincar nas madrugadas…
O código para o Arduino:
#includeconst int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
// a maximum of eight servo objects can be createdint posA = 90; // variable to store the servo position
int posB = 90; // variable to store the servo positionvoid setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 9 to the servo object
}void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();if (incomingByte == 'a' && posA <= 170) { digitalWrite(ledPin, HIGH); posA++; delay(2); } if (incomingByte == 'b' && posA >= 10) {
digitalWrite(ledPin, HIGH);
posA--;
delay(2);
}if (incomingByte == 'e' && posB <= 170) { digitalWrite(ledPin, HIGH); posB++; delay(2); } if (incomingByte == 'd' && posB >= 10) {
digitalWrite(ledPin, HIGH);
posB--;
delay(2);}
if (incomingByte == 'f') {
digitalWrite(ledPin, HIGH);
posB=posB;
delay(2);
}if (incomingByte == 'x') {
digitalWrite(ledPin, HIGH);
posA=posB=90;
delay(2);
}if (incomingByte == 'z') {
digitalWrite(ledPin, LOW);
posA=posA;
posB=posB;
delay(2);
}}
myservo1.write(posA);
myservo2.write(posB);
}
O código para o Processing:
import processing.serial.*;Serial port;
void setup() {
size(200, 200);rectMode(RADIUS);
// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);}
void draw() {
if (keyPressed == true) {
if (keyCode == LEFT) {
fill(255,0,0);
port.write('a');
}
if (keyCode == RIGHT) {
fill(0,255,0);
port.write('b');
}if (keyCode == UP) {
fill(0,0,255);
port.write('d');
}
if (keyCode == DOWN) {
fill(0,255,255);
port.write('e');
}
if (keyCode == SHIFT) {
fill(128,128,255);
port.write('x');
}} else {
fill(128,128,128);
port.write('z');
}rect(50, 25, 50, 50);
}
Obviamente dá pra melhorar e muito a qualidade destes dois códigos…