real time IR distance measure and stepper moving

Hi Everyone !
This is my first threed here at Arduino Forum .
But this does not mean i am new with arduino .
I am building a X axis slider that has an IR distance sensor in it , and i want to move the the axis back and forward acording to the distance the sensor reads , normaly it will be between 30 - 100 cm .
I am using a Nema 17 and a Polulu driver A4988 to drive the nema and a Sharp sensor to measure the distance .
i managed to set up a code that i think it will get the job done , but have not tested yet because i still have not got the nema and the driver ( i ordered them online) but i want to know your opinion , in how to make the code better .

Please help me :slight_smile:

Distance_control.ino (2.36 KB)

but i want to know your opinion , in how to make the code better .

For one
thing, it
looks like
it was written
by a drunken
sailor.

Use Tools + Auto Format and sober it up.

Second, code needs to be attached only when it exceeds the 9500 character limit. Yours would not have.

//Pinet e Stepperit
int dirpin = 2;
int steppin = 3;

//Matja e distances
#define sensorIR A0
float sensorValue , cm;
void setup ()
{
   pinMode(dirpin, OUTPUT); 
pinMode(steppin, OUTPUT);

 //Matja e afishuar ne serial monitor
  Serial.begin(9600);
}
void loop () 
{
  
  sensorValue=analogRead(sensorIR);
 cm=10650.08*pow(sensorValue, -0.935)-10;
 delay(50);
 Serial.print(" cm");
 Serial.println(cm);

 {
  if(cm<20)
  {
   int i;
  digitalWrite(dirpin, LOW);    
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm<30)
  {
   int i;
  digitalWrite(dirpin, LOW);    
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm<40)
  {
   int i;
  digitalWrite(dirpin, LOW);    
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm>70)
  {
   int i;
  digitalWrite(dirpin, HIGH);
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm>80)
  {
   int i;
  digitalWrite(dirpin, HIGH);
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm>80)
  {
   int i;
  digitalWrite(dirpin, HIGH);
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm>90)
  {
   int i;
  digitalWrite(dirpin, HIGH);
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  if(cm>100)
  {
   int i;
  digitalWrite(dirpin, LOW);    
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }
  
 }
 
}
 {
  if(cm<20)

What is THAT curly brace for?

  delay(100);

Completely f**king useless.

  if(cm<20)
  {
   int i;
  digitalWrite(dirpin, LOW);    
  delay(100);
  for (i = 0; i<4000; i++)       
  {
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);      
  }                              
  }

It seems to me that the number of steps to take should be based on the distance, not hardcoded to 4000 steps. You might have different ideas.

You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.

Enerik:
i managed to set up a code that i think it will get the job done , but have not tested yet

Come back when you have tested it. Maybe it will work and you won't have any question for us.

...R

Hello Paul !
Thank you for your replay .
Do you have any idea in how to fix the part of the code where the distance is translated into steps for the Nema , so that the stepper moves back and forward while maintaining a constan distance from the object this is the idea.
If i put an object infront of the sensor and the distance is <70 cm , the axis will go back until the distance = 70 cm

Do you have any idea in how to fix the part of the code where the distance is translated into steps for the Nema , so that the stepper moves back and forward while maintaining a constan distance from the object this is the idea.

Yes. Some number of steps (only you know (or can determine) how many that is) moves the device 1 centimeter.

You know how far you want to move, based on distance.

Suppose that you define idealDistance, with some value.
Suppose that you name the variable that holds the measured distance actualDistance.
Suppose that it takes 200 steps to move 1 CM.
Then, the required number of steps would be idealDistance - actualDistance * 200.

Create a function to step n times. Call that function with the appropriate number of steps.

Or, better yet, create a function to step n CM (based on how many steps it takes to step 1 CM). Call that function with the appropriate distance, based on the difference between the ideal distance and the actual distance.

Thank you !
As soon as i finish the coding part i will upload it here and you can take a look , if you dont minde.

Hi Pauls ,i managed to put up this code assuming that 1 cm = 200 steps (this can change according to stepper ) , can you please take a look.

Distance_Control.1.ino (1.14 KB)

Please post your code, in code tags.
Don't make people download it.

Sorry AWOL
This is the code

//Pinet e Stepperit
int dirpin = 2;
int steppin = 3;
int idealDistance = 70;


//Matja e distances
#define sensorIR A0
float sensorValue , cm;
void setup ()
{
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);

  //Matja e afishuar ne serial monitor
  Serial.begin(9600);
}
void loop ()
{
  int stepsToMove;
  int actualDistance;
  sensorValue = analogRead(sensorIR);
  actualDistance = 10650.08 * pow(sensorValue, -0.935) - 10;
  delay(50);
  Serial.print(" cm");
  Serial.println(cm);
  stepsToMove = (idealDistance - actualDistance) * 200;
  if (actualDistance < 70)
  {
    int i;
    for (i = 0; i < stepsToMove; i++)
    {
      digitalWrite(dirpin, LOW);
      digitalWrite(steppin, LOW);
      digitalWrite(steppin, HIGH);
      delayMicroseconds(500);
    }
  }

  if (actualDistance > 70)
  {
    int i;
    for (i = 0; i < stepsToMove; i++)
    {
      digitalWrite(dirpin, HIGH);
      digitalWrite(steppin, LOW);
      digitalWrite(steppin, HIGH);
      delayMicroseconds(500);
    }
  }
  if (actualDistance = 70)
  {
    digitalWrite(dirpin, HIGH);
    digitalWrite(steppin, LOW);
    digitalWrite(steppin, LOW);
    delayMicroseconds(500);
  }
}
  int actualDistance;
  sensorValue = analogRead(sensorIR);
  actualDistance = 10650.08 * pow(sensorValue, -0.935) - 10;

I prefer to initialize the variable when I declare it. I prefer to keep scope as small as possible.

 int sensorValue = analogRead(sensorIR);
  int actualDistance = 10650.08 * pow(sensorValue, -0.935) - 10;
 delay(50);

I prefer NOT to waste time uselessly.

 stepsToMove = (idealDistance - actualDistance) * 200;

I prefer NOT to use magic numbers.

#define STEPS_PER_CM 200

  stepsToMove = (idealDistance - actualDistance) * STEPS_PER_CM;

This way, there is one recognizable value to change when you measure the actual number of steps needed to move 1 centimeter.

 if (actualDistance = 70)

I prefer not to use the assignment operator in an if statement.

 if (actualDistance == 70)

Actually, I prefer not to do anything when nothing needs to be done. If the actual distance is the ideal distance, there is nothing to do.

The difference between the actual distance being greater than the ideal distance and the actual distance being less than the ideal distance is the direction of rotation. That you need to step some number of steps (which COULD be 0) does not depend on the direction.

 if (actualDistance < idealDistance)
    digitalWrite(dirpin, LOW);
  else
    digitalWrite(dirpin, HIGH);

  for(int i = 0; i < stepsToMove; i++)
  {
      digitalWrite(steppin, LOW);
      digitalWrite(steppin, HIGH);
      delayMicroseconds(500);
  }

That's way less code than you have, and does exactly the same thing.

Pauls , i thank you for your help , as i mention before this was my first time posting to arduino forum.
According to the code i will test it when i have all my hardware together ready for test , and if have any question i will really appreciate if you could help me .