Hello,
I want to sample a movement.
You can sample a circular movement by pressing once the mouse.
It works in one way when i go straight forward but not the if i give a modulo.
The problem is in void draw here
// virtualV2= (virtualV2)%400; // problem Here!
Can you show me the good way to resolve this problem?
// MANAGE ARDUINO && TENNSY
//import processing.serial.*;
//Serial DueSerialNativeUSBport101;
//int v1=0;
//int v1InMainLoop=0;
int actualSec;
int lastSec;
int virtualV2;
boolean bRecording = false;
boolean mouseRecorded;
float currTime;
float nextSamplePeriod = 2.0;
class Sample {
float t, x, y;
Sample( float t, float x, float y ) {
this.t = t; this.x = x; this.y = y;
}
}
class Sampler {
ArrayList<Sample> samples;
float startTime;
float sampleLengthTime;
float period;
int playbackFrame;
Sampler( float newPeriod, float now, float x, float y ) {
samples = new ArrayList<Sample>();
startTime = now;
addSample( now, x, y );
playbackFrame = 0;
period = newPeriod;
}
void addSample( float now, float x, float y ) {
samples.add( new Sample( now - startTime, x, y ) );
sampleLengthTime = now - startTime;
if( sampleLengthTime > period )
bRecording = false;
mouseRecorded = false;
// round up the period to the next power of 2
//period = sampleLengthTime < 1 ? 1 :
// pow( 2, max( 0, ceil( log( sampleLengthTime ) / log(2) ) ) );
}
void draw( float now ) {
if( samples.size() < 2 ) return;
// draw the sample trail
stroke( 255 );
beginShape(LINES);
for( int i=1; i<samples.size(); i++) {
vertex( samples.get(i-1).x, samples.get(i-1).y );
vertex( samples.get(i).x, samples.get(i).y );
}
endShape();
// draw the current sample point
//float st = (now - startTime) % period;
float st = now % period;
if( st > sampleLengthTime ) return;
if( st < samples.get( playbackFrame ).t ) playbackFrame = 0;
while( samples.get( playbackFrame+1).t < st )
playbackFrame = (playbackFrame+1) % (samples.size()-1);
Sample s0 = samples.get( playbackFrame );
Sample s1 = samples.get( playbackFrame+1 );
float t0 = s0.t;
float t1 = s1.t;
float dt = (st - t0) / (t1 - t0);
float x = lerp( s0.x, s1.x, dt );
float y = lerp( s0.y, s1.y, dt );
float X = map (x, 0, 400, 0, TWO_PI);
float Y = map (y, 0, 400, 0, TWO_PI);
noStroke();
fill( 255, 40, 40 );
circle ( 100* cos (X)+400, 100*sin (Y)+400, 20);
}
}
Sampler sampler;
ArrayList<Sampler> samplers;
void setup() {
//** printArray(Serial.list());
//** DueSerialNativeUSBport101 = new Serial(this, Serial.list()[2], 9600);
// Read bytes into a buffer until you get a linefeed (ASCII 10):
//*** DueSerialNativeUSBport101.bufferUntil('\n');
size( 800, 800, P3D );
frameRate( 30 );
samplers = new ArrayList<Sampler>();
}
void draw() {
print( " INTERNAL CLOCK LastSec " ) ; print( " actualSec " ) ; println( actualSec ) ;
if (actualSec!=lastSec){
lastSec=actualSec;
}
actualSec = second()%2; // Values from 0 - 10
//v1InMainLoop= mouseY;// doesn't work
//v1InMainLoop= v1;// // doesn't work
mouseY=virtualV2;
mouseX=virtualV2;
// mouseY= v1;// // work almost but there is one or two bad datas playedback in a loop
//activeSamplingMeasure();
activeSamplingSecond();
samplingMovementPro();
virtualV2+=5;
// virtualV2= (virtualV2)%400; // problem Here!
float x = map (virtualV2, 0, 400, 0, TWO_PI);
float y = map (virtualV2, 0, 400, 0, TWO_PI);
circle ( 100* cos (x)+200, 100*sin (y)+200, 20);
}
void samplingMovementPro() {
currTime = millis() * 0.001; // seconds since app started
background( 0 );
for( int i=0; i<3; i++ ) {
float p = pow( 2, i );
stroke( 64, p == nextSamplePeriod ? 255 : 64, 64 );
rect( (currTime % p) / p * width, i*10, 2, 8 );
}
if( bRecording ) {
samplers.get(samplers.size()-1).addSample( currTime, mouseX, mouseY ); // work almost with mouseY=v1
// samplers.get(samplers.size()-1).addSample( currTime, mouseX, v1InMainLoop ); // doesn't work
//net.phase[net.size()-1]= map (mouseY, 0, 400, 0, TWO_PI);
}
for( Sampler s : samplers )
s.draw( currTime );
}
void mousePressed() {
mouseRecorded = true;
}
void activeSamplingSecond() {
if (actualSec<=0 && actualSec!=lastSec && mouseRecorded == true) {
bRecording = true;
samplers.add( new Sampler( nextSamplePeriod, currTime, mouseX, mouseY ) );// work almost with mouseY=v1
// samplers.get(samplers.size()-1).addSample( currTime, mouseX, v1InMainLoop );// doesn't work
}
}
void activeSamplingMeasure() {
if (actualSec<=0 && actualSec!=lastSec && mouseRecorded == true) {
// if (measure%4<=0 && beatTrigged == true && mouseRecorded == true){
bRecording = true;
samplers.add( new Sampler( nextSamplePeriod, currTime, mouseX, mouseY ) ); // work almost with mouseY=v1
// samplers.get(samplers.size()-1).addSample( currTime, mouseX, v1InMainLoop );// doesn't work
}
}
void keyPressed() {
if( key == '1' )
nextSamplePeriod = 1.0;
else if( key == '2' )
nextSamplePeriod = 2.0;
else if( key == '4' )
nextSamplePeriod = 4.0;
else if( key == '8' )
nextSamplePeriod = 8.0;
else if( key == 'X' ) {
if( samplers.size() > 0 )
samplers.remove( samplers.size() - 1 );
}
}
/*
void serialEvent(Serial DueSerialNativeUSBport101) { // receive 2 datas splited with , and the last is send with println from ARDUINO
// read the serial buffer:
String myString = DueSerialNativeUSBport101.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int values[] = int(split(myString, ','));
if (values.length > 0) {// v1 is the value of my stepperMotor from 0 to 4000
v1 = (int) map (values[0], 0, 4000, 10, 410);
print (" MouseY "); print (mouseY); print (" v1 "); println (v1);
}
}
*/```