Pages: [1]   Go Down
Author Topic: vvvv to arduino using serial  (Read 431 times)
0 Members and 1 Guest are viewing this topic.
Wien Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Im having a very hard time trying to get any usable input over the serial port to arduino from vvvv.  I have included 2 my test patch and some code that zoomkat work on for something else that I thought I could modify.  I get some servo movement but usually as soon as I start my connection from vvvv everything on the screen freezes. I only need to get in a few float values to arduino to control some servos and latter positions of some stepper motors.  Please anyone else who may have dealt with this before and has a a solution then Id be very grateful.
Below is the code so far and attached is the v4 patch to go with it.  Its quite specific to VVVV so I hope Im posting in the right place.

Code:
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added

String readString;
#include <Servo.h>
Servo myservoa;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  myservoa.writeMicroseconds(1500); //set initial servo position if desired


  myservoa.attach(9);  //the pin for the servod control
  Serial.println("TESTME"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        //Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
         Serial.print("writing Microseconds: ");
         Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
        }
        else
        {   
         Serial.print("writing Angle: ");
         Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
     readString += c; //makes the string readString
    }
  }
}






* rs232_arduino.v4p (14.08 KB - downloaded 3 times.)
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's probably because you're using the String class. Although it's included in the IDE, it's a very bad class for a microcontroller environment (which don't have a memory management unit) and because the IDEs up to at least 1.0.3 have a bug in the free() function of the C library (think of it being part of the compiler) it produces a memory leak. Almost every sketch that uses the class will produce unexpected behavior sooner or later. So get rid of it. Use standard C character arrays instead.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're not going to get much help from anyone other than zoomkat. S/He is one of the few who recommend using the String object.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12524
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

as soon as I start my connection from vvvv everything on the screen freezes.

I don't know what vvvv is but presumably it's something that runs on the PC. Is it the PC screen that is freezing?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Wien Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

vvvv is a graphical programming environment like puredata or maxmsp
http://vvvv.org/
I got a little further with a new power supply, which seemed to stop the hanging as I guess I was under powering this little servo(micro servo).  Anyway with a simple test program on the arduino I as testing v4 ability to send out us full information over the rs232 and its seems its just not able to send consistent data and therefore I can use it at the moment.  Ill be resorting to using 2 Arduinos and firmata on one and analog to the other for now as I cant fix what ever string sending issues..Could be so easy but its not
Logged

Pages: [1]   Go Up
Jump to: