Need help with mouse to servo program.
in
Integration and Hardware
•
1 year ago
I am building a bus simulator cockpit for my computer (for the game 'Omsi'), and want to get the data out of the simulator and relay the gauge positions and dash lights to a replica dashboard i am building.
I have an arduino mega as the interface board, and at the moment i am just learning how to program it, and wanted a simple sketch that would allow me to move a servo with the computers mouse... just to prove i can do it, later on i will work out making the in-game gauge needles move the real ones.
So i found a sketch on the arduino playground, which uses processing to make the box you move the mouse in to move the servo via the arduino.
I've got the arduino sketch working and uploaded, but the processing sketch is not working,
It starts up, detects the com port, connects to the arduino, and the servo starts buzzing as if it's trying to hold a position but dosent have the right data to exactly where that position is, and i get a big black window and that's all,
I think i should have a window with a white box in it, which is where i move the mouse side to side to make the servo follow, but something isnt right... if i press the 'fix encoding and reload' button in tools, it clears the code, and starts up the sketch, and produces a much smaller black box with a white rectangle inside it, but it is not connecting to the com port, so nothing else happens.
my code is:
- /**
* Servocontrol (derived from processing Mouse 1D example.)
*
* Updated 24 November 2007
*/
// Use the included processing code serial library
import processing.serial.*;
int gx = 15;
int gy = 35;
int spos=90;
float leftColor = 0.0;
float rightColor = 0.0;
Serial port; // The serial port
void setup()
{
size(720, 720);
colorMode(RGB, 1.0);
noStroke();
rectMode(CENTER);
frameRate(100);
println(Serial.list()); // List COM-ports
//select second com-port from the list
port = new Serial(this, Serial.list()[0], 19200);
}
void draw()
{
background(0.0);
update(mouseX);
fill(mouseX/4);
rect(150, 320, gx*2, gx*2);
fill(180 - (mouseX/4));
rect(450, 320, gy*2, gy*2);
}
void update(int x)
{
//Calculate servo postion from mouseX
spos= x/4;
//Output the servo position ( from 0 to 180)
port.write("s"+spos);
// Just some graphics
leftColor = -0.002 * x/2 + 0.06;
rightColor = 0.002 * x/2 + 0.06;
gx = x/2;
gy = 100-x/2;
}
Can anyone tell me where i've gone wrong, basically i copied the code and changed the value for the com port from 1 to 0 to get it to work,
i am just getting into the programming lark, so it's all new and confusing to me, i am working my way through the tutorials for both this program and the arduino.
i'm running win7 32 bit,
1