Servo Control by Processing
Kyle Machulis’ links:
import processing.serial.*;
Serial myPort;
float myAngle;
//-------------------------------------------------
void setup() {
size(300, 300);
smooth();
myAngle = 70;
println(Serial.list());
myPort = new Serial(this, Serial.list()[0]);
}
//-------------------------------------------------
void draw() {
background(200);
float cx = width/2;
float cy = height/2;
float radi = 100;
if (mousePressed) {
myAngle = map(mouseX, 0, width, 0, 180);
myAngle = constrain(myAngle, 0, 180);
}
else {
myAngle = map(noise(millis()/3000.0), 0,1, 0, 180);
}
int servoValue = (int) map(myAngle, 0,180, 0,254);
myPort.write(servoValue);
float ax = cx + radi*cos( radians(180 + myAngle));
float ay = cy + radi*sin( radians(180 + myAngle));
fill(255);
line(cx, cy, ax, ay);
ellipse(cx, cy, 12, 12);
ellipse(ax, ay, 8, 8);
fill(0);
textAlign(CENTER);
String angleString = "Angle = " + (int)myAngle;
text (angleString, cx, cy+50) ;
}
Comments Off on Servo Control by Processing