I am having trouble getting the servo to respond to the x co-ordinate of my mouse and am not sure what i am doing wrong.
any help would be greatly appreciated.
here is the code for arduino
#include <Servo.h>
Servo servo;
String inputString = “”;
boolean stringComplete = false;
void setup() {
servo.attach(9);
Serial.begin(9600);
inputString.reserve(200);
}
[/code]
void loop() {
if (stringComplete) {
int servoval = map(stringComplete, 0, 500, 600, 2400);
servo.writeMicroseconds(servoval);
stringComplete = false;
}
else {
servo.writeMicroseconds(1700);
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == ‘\n’) {
stringComplete = true;
}
}
}
[/code]
and here is the code for processing
import processing.serial.*;
Serial myPort;
int centre = 1390;
void setup() {
size(500,500);
String portName = Serial.list()[3];```
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(255, 0, 0);
if (keyPressed) {
if (key == 'c') {
myPort.write(centre);
println("1390");
}
}
if (mousePressed) {
ellipse(mouseX, mouseY, 50, 50);
int servoval = mouseX;
myPort.write(servoval);
println(servoval);
}
}