1

I want to make a servo change its angle using my mouse. When I move mouse horizontally the servo should change its angle correspondingly. This is the sketch I wrote, unfortunately the servo doesn't move one bit. HELP.

Processing Code:

import processing.serial.*;
Serial port;
void setup() {
  size(256, 150);
  println("Available serial ports:");
  // if using Processing 2.1 or later, use Serial.printArray()
  println(Serial.list());
  port = new Serial(this, Serial.list()[1], 9600);
}

void draw() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, height);
  }
  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
  println(mouseX);
}

Arduino Code:

#include <Servo.h>
Servo myservo;
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the servo:
  myservo.attach(10);
}
void loop() {
  byte pos;
  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    pos = Serial.read();
    // set the angle of the servo:
    myservo.write(map(pos, 0, 255, 0, 180));
  }
}
8
  • 1
    Welcome to Arduino:SE. What exactly is your problem here? What do you expect the code to do? What does it actually do? What have you tried to diagnose the problem? Please help us to help you. Oct 25, 2017 at 10:43
  • Hi @sempaiscuba, thanks for replying. I'm trying to operate a servo using Arduino and Processing. what I wanted it to do was, i'd move my mouse horizontally on a canvas and the servo would change the angle accordingly and simultaneously. The problem is the Servo isn't moving at all, the circuitry is fine.
    – Socasx7
    Oct 25, 2017 at 10:50
  • 1
    Arduino side looks fine. This is most likely a Processing problem, not an Arduino problem.
    – Majenko
    Oct 25, 2017 at 11:00
  • @Majenko I Didn't see that coming, I thought Processing was fine. Wait, is it because, mouseX is more of an int than a Byte?
    – Socasx7
    Oct 25, 2017 at 11:04
  • This, println(Byte.valueOf(Integer.toString(mouseX))); brings out an error: Value out of range. Value: 128, Radix 10.
    – Socasx7
    Oct 25, 2017 at 11:07

1 Answer 1

2

When you try and solve a problem you want to focus on just one part at a time. If I have understood what you are doing correctly then you are trying to:

  1. Get a computer to talk to an Arduino over serial
  2. Get an Arduino to position a servo
  3. Get an Arduino to decode information sent by the computer
  4. Get the computer to code the information
  5. Get the computer to draw graphics.

You can try and ice a cake, feed a baby, talk on you mobile and drive a truck all at the same time, it doesn't mean its a good idea. When you crash what was the cause? You haven't got a clue, you were too busy trying to do all that not analyse what is happening at the same time.

So, what I'm trying to say is break it down. Normally with motors the problem is related to power, people assume that the Arduino can provide enough power for a servo, that's not always the case.

I would:

  1. Write a program that sets the servo to 10 hard coded positions.
  2. Write a program that sets the servo to the position manually entered on the command line.
  3. Write a program that draws the gradient, interprets the user entry and writes the command to be sent to the Arduino to the command line.
  4. Write a program that runs on the Arduino and echoes what it receives over the serial line to some "other output mechanism" (Connecting the computer via SoftwareSerial might be a good idea).

And once they are all working as you expect connect them all together and it will all magically work :)

(Sorry if I'm teaching you to suck eggs, if you have already done all this ignore me)

Not the answer you're looking for? Browse other questions tagged or ask your own question.