I want to make a servo change its angle using my mouse. When I move mouse horizontally the servo should change its angle correspondingly. This is the sketch I wrote, unfortunately the servo doesn't move one bit. HELP.
Processing Code:
import processing.serial.*;
Serial port;
void setup() {
size(256, 150);
println("Available serial ports:");
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
// draw a gradient from black to white
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, height);
}
// write the current X-position of the mouse to the serial port as
// a single byte
port.write(mouseX);
println(mouseX);
}
Arduino Code:
#include <Servo.h>
Servo myservo;
void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the servo:
myservo.attach(10);
}
void loop() {
byte pos;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
pos = Serial.read();
// set the angle of the servo:
myservo.write(map(pos, 0, 255, 0, 180));
}
}