Servo move at mouse click with processing

Hi everyone :slight_smile:

First of all, I’m sorry if this has been asked or is in the wrong place but I couldn’t find quite what I’m looking for yet :stuck_out_tongue:

What I want to do is:

Processing sends value to serial on mouse click
Arduino reads value and moves servo forwards
Processing sends new value to tell Arduino left click has been released
Arduino stops moving servo

So far I have the processing code working fine, but how do I move the servo forward 1 degree at a time until the serial value changes ?

This is my code so far (jut to move when clicked atm, base is one of the servo’s)

#include <Servo.h>

Servo base;
Servo arm;
Servo elbow;

int pos = ( +1);
int SerialValue = 0;

void setup(){

void loop() {
  SerialValue =;
  if (SerialValue == 104){
    base.write(pos +1);

Thanks in advance :slight_smile:

if (SerialValue == 104){

I’ve been programming since the mid 70s, and yet I have no idea what the ASCII character 104 is.
I suppose I could look it up.

Not sure what you expect/want this:
int pos = ( +1); to do.

Maybe pos should move forward too:

base.write(pos +1);

lol 104 is just a random character, not sure why.....

and that other bit was just me experimenting to see what happens, obviously didn't work I just couldn't be bothered to delete it first :P

Before reading anything from the serial port, you should make sure that there is something to read.

Your processing sketch needs to send a value when the mouse is pressed, and another value when the mouse is released.

You should start incrementing the position when the first value is received, and stop incrementing it when the second value is received.

You need to wait some interval after incrementing the position and moving the servo before you do it again.

You need to figure out what you are going to do when the servo hits its upper limit.

Hey, Thanks for the help :)

I've got it working now with

void loop() {
  int pos =;
  SerialValue =;
  if (SerialValue == 104){
    base.write(pos +1);

The problem was in the int pos =; not being in the loop, which is obvious now I think about it ;D

now to do the same with every other button and servo!

That code will move the servo 1 degree every time a 104, whatever the heck that is, comes in the serial port. That is not what you said you wanted to do in the beginning.

The else clause is completely unnecessary. There is no reason to tell the servo to go where it is, on every pass through loop.

Hint: I looked it up, and 104 is a capital D! So:if (SerialValue == 'D'){

You should be able to get some ideas from the sweep code example.


Sorry for the confusion, Even I wasn't entirely sure what I wanted really, but what it does now works for me. The 104 is just the number that processing is chucking at the serial port, which I picked at random - to be honest I don't really understand the serial very much (as you probably guessed ;) ) but this is working now so thanks anyway. And I realised after that I didn't need the else :)