Why is this not working ?(stepper motor, and sonar)

The stepper motor should start sipnnig when sensor distance is <10, but it doesn’t, anyone???

#include <AccelStepper.h>
#include <NewPing.h>
 
#define TRIGGER_PIN  13
#define ECHO_PIN     12
#define MAX_DISTANCE 200
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// Define some steppers and the pins the will use
AccelStepper stepper1(5, 8, 9, 10, 11);

int distancia [20];

void setup()
{  
  stepper1.setMaxSpeed(1000.0); 
  stepper1.setAcceleration(1000.0); 

  Serial.begin(115200);
  
  

}
void myDelay(long interval)
{
    long init = millis();
    while ((millis() - init) <= interval)
    {
        stepper1.stop();
    }
}

void scanear()
{   
 int uS = sonar.ping();
   int espaco = uS / US_ROUNDTRIP_CM ;
   
  while(stepper1.distanceToGo() != 0){
  
 
   if(espaco<10 ){
     stepper1.moveTo(20048);
     stepper1.run();
   }//if2 
  
    
  }//while   
 Serial.println(espaco);
  

}

void loop()
{
   
scanear();   
 

}

You aren’t calling stepper1.run() in the right place - it needs to always be called.

I called it, what would be the right place?

  while(stepper1.distanceToGo() != 0){  <<<<<<<<<< relies on .run() being called
  
 
   if(espaco<10 ){
     stepper1.moveTo(20048);
     stepper1.run();  <<<<<<<<<<<<<< won't necessarily get called
   }//if2 
  
    <<<<<<<< move the call here and it will get called everytime.

  }//while

Still doesn’t :expressionless:

Smug:
Still doesn't :expressionless:

May be true but is not very helpful.

Post your latest code !

...R

This program WORKS:

   stepper1.moveTo(4096); 
    stepper1.run();

But when i put conditionals on it, the motor simply doesn’t move.

stepper1.moveTo(4096); 
    stepper1.run();
  if(espaco>20){
     fazer = 1;
    
   }
   if(espaco<20 && espaco !=0 )x++;{
    distancia[x] = espaco;
    posicao_motor1[x] = stepper1.currentPosition();
    fazer = 0; 
    
    }while(fazer =1);

Any idea of what is going on and how to fix?

There are no errors at compling, and variables such as “espaco” are ok.

Translate it to english.
I would measure the input to see if the logic state matches your code. Are you using pullup or pulldown resistors ?

Post a schematic.

Well, i’m 100% sure that the wiring is ok, and i’m using a ultrasonic HC-SR04 sensor, “espaco” = space, that is the ultrasonic measuring.

if wou want the full code :

#include <AccelStepper.h>
#include <NewPing.h>
 
#define TRIGGER_PIN  13
#define ECHO_PIN     12
#define MAX_DISTANCE 200
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// Define some steppers and the pins the will use
AccelStepper stepper1(5, 8, 9, 10, 11);



void setup()
{  
  stepper1.setMaxSpeed(1000.0); 
  stepper1.setAcceleration(1000.0); 

  Serial.begin(115200);
  
 

}



void myDelay(long interval)
{
    long init = millis();
    while ((millis() - init) <= interval)
    {
        stepper1.stop();
    }
    stepper1.run();
}

void scanear()
{   
  
 int uS = sonar.ping();
   int espaco = uS / US_ROUNDTRIP_CM;
   boolean fazer = 1;
   int x = 0;
   int posicao_motor1[20];
  int distancia [20];
   if(espaco>20){
     fazer = 1;
    
   }
   if(espaco<20 && espaco !=0 )x++;{
    distancia[x] = espaco;
    posicao_motor1[x] = stepper1.currentPosition();
    fazer = 0; 
    
    }while(fazer =1);
   

  
   
  



}

void loop()
{
   
scanear();   

}

Why are you starting another Thread about the same code as in this Thread

I recognized the strangely named function void myDelay(long interval)

I will ask the Moderator to merge the two Threads otherwise everyon is just wasting their time.

You posted a couple of lines of code

This program WORKS:

   stepper1.moveTo(4096); 

stepper1.run();

Why don't you post the full working sketch so we can all start from somewhere useful ?

My guess is that you are not using stepper1.run() properly in the code that is causing a problem - though why that should be in a function called myDelay() I have no idea.

...R

Well, i posted the full code up there.

   if(espaco<20 && espaco !=0 )x++;{

Why is there an open curly brace after the ; that marks the end of the body of the if statement?

    }while(fazer =1);

Why is there a while statement after the }? Why are you assigning a value to fazer using a while statement? What are you expecting to do while you are able to assign a value to fazer?

Smug:
Well, i posted the full code up there.

Where exactly? I don't see working code in any of your posts - yet you claim in Reply #6 to have a version with "This program WORKS:"

...R

Reply#6

This program WORKS:

  first code block

But when i put conditionals on it, the motor simply doesn’t move.

second code block in Reply#6]