Hello everyone,
Ok so I've been working all day but to no avail.....
I'm attempting to control a servo motor via serial communication (characters sent from a simple Processing sketch)
Since I've never done this before I tried to use the LED example (that detects if the mouse cursor is over the square and if so, send power to the LED) as a starting point.
I assumed that it would be as easy as changing the code to send a pulse to the servo as the serial reads a ASCI character, but I'm getting no movement.
The other problems occur when I attempt to run the Processing sketch - it sometimes disconnects the serial connection leaving error messages etc.
heres the code for the ARDUINO:
int outputPin = 13;
int val;
int minPulse = 1000;
int maxPulse = 2500;
int pulse = 0;
int lastPulse = 0;
int refreshTime = 20;
void setup()
{
pinMode(outputPin, OUTPUT);
pulse = maxPulse;
Serial.begin (9600);
}
void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == 'H') {
pulse = minPulse;
}
if (val == 'L') {
pulse = maxPulse;
}
}
updateServo();
}
void updateServo()
{
if (millis() - lastPulse >= refreshTime) {
digitalWrite(outputPin, HIGH);
delayMicroseconds(pulse);
digitalWrite(outputPin, LOW);
lastPulse = millis();
}
}
heres the code for PROCESSING
import processing.serial.*;
Serial port;
void setup()
{
size (200,200);
noStroke();
frameRate(10);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
}
boolean mouseOverRect()
{
return ((mouseX >= 50) && (mouseX <=150) &&(mouseY >= 50) && (mouseY <= 150));
}
void draw()
{
background(#222222);
if(mouseOverRect())
{
fill(#BBBBB0);
port.write('H');
}
else
{
fill(#666660);
port.write('L');
}
rect(50,50,100,100);
}
If anyone has some insight on Serial Servo control, I would greatly appreciate it.
Cheers
- Nick