Proximity Sensor controlling Stepper?

Conundrum - Help needed (for a gallery installation). I am an enthusiastic coding novice. I have tried for over a week to get a stepper motor to turn one direction (+1/2 revolution) then reverse (-1/2 revolution from zero) based on 2 IR distance sensor input. Basically I have fixed an arrow to the top of a stepper motor and want it to point in the direction where an object is closest to one of the sensors (facing left and right). Say if an object was 50cm away from sensor one and the other sensor detected nothing or a distance further away than that, the motor would turn that direction. I have tried several convoluted codes but can anyone help me out? I have an EasyDriver motor controller, and a simple Stepper + 2 IR Sharp sensors. (I hope I'm explaining myself - apologies if not)...

Are you able to read distances with the IR distance sensors?

Get one sensor working.. declare any variables you think you might need for a sensor. In setup(), do anything you need to start things going. Remember... just one sensor. Also in setup, start your Serial, so you can see what's happening.

In loop() ping the sensor, then use the techniques from the Arduino example sketch Blink Without Delay to delay half a second or so, then ping again, and so on. Right after the ping, use Serial.println() to send the value to the Serial Monitor on the PC.

Got it all working? Good. Do the same with the other sensor.

Now that they are both working, attach a servo, and get that working. To test, use the Serial Monitor to send characters, and Serial.available() and Serial.read90, to get them in the Arduino. Check for a character 'r' or 'l'. When you get one of those, and within an if (character == 'r') block, tell the servo to move clockwise, and within the if (character == 'l') block, tell it to move counterclockwise. If you guess wrong, just reverse the 'r' and 'l' in the if blocks.

OK... now comes the fun part.. In your ping code for each sensor, save the value received in a variable, one variable for Left, and one for Right, (or CW and CCW). Now you can get rid of the Serial.available() and Serial.read(), and replace the if ('l') and if ('r') with this...

Write a statement if (left > right), and in that block, is you code to move the servo right. Then make another statement if (right > left), and in that block is your code to make the servo move left. If your servo moves the wrong way, swap the if statement conditions.

Place a delay (use millis ) in loop(), so that the servo has a chance to get where you told it to go, and you're pretty much done.

Thanks for you help.
Sadly I cannot use servos instead as the designed 3D Printed parts are specifically made for small bi-polar stepper motors, and the circuitry already fixed for easy drivers.
This is the code I was using for 2 steppers which while according to arduino was error free, wouldn’t get either motor to move:

#include <DistanceGP2Y0A21YK.h>

DistanceGP2Y0A21YK Dist;
DistanceGP2Y0A21YK Dist2;
int comparison;

int distanceM34;

int distanceIR1;
int distanceIR2;

#define DIR_PIN 4
#define STEP_PIN 6

#define DIR_PIN2 3
#define STEP_PIN2 5

void setup() { 
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT); 
  pinMode(DIR_PIN2, OUTPUT); 
  pinMode(STEP_PIN2, OUTPUT); 
  Serial.begin(9600);
  Dist.begin(A0); //We are using analogue pin 0 for one IR sensor
  Dist2.begin(A1); //We are using analogue pin 1 for one IR sensor
  
  distanceM34 = 0;
} 

void loop(){ 
  comparison = distanceIR1 - distanceIR2;
  
  if(comparison > (distanceIR1 / distanceIR2) * 0.25){
    rotateM34(distanceM34 * -1, 0.1);  
  }
    
  if(distanceIR1 > 10 && distanceIR1 < 70){
    //rotate a specific number of degrees
    rotateM34((1600*3), 0.1);
    distanceM34 = distanceM34 + (1600*3);
  }
  
}

void rotateM2(int steps, float speed){ 
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);

  digitalWrite(DIR_PIN,dir); 

  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH);   
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

void rotateM34(int steps, float speed){ 
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);
  digitalWrite(DIR_PIN2,dir); 

  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN2, HIGH);  
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN2, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

SENSORS = Sharp Infra-red Distance Sensor - GP2Y0A02YK
(link: http://sharp-world.com/products/device/ … 0a02_e.pdf )

There are no obvious errors in the code when compiled, but the motors don’t seem to be doing anything.
Actually what would be better to simplify things would be to just get one motor turning left and right (a maximum no. of revolutions) according to IR sensors.

You might want to add some serial.print statements to tell you what these variables are:

  comparison = distanceIR1 - distanceIR2;

I can't see anything that sets those distance variables to anything, so I'd expect comparison to be zero all the time. Are you missing a call to the library to get values for them?

Hi,
Divide and conquer. You have a whole lot of complex code there!

Get the motors working alone first,

Get the sensors working and sending results to the Serial Monitor.

THEN write the logic that implements your idea.

Some suggestions for merging multiple working sketches are on the ArduinoInfo.Info WIKI HERE:

The approach of “StepWise Refinement” is an old Computer Science idea from Nicholas Wirth, who created PASCAL.

hi guys, there is going to solve this problem I want to fill the bottle with tape system. system as follows; 1) turns the tape engine 2) And Sharp sensor detects the bottle 3) begins to fill up water 4) was filled with water and the belt moves !! ^^ However, the sensor detects the bottle again and the water begins to fill again. ^^ Here is my problem. Tape engine sensors help make passive relay appointment of returning after a certain time after the band returned again with the help of the motor you want to make active sensors relay.

I wish I did not like dealing with for days. If you throw confident codes, user can modify the code.