Controlling a servo motor with processing

Basic sketch for comunicate processing and arduino.

THe goal of this basic sketch is to control a servo motor using Processing.

For our final project we need to control a servo motor with Processing. Actually we need to change the rotation of the motor depending if we are in the right or left side of the processing aplication screen.

We prepeared I basic sketch that rotates the motor to the right if we push a button on the right side of the screen, and that rotates it to the left if we push another button on the left side.

We used the serial library of Processing, this Tom Igoe tutorial as reference and also some code from chapter 2 of the book Making things talk, by Tom Igoe

Here is a short video:

Servo motor cotrolled by processing from berio on Vimeo.

Here some pictures of wires:


And finally the code:

1. Arduino Code:

 
/*
Controlling servo motor from Processing.
Arduino Version.

Based on Tom Igoe"s example:
http://itp.nyu.edu/physcomp/Labs/Servo

by Berio Molina
Created 10 May 2008
*/

int servoPin = 9; // Control pin for servo motor
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 1000; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 40; // the time needed in between pulses

int incomingByte = 0;

void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
Serial.begin(9600);

// Set the motor position value to the middle
pulse = (maxPulse-minPulse)/2;
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
}

void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

if(incomingByte == 1){ // 1 = value that is comming from processing
if(pulse pulse -= 1;
}
}
if(incomingByte == 2){ // 2 = value that is comming from processing
if(pulse>minPulse){
pulse += 1;
}
}

// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
}

2. Processing Code:

 
/*
Controlling servo motor from Processing.
Processing Version.

Based on Tom Igoe"s example:
http://itp.nyu.edu/physcomp/Labs/Servo

by Berio Molina
Created 10 May 2008
*/

import processing.serial.*;

Serial myPort;

color colorButtonR = #222222;
color colorButtonL = #222222;

void setup(){
size(400, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
noStroke();
smooth();
}

void draw(){
background(0);
fill(colorButtonR);
rect(width-100, 0, 100, height);
fill(colorButtonL);
rect(0, 0, 100, height);

if(mouseX<100){
cursor(HAND);
if(mousePressed){
colorButtonL = #284c56;
myPort.write(1); // send 1 to the serial port
}else{
colorButtonL = #222222;
}
}else if(mouseX>width-100){
cursor(HAND);
colorButtonR = #284c56;
if(mousePressed){
myPort.write(2); // send 2 to the serial port
}else{
colorButtonR = #222222;
}
}else{
cursor(ARROW);
}
}

Para o proxecto final do curso, precisamos controlar un servo motor a través de Processing. En realidade o que nos fai falta é xirar o motor para un lado ou para outro, dependendo si estamos no lado exquerdo da pantalla da aplicación de Processing ou no lado dereito

Preparamos un boceto básico que xira o motor á dereita si se pulsa un botón que está á dereita da pantalla, e á exquerda si se pulsa un botón que está á dereita.

Empregamos a librería serial de Processing, este tutorial de como controlar servo motores de Tom Igoe tutorial, e o capítulo número 2 do libro Making things talk, tamén de Tom Igoe.

Aquí vos deixo un video do boceto en funcionamento:

Servo motor cotrolled by processing from berio on Vimeo.

Aquí unhas poucas fotos das conexións:


E por último o código:

1. O código que hai que poñer en Arduíno:

 
/*
Controlling servo motor from Processing.
Arduino Version.

Based on Tom Igoe"s example:
http://itp.nyu.edu/physcomp/Labs/Servo

by Berio Molina
Created 10 May 2008
*/

int servoPin = 9; // Control pin for servo motor
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 1000; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 40; // the time needed in between pulses

int incomingByte = 0;

void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
Serial.begin(9600);

// Set the motor position value to the middle
pulse = (maxPulse-minPulse)/2;
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
}

void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

if(incomingByte == 1){ // 1 = value that is comming from processing
if(pulse pulse -= 1;
}
}
if(incomingByte == 2){ // 2 = value that is comming from processing
if(pulse>minPulse){
pulse += 1;
}
}

// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
}

2. O código que hai que poñer en Processing:

 
/*
Controlling servo motor from Processing.
Processing Version.

Based on Tom Igoe"s example:
http://itp.nyu.edu/physcomp/Labs/Servo

by Berio Molina
Created 10 May 2008
*/

import processing.serial.*;

Serial myPort;

color colorButtonR = #222222;
color colorButtonL = #222222;

void setup(){
size(400, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
noStroke();
smooth();
}

void draw(){
background(0);
fill(colorButtonR);
rect(width-100, 0, 100, height);
fill(colorButtonL);
rect(0, 0, 100, height);

if(mouseX<100){
cursor(HAND);
if(mousePressed){
colorButtonL = #284c56;
myPort.write(1); // send 1 to the serial port
}else{
colorButtonL = #222222;
}
}else if(mouseX>width-100){
cursor(HAND);
colorButtonR = #284c56;
if(mousePressed){
myPort.write(2); // send 2 to the serial port
}else{
colorButtonR = #222222;
}
}else{
cursor(ARROW);
}
}