Ultrasonic sensors & motor

WOULD APPRECIATE HELP ASAP
Hi!
I am in a dire situation since I almost have no time to finish my last project before my exam, and I am stuck with the code. I am using the exact code from this example (Arduino Playground - NewPing Library). I just adjusted a number of sensors that are in the system. What I want is, that when a specific sensor registers movement, then it give a high or low output, or turns on and off an LED (Sort of the same thing). I am trying to make a motorised rail that will move a payload to a certain position when a certain sensor is registering movement

Kind Regards

Picture of the setup: https://drive.google.com/file/d/0B_1I3ISO-b7tMHFEWGVkZ1NfeEk/view?usp=sharing

#include <NewPing.h>
 
#define SONAR_NUM     3 // Number or sensors.
#define MAX_DISTANCE 200 // Max distance in cm.
#define PING_INTERVAL 33 // Milliseconds between pings.

int LEDPin = 10; //LEDPin
unsigned long pingTimer[SONAR_NUM]; // When each pings.
unsigned int cm[SONAR_NUM]; // Store ping distances.
uint8_t currentSensor = 0; // Which sensor is active.
 
NewPing sonar[SONAR_NUM] = { // Sensor object array.
  NewPing(2, 2, MAX_DISTANCE),
  NewPing(3, 3, MAX_DISTANCE),
  NewPing(4, 4, MAX_DISTANCE)
};
 
void setup() {
  int pinMode(LEDPin,OUTPUT);
  Serial.begin(115200);
  pingTimer[0] = millis() + 75; // First ping start in ms.
  for (uint8_t i = 1; i < SONAR_NUM; i++)
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
 
void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;
      if (i == 0 && currentSensor == SONAR_NUM - 1)
        oneSensorCycle(); // Do something with results.
      sonar[currentSensor].timer_stop();
      currentSensor = i;
      cm[currentSensor] = 0;
      sonar[currentSensor].ping_timer(echoCheck);
              if (i == 0 && currentSensor == SONAR_NUM - 1)
        digitalWrite(LEDPin, HIGH);
    }
  }
  // The rest of your code would go here.
}
 
void echoCheck() { // If ping echo, set distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
 
void oneSensorCycle() { // Do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();
}

Show you sketch. Be sure to auto-format it and post it in CODE tags: Click on the </> icon above and paste your sketch into the code box.

I dont quite understand what you mean, i dont see the </> icon

h3rtz:
I dont quite understand what you mean, i dont see the </> icon

Top/left of the editor window

I have now updated the post with the code I have for the sensors. And if it would help here is the code for the motors. (I am using an H-bridge to change the direction)

#define I1 8  // Control pin 1 for motor 1
#define I2 9  // Control pin 2 for motor 1
#define I3 12  // Control pin 1 for motor 2
#define I4 13  // Control pin 2 for motor 2

void setup() {


    pinMode(I1, OUTPUT);
    pinMode(I2, OUTPUT);
    pinMode(I3, OUTPUT);
    pinMode(I4, OUTPUT);
}

void loop() {

    digitalWrite(I1, HIGH); //MOD URET, set nedefra cylinder
    digitalWrite(I2, LOW);  //MOD URET, set nedefra cylinder
    digitalWrite(I3, LOW);  //MOD URET, set nedefra cylinder
    digitalWrite(I4, HIGH); //MOD URET, set nedefra cylinder

    //digitalWrite(I1, LOW);  //MED URET, set nedefra cylinder
    //digitalWrite(I2, HIGH); //MED URET, set nedefra cylinder
    //digitalWrite(I3, HIGH); //MED URET, set nedefra cylinder
    //digitalWrite(I4, LOW);  //MED URET, set nedefra cylinder
    
}

I am trying to make a motorised rail that will move a payload to a certain position when a certain sensor is registering movement

So, what is the problem?

That the way the sensors are defined in the code makes it so I have no idea what to reference the sensors as when I want to make the output interact with the motors then I don't know what to write, since all the sensors are called the same, just with different pins in the sensor array NewPing. And all the examples I could find with ultrasonic sensors that measure and outputs a distance has no leads, on how to do this.

since all the sensors are called the same

They aren't. One is called sonar[0]. One is called sonar[1]. One is called sonar[2]. Different names for each one.

has no leads, on how to do this.

Forget about there being multiple ping sensors for now. How do you want to run the motors if one of the sensors, any one, sees a distance of X?

I want it to move forward or backwards towards the sensor that is being activated so if we say the first sensor is 10 cm from the start position 0 cm, then I want it to move 10cm when the sensor is activated. And to make sure that it has moved that length I was thinking of having another sensor looking at the payload from one end of the rail so it always knows the distance to the payload.

I think you have to be more specific than "being activated". These sensors will produce outputs when they get an echo off any object between a few centimeters and a few meters away.

I would like the action to happen when the sensor detects movement within about 150cm

The ultrasonic sensor is not a motion detector, it is a distance sensor. It would be easy to trigger some behavior when a sensor detects an object 150 or fewer cm away, if that would work. Make sure you ignore distances of zero: that means no object was detected.

That sounds like something I could use because the 150 cm is just a random estimate of the distance to a person being in front of the sensor, so what you are suggestion should work just as well. How could this be done?

for (int i=0; i<3; i++) {
    if (cm[i] > 0 && cm[i] < 150) {
        // Something is within 150 cm of sensor 'i'.  Do something!
    }
}

That will do something for each sensor that has an object within 150 cm. You can make it fancier by keeping track of which sensor has the closest distance and acting on that information. It's not clear yet what you want to do with the zero to three distance measurements.

I will try that, but i dont understand what you mean by "It's not clear yet what you want to do with the zero to three distance measurements"

"It's not clear yet what you want to do with the zero to three distance measurements"

You are making 4 distance measurements, numbered zero to three. What do you want to do with the 4 numbers ? Is one more important than the other and if so, which ?

The code seems to work as intended but I would like to edit the code so I could have separate actions for the different sensors, because right now all of them activate the same action, even when one, two or all of them are seeing an object at the desired length from the sensor. How could this be done?

UKHeliBob:
You are making 4 distance measurements, numbered zero to three. What do you want to do with the 4 numbers ? Is one more important than the other and if so, which ?

They are all equally important and are going to perform the same function. When a sensor is activated I want the motor to run backwards or forward so it moves the payload to the registered sensor and then stops moving until another sensor is activated, and then the payload should move to that sensor instead.

Do you have a motor associated with each sensor ?
Do the sensors point in different directions ?

Please describe what you are doing so that help given is in context.

I is only one motor and the sensors are placed at different positions along the rail