perlin noise movement problem :-)

Hi folks…
I am working on a project with some motors and i want to let them have a kinda smooth random movement created through perlin noise. i already created my desired effect in processing and everything works well. No putting everything together in arduino i am having some problems…
i used the perlin noise from that post: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191768812 (works right, i think)

So here’s the code… everything works well at the “down-counting” part but when pos = 0 it doesn’t count up again. Where is my brain mistake, folks?
Happy for any suggestion…

#include <Servo.h>  // include servo library
#include <math.h>   // include math library

Servo myServo1;     // create servo-object #1
Servo myServo2;     // create servo-object #2

int pos = 180;        // set position to 0

float seed,seed2;
boolean dir;
float x1,y1;

float persistence;
int octaves;


void setup()
{

  myServo1.attach(9);    // attach servo #1 to pin 9
  myServo2.attach(10);   // attach servo #2 to pin 10

  dir = true;
  //persistence affects the degree to which the "finer" noise is seen
  persistence = 1;
  //octaves are the number of "layers" of noise that get computed
  octaves = 3;

  dir = true;

  Serial.begin(9600);

}


void loop()
{
  seed += PI/500;

  x1 = float(millis())/100.0f;
  y1 = 10.0f;

  if(dir == true){
    pos += 2*PerlinNoise2(seed,10.0f,persistence,octaves);
    delay(15);
  }

  if(dir == false){
    pos -= 1*PerlinNoise2(seed,10.0f,persistence,octaves);
    delay(15);  
  }

  if(pos <= 0){
    dir = true;
    //Serial.println("+");
  }

  if(pos >= 180){
    dir = false;
    // Serial.println("-");
  }

  myServo2.write(pos);

  Serial.print(pos);    
  Serial.print("//");
  Serial.println(PerlinNoise2(seed,10.0f,persistence,octaves));
}

best regards
maerzhase

Well it is hard to say because you have not posted all your code therefore we can't compile it and run it to see. Especially the definition of PerlinNoise2() Just some points:- Why check for == to both true and false why not:-

if(dir == true){
    pos += 2*PerlinNoise2(seed,10.0f,persistence,octaves);
    delay(15);
  }

else{
    pos -= 1*PerlinNoise2(seed,10.0f,persistence,octaves);
    delay(15);  
  }

Then why:-

1*PerlinNoise2(seed,10.0f,persistence,octaves);

multiplying by integer 1 is not going to do much.

Put in print statements to see if you are getting into the correct increment / decrement routine and check the value you are incrementing with a print statement, it might be negative.

  seed += PI/500;

Will be the same seed every time so that statement is not worth doing.

thanks so far. i gonna check around.
here the perlin noise stuff if you’re interested to compile:

float Noise2(float x, float y)
{
  long noise;
  noise = x + y * 57;
  noise = (noise << 13) ^ noise;
  return ( 1.0 - ( long(noise * (noise * noise * 15731L + 789221L) + 1376312589L) & 0x7fffffff) / 1073741824.0);
}

float SmoothNoise2(float x, float y)
{
  float corners, sides, center;
  corners = ( Noise2(x-1, y-1)+Noise2(x+1, y-1)+Noise2(x-1, y+1)+Noise2(x+1, y+1) ) / 16;
  sides   = ( Noise2(x-1, y)  +Noise2(x+1, y)  +Noise2(x, y-1)  +Noise2(x, y+1) ) /  8;
  center  =  Noise2(x, y) / 4;
  return (corners + sides + center);
}

float InterpolatedNoise2(float x, float y)
{
  float v1,v2,v3,v4,i1,i2,fractionX,fractionY;
  long longX,longY;

  longX = long(x);
  fractionX = x - longX;

  longY = long(y);
  fractionY = y - longY;

  v1 = SmoothNoise2(longX, longY);
  v2 = SmoothNoise2(longX + 1, longY);
  v3 = SmoothNoise2(longX, longY + 1);
  v4 = SmoothNoise2(longX + 1, longY + 1);

  i1 = Interpolate(v1 , v2 , fractionX);
  i2 = Interpolate(v3 , v4 , fractionX);

  return(Interpolate(i1 , i2 , fractionY));
}

float Interpolate(float a, float b, float x)
{
  //cosine interpolations
  return(CosineInterpolate(a, b, x));
}

float LinearInterpolate(float a, float b, float x)
{
  return(a*(1-x) + b*x);
}

float CosineInterpolate(float a, float b, float x)
{
  float ft = x * 3.1415927;
  float f = (1 - cos(ft)) * .5;

  return(a*(1-f) + b*f);
}

float PerlinNoise2(float x, float y, float persistance, int octaves)
{
  float frequency, amplitude;
  float total = 0.0;

  for (int i = 0; i <= octaves - 1; i++)
  {
    frequency = pow(2,i);
    amplitude = pow(persistence,i);

    total = total + InterpolatedNoise2(x * frequency, y * frequency) * amplitude;
  }

  return(total);
}

maybe u also have another good suggestion how i can realize a smooth random movement. it should be “pumping” like… going up and down…

You got this logic the wrong way round, it should be:-

if(pos <= 0){
    dir = false;
    Serial.println("+");
  }

  if(pos >= 180){
    dir = true;
    Serial.println("-");
  }

The other point is that you noise function only returns values less that one. In the increment mode you multiplying by 2 so the integer variable pos gets changed. However in the decrement mode you only multiply it by one and so the value of pos never changes.

There is little point in getting a noise value like this if all you are going to do is to round it up an increment or decrement pos.
At the very least pos should be a float and then converted into an int for the servo position call.

thanks u where right but anyway i got rid of the noise idea....working with int's it was not the best solution at the end... i worked out another solution now. in a random amount of time i am creating new destinations – while the servo is just getting a value that is approaching to the destination. i kinda got the result i wanted to have.... the servo is working quiet smooth in random directions... if someone is interested let me know...i can upload a commented version of the code when it's final...

best regards, maerzhase

Hopefully, this would help some people out. I am also working on a project with lots of motors that I needed to have some more natural behaviour other than a traditional random or sweep style of movement. I decided to put the SimplexNoise algorithm into a Arduino library. It's available on Github https://github.com/jshaw/SimplexNoise

Cheers,