String from serial into an array?

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

Thanks in advance :slight_smile:

Im currently sending an ascii string from VVVV

From what?

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.

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.

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.

VVVV

WTF?

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

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

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.

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

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

Msquare,

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

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 ... :blush:
<leaves room, slightly embarassed, muttering to himself> :wink:

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

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

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

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

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

Bad URL

This is right:

I'm telling you this thing is strange. :roll_eyes: Have you thought about illuminating the bags with LEDs from below?