Pages: [1] 2   Go Down
Author Topic: From processing to arduino  (Read 1077 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi today i decided to make an interface between my pc and my arduino uno r3.
The thing is when I am sending only one byte I can use it without problems waldo when I am sending more than one byte like "arduinoPort.write(pos1);" and "arduinoPort.write(pos2);" the values get very unstable like jumping from 90 to 0 or to 130 how could I solve this problem conection?
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Send it as a string using sprintf() and split it back to individual parts with strtok()
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont thing I can do that, it says "the function sprintf(int, int) does not exist".
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
sprintf(int, int)
There is a little more needed than that.
Here is my code using sprintf and strtok. You do NOT need the virtualwire library, just look at how they are used and setup.
http://arduino.cc/forum/index.php/topic,151900.15.html
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
sprintf(int, int)
There is a little more needed than that.
As I understand the original question, pedroply is using Processing.  Processing does not support sprintf().

It really shouldn't be necessary to create an entire string on the sending side anyway.  Just send a delimiter in between the individual values. 
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And how do I split the values into ints on the arduino?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

First you have to think about what you are sending.

If you are sending ASCII strings, then you'll need to use functions like strtok() and atoi().  Hazard's example link above has receiving code similar to what you would need.

If you are sending "byte" values of what the integers in processing represent, then it is quite a bit easier since you don't have to convert from ASCII to decimal.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I am sending bytes.
And thanks i had forgotten to tell you.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3422
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is in the receiving code from the link I gave you.

Code:
// the data to be split "x,y,z,s" => "111,222,333,444"
        x=atoi(strtok(c,",")); // strtok will look at the split point ",", and will return everything before it, so x = 111
        y=atoi(strtok(NULL,",")); // this will continue off from where the first line ended, and will look again for "," and now y = 222
        z=atoi(strtok(NULL,",")); // same here
        s=atoi(strtok(NULL," ")); // this is looking for a space " " again s = 444

//The atoi() function will convert an array of chars {'1','1','1'} into an actual int 111. Here I just combined both functions into one.

added: just read James C4S's post
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I cant get it work:
Code:
#include <Servo.h>
 
Servo myservo;

 

int pos = 0;


 
void setup()
{
  myservo.attach(9);
  Serial.begin(9600);
 
}
 
void loop()
{
           
     if (Serial.available() > 0) {
      pos = atoi(strtok(NULL,",")); //how do i use this??
      myservo.write(pos);
 
  }
 

}
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

//how do i use this??

RTFM
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still the same I am don't know how to use it in my situation.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still the same I am don't know how to use it in my situation.

Typically it takes more than 15 minutes to read and understand stuff like this; It's always a good idea to setup some examples and play around with them until you have a better understanding.

Regardless, you haven't said anything about how the string that is sent over the Serial line is formatted.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In processing it is like this:
Code:
arduinoPort.write("h");
  arduinoPort.write(",");
  arduinoPort.write(pos);
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In processing it is like this:
Code:
arduinoPort.write("h");
  arduinoPort.write(",");
  arduinoPort.write(pos);

Mixing ASCII and binary is rarely a good idea. Pick an encoding scheme and stick to it.
Logged

Pages: [1] 2   Go Up
Jump to: