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