mardi 13 décembre 2011

processing -> arduino -> servo

But contrôler un servomoteur.

Code processing

import controlP5.*;

import processing.serial.*;
Serial myPort; // The serial port


ControlP5 controlP5;

public int myColorRect = 200;

public int myColorBackground = 100;

//variable global
int slide1;

void setup() {
size(400,400);
frameRate(25);
controlP5 = new ControlP5(this);
controlP5.addSlider("sliderA",63,118,100,100,260,100,14).setId(4);
controlP5.addTextfield("textA",100,290,100,20).setId(5);
println(Serial.list());

String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}

void draw() {
background(myColorBackground);
fill(myColorRect);
rect(0,0,width,100);
myPort.write(slide1);
}


// a slider event will change the value of textfield textA
public void sliderA(int theValue) {
((Textfield)controlP5.controller("textA")).setValue(""+theValue);
}

// for every change in textfield textA, this function will be called
public void textA(String theValue) {

println("### got an event from textA : "+theValue);
slide1=int(theValue);
println("slide 1 : "+slide1);

}

public void controlEvent(ControlEvent theEvent) {
println("got a control event from controller with id "+theEvent.controller().id());
}


Code arduino

#include

Servo myservo;

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
int pos;

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println((int)incomingByte, DEC);
pos=int(incomingByte);
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);

}
}

Et voila ca marche

Il a juste fallut régler la plage de valeur du servo pour ne pas le mettre en buté. La consomation du servo est également à surveiller. Parfois il demande pas mal mais c'est surtout quand il arrive en butée. Généralement et en fonctionnement normal c'est de l'ordre de 0,10 à 0,30A max

Aucun commentaire:

Enregistrer un commentaire