GMU:Tutorials/Performance Platform/Controlling an Arduino with The Captury: Difference between revisions

From Medien Wiki
No edit summary
Line 133: Line 133:


'''The Code Controlling with Captury:'''
'''The Code Controlling with Captury:'''
<source lang="Java" line start= "1">
#include <OSCMessage.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>   
#include <OSCBoards.h>
EthernetUDP Udp;
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
//the Arduino's IP
IPAddress ip(128, 32, 122, 252);
//destination IP
IPAddress outIp(128, 32, 122, 125);
//port numbers
const unsigned int inPort = 1065;
const unsigned int outPort = 1065;
int photocellPin = 0;    // the cell and 10K pulldown are connected to a0
int photocellReading;    // the analog reading from the sensor divider
int x1=0; int y1=0; int z1=0; int x2=0; int y2=0; int z2=0;
int temptime,time1,time2;
float vel;
void setup() {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);
  pinMode(3, OUTPUT);
 
  //setup ethernet part 
  Ethernet.begin(mac,ip);
  Udp.begin(inPort);
  //Recording the original position and running time
  OSCMessage msg;
  x1 = msg.getFloat(0);
  z1 = msg.getFloat(1);
  y1 = msg.getFloat(2);
  time1 = millis();
}
void OscMsg(OSCMessage &msg){
 
/*  //Using SLIPSerial to get serial data
  while(!SLIPSerial.endofPacket())
  {
    int size = SLIPSerial.available();
    if (size > 0)
    {
        //fill the msg with all of the available bytes
        while(size--)
        {
            msg.fill(SLIPSerial.read());
        }
    }
   
  }
  if (msg.isString(0))
  {
    //get that String
    String data = msg.getString(0);
  }*/
 
  //OSCMessages can be routed to a specific function(GetPosition) by matching their address partial
//  msg.route("/blender/Root/vector",GetPosition);
//}*
void GetPosition(OSCMessage &msg);
  //Recording the distance between two points
  float x = msg.getFloat(0);
  float z = msg.getFloat(1);
  float y = msg.getFloat(2);
  x2 = abs(x - x1);
  y2 = abs(y - y1);
  x2 = abs(z - z1);
   
  x1 = x;
  y1 = y;
  z1 = z;
  float distance = sqrt(sq(x2)+sq(y2)+sq(z2));
  //Record the duration time that this bone move from the first point to the second point
  temptime = millis();
  time2 = (temptime - time1)/1000;
  //Calculate the velocity of this bone
  vel = (distance/time2)*100;
  }
 
void loop()
//  photocellReading = analogRead(photocellPin);
  //Giving the velocity to motor
  OscMsg(OSCMessage &msg);
  pphotocellReading = vel;
 
  if(photocellReading < 600){
    digitalWrite(3, HIGH);
    delay(100);
  }
  else if(photocellReading > 600){
    digitalWrite(3, LOW);
    delay(100);
  }
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);    // the raw analog reading
  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0
  //photocellReading = 1023 - photocellReading;
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  //LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
  //analogWrite(LEDpin, LEDbrightness);
  delay(10);
}
</source>
'''TIPS!''' Also, you have to make sure that you used the right port related with the Captury.