jeudi 7 février 2013

Controlling the steering servo with a mouse: the Processing code


As the Arduino program is based on Processing, the syntaxe used to write the code will be very similar.


To write the code, I used the examples below, which are matching perfertly what I intented to do.
and

After some changes, here the code that I will use:

import processing.serial.*;

int xpos=90; // set x servo's value to mid point (0-180);
Serial myport; // The serial port we will be using

void setup()
{
size(720, 360); // size of the main windows

myport = new Serial(this, "COM3", 9600); // declare the serial port on COM3 with a speed of 9600 bds)
}

void draw()
//Note that MouseX is referring to the X axis position of the mouse, from 0 on the left to 720 on the right
{
fill(175);
rect(0,0,720,360);
fill(255,0,0); // set the color RED for the next shapes
rect(360, 175, mouseX-360, 50); // draw a rectangle from the middle position to the x axis of the mouse position

update(mouseX);
}

void update(int x)
{
//Calculate servo postion from mouseX
xpos= x/4; // the middle will be 360/4=90

myport.write(xpos+"x"); //sends the position throught the serial port followed by the letter x, by that way we can if we want, control more servos with different letters
println(xpos); // display the value of the position into the processing monitor
delay(25);
}

Aucun commentaire:

Enregistrer un commentaire