Pages: [1]   Go Down
Author Topic: Using an external file to set servo position (Arduino+Processing)  (Read 549 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,
I'm new to this stuff so please bare with me.
Basically I'm trying to get a servo to move to a position indicated on an external file. The idea is to use Processing to read the file, send the position through serial to Arduino, and the Arduino turns the servo.

Here's the Arduino code;
------------------------------------------------------
#include <Servo.h>

Servo myservo;
int fileInput;
int servoPin = 9;

void setup()
{
  Serial.begin(9600);
  myservo.attach(servoPin);
 
}

void loop()
{
  Serial.println(myservo.read());
  while (Serial.available() == 0);
  fileInput = Serial.read();
  myservo.write(fileInput);
}
---------------------------------------------------
And here's the Processing code;

import processing.serial.*;

String[] lastPos;
int[] startVal;

int count=0;

Serial port;

void setup()
{
  size(400,200);
  port = new Serial(this, "COM5", 9600);
  lastPos = loadStrings("lastPos.txt");
  startVal = int(split(lastPos[0], ',')); // convert string to integer
  port.write(startVal[0]);
}

void draw()
{
  background(255);
}
-----------------------------------------------------------------

Any help/pointers on how to do this?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 638
Posts: 50319
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Processing application opens a file. We have no idea what is in that file.

It reads all the records in that file, into an array of Strings.

It splits the first String, at the commas, into an array of Strings, and converts the Strings to ints.

It sends the first one to the Arduino immediately after resetting the Arduino (opening the serial port does that).

The Arduino gets at most one value, which you read and print. Then, you wait for more values to arrive (none ever will) before reading the value and moving the servo.

Now that you know what is wrong, you'll need to decide how to fix the problem(s).
Logged

Pages: [1]   Go Up
Jump to: