Ping sensor, stepper motor, ArrayList help needed

Im detecting movement in the range 50-500mm using a Ping sensor. What I would like to do is: when movement is towards the Ping sensor, the stepper motor rotates forward and when moving away, it rotates backwards.
In the code below I tried to store ‘mm’ in a variable ‘oldmm’ so for the next loop I can compare it to mm. Anyway, thats obviously not the way to go about it so I though maybe I need to save ‘mm’ in an array. Dont know anything about arrays, came across ArrayList as a possibility used in Processing, my logic being that I could store a limited number of ‘mm’ in the array list and get the average and compare that to the current ‘mm’. I failed miserably at trying this array technique (see commented code for attempt).
Can you tell me if Im going about this the right way?

#include <AFMotor.h>

int pingPin = 2;
int pingValue = 0;
int oldmm = 0;

//ArrayList shorts = new ArrayList(5);

AF_Stepper motor(200, 2);

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

void loop() {
long duration, mm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

mm = microsecondsToMillimeters(duration);

delay(100);

// shorts.add(mm);
//
// for (int i=0; i<shorts.length, i++){
// shorts*.run();*
// }

  • if((mm>oldmm) && (mm<500)){*

  • motor.setSpeed(100);*

  • motor.step(5, FORWARD, DOUBLE);*

  • }*

  • else if((mm<oldmm) && (mm>50)){*

  • motor.setSpeed(100);*

  • motor.step(5, BACKWARD, DOUBLE);*

  • }*

  • int oldmm = mm;*
    }
    long microsecondsToMillimeters(long microseconds)
    {
    _ return microseconds / 29 / 2 * 10;_
    }

Also keep in mind that the Arduino does not have new or delete operators.

Thanks for replying.
This is for an interactive sculpture. Im making what I guess could be called snow globes with little landscapes inside. But instead of snow im using ferrofluid. The person will interact with the sculpture by moving their hands towards and away from the ping sensor. The ping sensor values control a mechanism including the motors and magnets that move towards the ferrofluid to change the landscape in the globe.
One of the sculptures uses a standard servo and its easy to map the values so that when the person is moving towards the ping, the motor turns precisely in one direction and vice versa.
But another sculpture needs continuous rotation. I figured a stepper motor would be more precise over using a continuous rot servo.

What I tried to do in the previous code was store the value of ‘mm’ in ‘oldmm’ so that on the next loop I could compare and do something with the difference. It didnt work in that code and I thought I had to do something with arrays o get this to work. But I kept at the code and now it seems to work.
When I move my hand toward the ping sensor it know the distance I have moved and steps the motor that distance forward or backward depending on the movement.

I have no idea how I got this to work but it works.

// Controlling stepper motor with ping sensor and within a certain range
// This code is created based on the example from Motor Shield website
// http://www.ladyada.net/make/mshield/

#include <AFMotor.h>

int pingPin = 2;
int pingValue = 0;
int oldmm = 0;


// 1st argument: steps. e.g. 1.8 degree step motor -> 360/1.8 = 200 steps.
// 2nd argument: port number. Motor connected to M1 and M2 uses 1. M3 and M4 goes to 2.
AF_Stepper motor(200, 2);


void setup() {
  Serial.begin(9600);
  motor.release();
  int oldmm = 0;

}

void loop() {
  long duration, mm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  mm = microsecondsToMillimeters(duration); 

  delay(100);



  if((mm<500)&&(mm>50)){

    if(oldmm>mm){
      int mmMinus = oldmm - mm;
      motor.setSpeed(100);
      motor.step(mmMinus, FORWARD, DOUBLE);
    }

    else if(oldmm<mm){
      int mmPlus = mm - oldmm;
      motor.setSpeed(100);
      motor.step(mmPlus, BACKWARD, DOUBLE);
    }
  }
  Serial.print("oldmm");
  Serial.print(oldmm);
  Serial.println();

  Serial.print("mm");
  Serial.print(mm);
  Serial.println();

  if(mm<500){
    oldmm = mm;
  }
}

long microsecondsToMillimeters(long microseconds)
{
  return microseconds / 29 / 2 * 10;
}

I have no idea how I got this to work but it works.

Maybe by writing some correct code? ;D