Go Down

Topic: String from serial into an array? (Read 1 time) previous topic - next topic

acrossell

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 :(

Thanks in advance :)

PaulS

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.

liudr

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.

Graynomad

Rob Gray aka the GRAYnomad www.robgray.com

acrossell

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 ;)


Code: [Select]

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 :))

liudr

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.

Msquare

The bit I focused on was
Code: [Select]
array1[i]= Serial.read()
and
Code: [Select]
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: [Select]
analogWrite(pwmPin3,array1[2]
to
Code: [Select]
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  ;) .

Lastly, as you know how to do arrays ... instead of using variables pwmpin1, pwmpin2, use an array
Code: [Select]
int pwnpins[] = { 3, 5, 6, 11, 9 } ;
and then your last code simplifies to
Code: [Select]
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...

liudr

Msquare,

I am hoping the VVVV outputs binary. The OP said the program worked  :)

Msquare

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>  ;)


liudr

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

acrossell

#10
May 10, 2011, 11:58 pm Last Edit: May 11, 2011, 12:15 am by acrossell Reason: 1
Just to a little to share what i've been doing with my arduino, cheers for everyones help :)

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


liudr


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

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?

Go Up