Arduino + sensors: basic question[urgent]

edited June 2014 in Arduino

Hi, So I basically getting gyroscope and pressure resistive sensor data from an Arduino to processing with "serial" library, which is pretty simple and straightforward. I have no problems in that, but I am getting this data continuously and will use this data to do visuals. How can I use this to to do animation in "draw()"? The way I am doing it makes the animation super super slow for some reason. And my frameRate id also perfect.. I know this is wrong way but I don't the right way.. for example: What should be the right way to do this? Its more of a processing question that a arduino question

AS SOON AS PUT : while (port.available () > 0) { serial = port.readStringUntil('\n'); if (serial != null) { OUTSIDE MY ANIMATION CODE, THE WHOLE ANIMATION GOES DOWN AND SLOW.

void draw()
{
  println(frameRate);

  if (key == 'o') {
    if (!song.isPlaying()) {
      song.play();
    }
  }

  while (port.available () > 0) {
    serial = port.readStringUntil('\n');
    if (serial != null) {
      String[] a = split(serial, ' ');

      FSR1 = Integer.parseInt(a[0]);
      FSR2 = Integer.parseInt(a[1]);
      FSR3 = Integer.parseInt(a[2]);
      FSR4 = Integer.parseInt(a[3]);


      //  println(FSR3 + " " + FSR4);
      float FSRMAP1 = map((float)FSR1, 0, 1000, 0, width);
      float FSRMAP2 = map((float)FSR2, 0, 1000, 0, width);
      float FSRMAP3 = map((float)FSR3, 0, 1000, 0, width);
      float FSRMAP4 = map((float)FSR4, 0, 1000, 0, width);
      //split gyro values
      String[] gyrArrX = split(a[4], ":");
      gyroX = Float.parseFloat(gyrArrX[1]);

      float gyroXMap = map(abs(gyroX), 0, 300, 0, width);
      float gyroYMap = map(abs(gyroY), 0, 300, 0, width);

      String[] gyrArrY = split(a[6], ":");
      gyroY = Float.parseFloat(gyrArrX[1]);

      String[] gyrArrZ = split(a[8], ":");
      gyroZ = Float.parseFloat(gyrArrX[1]);

      println(gyroX + " " + gyroY + " " + gyroZ);


      switch (back)
      {
      case 0:
        //smoke effect
        if (frameCount % 2 == 0 ) image(g, 0, 0, width+1, height+1);
        // get smokey
        blend(g, 1, 1, width-1, height-1, 0, 0, width, height, REPLACE);
        break;
      case 1:
        stroke(0);
        fill(0, 20);
        rect(0, 0, width, height);
        break;
      default:
        background(0);
        break;
      }

      fft.forward(song.mix);
      float push = 0; 
      for (int i = 0; i < freqs.length; i++) {
        push+=fft.getBand(fft.freqToIndex(freqs[i]));
      }

      for (int i = 0; i < balls.length; i++)
      {
        balls[i].draw();
        balls[i].update();
        for (int j = i; j < balls.length; j++)
          checkForCollision(balls[i], balls[j]);   

        balls[i].push(push/100, new PVector(random(0, 2)-1, random(0, 2)-1));
      }
    }
  }
}

Answers

  • The solution I use (I forget what example it came from) is a separate serialEvent() routine, part of the Serial library, that gets called whenever there's a complete line of input. I then set global variables in that routine while draw() uses the variables. Now doing this means that draw() doesn't necessarily get every line, so I'm not sure how to handle it if the occasional missing line occurs.

    Here's my code. sensors[] is the global variable.

    // serialEvent method is run automatically
    // whenever the buffer receives a linefeed byte 
    
    void serialEvent(Serial myPort) { 
      try {
        // read the serial buffer:
        String myString = myPort.readStringUntil(linefeed);
        // if we get any bytes other than the linefeed:
        if (myString != null) 
        {
          // remove the linefeed
          myString = trim(myString);
    
          // split the string at the tabs and convert the sections into integers:
          int mysensors[] = int(split(myString, '\t'));
          count = mysensors.length;
    
          if (count == 5) {               // make sure we don't have a serial dropout
            for (int i=0; i<count; i++)
            {
              // set sensor[i] value for use in draw()
              sensors[i].value = mysensors[i];
              // print out the values we got:
              print(i + ": " + sensors[i].value + "\t");
            }
            println();
          }
        }
      }
      catch(Exception e) {}  //println("got one"); }
    }
    

    I forget what problem was solved by the try/catch business, but it helped.

Sign In or Register to comment.