0

I'm currently working on an arduino project at University. Basically what I am trying to do is send a string of data from VVVV to arduino, however I need to parse the data into an array and I have absolutely no idea how to do this!

The string being sent from VVVV is something like this; U,c,R,|,W,H,P and I need each of those values to be written to a specific servo each time, so value 1 need to go to servo 1, and so on.

Here is my code at the moment, I realize its coded pretty badly, and I do intend to make to make it more efficient when I have worked out how to parse the data.

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

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

void setup()
{
 // begin the serial communication
 Serial.begin(9600);

 servo1.attach(2);
 servo2.attach(3);
 servo3.attach(4);
 servo4.attach(5);

}

void loop(){
  while (Serial.available() == 0)  {
    array1[0] = 0;
    array1[1] = 0;
    array1[2] = 0;
    array1[3] = 0;
  }

{


 // check if data has been sent from the computer
 if (Serial.available() >= 4) {
    for ( int i = 0; i < 4; i++)
    array1[i] = Serial.read();


  }


   Serial.print(array1[0]);
   Serial.print(array1[1]);
   Serial.print(array1[2]);
   Serial.print(array1[3]);



   servo1.write(array1[0]);
   servo2.write(array1[1]);
   servo3.write(array1[2]);
   servo4.write(array1[3]);


}
}

Edit - I should probably mention that I'm eventually looking to use 7 servos, but for the moment I'm just using 4 in my circuit. Also, when I upload this patch and enable VVVV, the arduino just disconnects.

3 Answers 3

1

@opc0de This serial issue has cause a lot of confusion. Including to myself. Possibly similar issue here.

I recently dabbled into this. The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not node.js.

I have an Uno and put a capacitor between Reset and Ground.Here's a page with some good info on the subject. Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

0
if (Serial.available() > 3)
{
    for (int i=0; i < 4 ; i++)
    {
        array[i] = Serial.read();
    }
    Serial.flush();
}

Hope it helps !

3
  • Hi, thanks for trying to help, but unfortunately this has not fixed the issue. :( When I enable the VVVV patch (which starts sending data), the arduino just disconnects itself.
    – thatseika
    May 7, 2012 at 22:41
  • You're right, it was a power issue (boy do I feel silly) seems that as I'm only sending a single ascii character at the time I can just have servo1 reading byte1 and so on. I'll post the code when/if I figure it out. Thanks again. :)
    – thatseika
    May 9, 2012 at 15:45
  • I would guess that the servo uses more power then is available from the USB port. So when the first byte is send and the servo turns on, the voltage drops and the micro resets.
    – Jeff
    Aug 11, 2012 at 3:23
0

If you want to assign the value as soon as received, you can do this:

if(Serial.available() >= 3) {

    servo1.write(Serial.read());
    servo2.write(Serial.read());
    servo3.write(Serial.read());
    servo4.write(Serial.read());

    Serial.flush();
}

If you want to have the received values stored on the array to do something more later, then:

if(Serial.available() >= 3) {

    for(i = 0; i < 4; i ++) {

        array[i] = Serial.read();
    }
    Serial.flush();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.