Servo Control by Processing

Uncategorized — golan @ 1:28 pm

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) ;
}

0 Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2019 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity