Pages: [1]   Go Down
Author Topic: String from serial into an array?  (Read 966 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

Im currently sending an ascii string from VVVV via serial in the format;     

value1,value2,value3,

Id like to split these values into an array so that I can control some transistors via PWM. I have been looking about for a couple of days at various tutorials on constructing arrays and parsing strings but im struggling to put it all together. Could anybody point me in the direction of a working example or a rough pointer as to how I should approach this. My coding experience is sadly lacking smiley-sad

Thanks in advance smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Im currently sending an ascii string from VVVV
From what?

Quote
I have been looking about for a couple of days at various tutorials on constructing arrays and parsing strings
This topic comes up at least once a day on the forum, it seems, so there should be plenty of examples available.

Quote
but im struggling to put it all together. My coding experience is sadly lacking
Sad. Show us what you have so far, and we can explain it and offer guidance for moving forward.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A agree with Paul, this is a typical question frequently asked. Search a bit more or show us some code you have. Please also have an example of the numbers passed from PC and what they should do in arduino.
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
VVVV
WTF?
Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

VVVV is a free graphical programming environment akin to maxMsp but focussed towards the visual side of things.

So I took your advice and hammered away at it a little more and lo and behold I managaged to get what I wanted, Cheers for the kick up the ass smiley-wink


Code:
int pwmPin1 = 3;
int pwmPin2 = 5;
int pwmPin3 = 6;
int pwmPin4 = 11;
int pwmPin5 = 9; //declare all pins for transistors
int ledPin = 13; //declare ledPin to verify board is working

int pin10 = 10; //for simple pwm VVVV patch


char array1[5] = { '0', '0', '0', '0', '0' }; //array for midi variables


void setup() {
 
  pinMode (pwmPin1, OUTPUT);
  pinMode (pwmPin2, OUTPUT);
  pinMode (pwmPin3, OUTPUT); //declare all transistor pins as output
  pinMode (ledPin, OUTPUT);  //declare ledPin as output
  digitalWrite(ledPin, HIGH); //set ledPin on
  Serial.begin(9600); //begin serial
 
  }
 
 
void loop() {
 
  if (Serial.available() >= 5)  {
   for ( int i = 0; i < 5; i++)
   array1[i]= Serial.read() ;
   
   for ( int i = 0; i < 5; i++)
   Serial.println(array1[i]); //print to com port monitor for debugging
   }
 
{
 
int pwmValues[4];
pwmValues[0]=1;
pwmValues[1]=0; 
pwmValues[2]=255; // test array


analogWrite(pwmPin1,array1[0]);
analogWrite(pwmPin2,array1[1]);
analogWrite(pwmPin3,array1[2]);
analogWrite(pwmPin4,array1[3]);
analogWrite(pwmPin5,array1[4]);


}
}

If any of you can see better ways of doing things than I have done please let me know. Ill be building upon this project in the next week or so and appreciate all help suggestions and input smiley)
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks good.

I would replace
int pwmPin1 = 3;

with

#define pwmPin1 3

Notice there is no "=" or ";" in the latter line of code. This saves some memory, since I don't think you will change pwmPin1 from 3 to something in the middle of your program.
Logged


Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1204
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The bit I focused on was
 
Code:
array1[i]= Serial.read()
and
 
Code:
analogWrite(pwmPin3,array1[2]);
which means there is no parsing or conversion of ASCII to number going on. What is happening is that if VVVV is sending "23456" then PWM 1 will start outputting a 19% duty cycle, the PWM 2 a 20% duty cycle .. The ASCII character "2" has the binary value 50, which is put out via analogWrite(), which causes the 50/255 = 19% ....

Is that what you wanted?

The current format allows for a single digit for each channel (you're not looking for any "," as indicated in the first post) but that might be fine. If you're happy with a single digit and "0" is 0% and "5" is 50% and "9" is 90%, then change the
 
Code:
analogWrite(pwmPin3,array1[2]
to
 
Code:
analogWrite(pwmPin3,(array1[2]-48)*255)

If it works for you without above suggestion, then the VVVV program is outputting a true binary byte so that you are getting a correct 0-255 value. But now you know why if you didn't already  smiley-wink .

Lastly, as you know how to do arrays ... instead of using variables pwmpin1, pwmpin2, use an array
 
Code:
int pwnpins[] = { 3, 5, 6, 11, 9 } ;
and then your last code simplifies to
 
Code:
for ( int p=0; p<5; p++) analogWrite( pwmpins[p], array1[p] ) ;
You can do the same improvemnt in the setup() to set the pins to output. (Is there a reason why you do not set pwmins 4 & 5 to OUTPUT ?)

Hope this gave some more inspiration for next weeks work...
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Msquare,

I am hoping the VVVV outputs binary. The OP said the program worked  smiley
Logged


Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1204
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the OP said he sent an ASCII string, and thus we can conclude he does not.  Nothing as great as a solution to a non-existing problem ...  smiley-red
<leaves room, slightly embarassed, muttering to himself>  smiley-wink

Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wish some sample input and expected output are provided to test the code with. smiley
Logged


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

Just to a little to share what i've been doing with my arduino, cheers for everyones help smiley

http://www.youtube.com/watch?v=EKjChvta5Bs&feature=youtu.be

« Last Edit: May 10, 2011, 05:15:49 pm by acrossell » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just to a little to share what i've been doing with my arduino, cheers for everyones help smiley

http://www.youtube.com/watch?v=EKjChvta5Bs&feature=youtu.be


Bad URL

This is right:
http://www.youtube.com/watch?v=EKjChvta5Bs&feature=youtu.be

I'm telling you this thing is strange. smiley-roll Have you thought about illuminating the bags with LEDs from below?
Logged


Pages: [1]   Go Up
Jump to: