stepper motor movement

I have this code, reading two sensor and move with reference with high intensity of light difference of the two sensor

I got it to work, but the movement is not smooth. any suggestion?

keep in mind, I change the setspeed and setaccerelation values as well as played with time delay but non seems to work

#include<AccelStepper.h>
AccelStepper motor1(1,8,9);


//variable initialization
int esV,wsV; //east sensor value esV AND west sensor value wsV
int dwe; //difference west and east value
int pos=400; //movement of tilt step
int delaynum=500;
int delaynum2=100;

#define sensor0 0 //west sensor
#define sensor1 1 //east sensor

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  motor1.setMaxSpeed(3000);
  motor1.setAcceleration(300);

}// end of void setup -------------------------------------------------------

void loop() {
  //read sensor then map west and east to get new values wsV AND esV
  //difference of wsV and esV call the function dweV 
  sensorV();
  dweV();   
  tilt();

  Serial.print("westvalues: ");
  Serial.print(wsV);
  Serial.print(" ");
  Serial.print("east values: ");
  Serial.print(esV);
  Serial.print(" difference west east  ");
  Serial.println(dwe);
  
  delayMicroseconds(delaynum);
  // delay(delaynum);
}//end of void loop ----------------------------------------------------

void sensorV()
{
  wsV=analogRead(sensor0);
  wsV=map(wsV,0,1023,0,255);
  wsV=constrain(wsV,0,255);

  esV=analogRead(sensor1);  
  esV=map(esV,0,1023,0,255);  
  esV=constrain(esV,0,255);

  //delayMicroseconds(delaynum);
  //delay(delaynum);
}//end of sensor reading -----------------------------------------------

//this loop find the difference of sensor west and east
int dweV()
{
  //change of west and east values let call it cwe
  dwe= wsV-esV;   
  return dwe;
}//end of difference west east -----------------------------------------


//this loop will move only specified distance by checking the difference of the two sensor
void tilt()
{ 
  if(dwe>0)
  {
    if(motor1.distanceToGo() ==0)
      motor1.moveTo(pos);
  }
  else
    if(motor1.distanceToGo() ==0)
    {
      motor1.moveTo(-pos);
    }
  motor1.run();
  motor1.stop();
  
 delayMicroseconds(delaynum);
}//end of tilt loop -----------------------------------------------

Can you describe the movement that actually happens in more detail?

Also (minor point) you don't need "return dwe;" because that function isn't intended to return a value and the code that calls it isn't expecting a return value.

...R

it continuously rotating from left to right as I keep change the intensity of light from one to the other as I wanted to. for example when I shine one one it will go right and shine on the other it will change direction but goes very slow with click like stop sound. you know when you hit hammer or clock sound, that is it.

the movement of stepper motor is rotating with click and very slow pace. but if I remove the run stop, the only problems the motor just go all the way to the end of the desire step I set and come back without follow my intensity of light I shine on any of them. it just goes from -vea to +vea and a be a number of step.

second, can you clarify for me, why I don’t need to return the dwe? I can see on serial print I still get the difference even without return like you said but I was sure that why I put it there. other reason is because I call that function dweV() that is why I thought it is necessary to return. if you comment dweV() and have serial print with just dwe YOU don’t see the difference on serial print. it will show 0 0 0 that was another reason why I return dwe.

but anyone can tell me why I don’t know to return plus the other question above

thanks

now I am using another code which doesn’t make noise and here is the code. I commented the serial coz I already have an idea on how the sensor behave even though I left one serial of the difference reading

I don’t know how to attach audio here I would have done so. may be I can email it to you

#include<AccelStepper.h>
AccelStepper motor1(1,8,9);


//variable initialization
int esV,wsV; //east sensor value esV AND west sensor value wsV
int dwe; //difference west and east value
int pos=4000; //movement of tilt step
int delaynum=500;
int delaynum2=100;

#define sensor0 0 //west sensor
#define sensor1 1 //east sensor

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  motor1.setMaxSpeed(300);
  motor1.setAcceleration(50);

}// end of void setup -------------------------------------------------------

void loop() {
  //read sensor then map west and east to get new values wsV AND esV
  //difference of wsV and esV call the function dweV 
  sensorV();
  dweV();   
  tilt();
  //
  //  Serial.print("westvalues: ");
  //  Serial.print(wsV);
  //  Serial.print(" ");
  //  Serial.print("east values: ");
  //  Serial.print(esV);
  //  Serial.print(" difference west east  ");
  Serial.println(dwe);

  delayMicroseconds(delaynum);
  // delay(delaynum);
}//end of void loop ----------------------------------------------------

void sensorV()
{
  wsV=analogRead(sensor0);
  wsV=map(wsV,0,1023,0,255);
  wsV=constrain(wsV,0,60);

  esV=analogRead(sensor1);  
  esV=map(esV,0,1023,0,255);  
  esV=constrain(esV,0,60);

  //delayMicroseconds(delaynum);
  //delay(delaynum);
}//end of sensor reading -----------------------------------------------

//this loop find the difference of sensor west and east
int dweV()
{
  //change of west and east values let call it cwe
  dwe= wsV-esV;   
}//end of difference west east -----------------------------------------



//this loop will move only specified distance by checking the difference of the two sensor
void tilt()
{ 
  if(dwe>20)
  {

    motor1.moveTo(pos);
  }
  else
  {

    motor1.moveTo(-pos);
  }

  motor1.run();


  //delayMicroseconds(delaynum);
}//end of tilt loop -----------------------------------------------
  //change of west and east values let call it cwe
  dwe= wsV-esV;

Then, why don't we? Useless comments don't help. Why is dwe a global variable? There is no point in having a function that does nothing more than assign the result of a subtraction operation to a global variable.

If the difference between the two sensors is greater than 20, go to one position. Otherwise, go to the other position. How is that useful?

Let's not worry for the moment about using a function to set a global variable. If it makes the code easier to follow there is nothing practical wrong with it.

When you want to write a function to return a variable you need to do it like this

int myfunc() {
  int someValue = 0;
 //...
 return someValue;
}

and you call the function with something like this

int myReturnVal;
myReturnVal = myfunc();

In the example the variable someValue is local to the function and disappears when the function returns.

To go back to your stepper noise problem, I can't come up with any suggestions based on what you have said. I suggest you write a short sketch that just moves the motor from a to b and back using the same motor movement code that's in your above sketch. Is it possible the stepper isn't wired up correctly or doesn't have a suitable power supply?

...R

thanks everyone

I will send my final code so yall can see what I was calling and use for what?

many contributor keep saying why I call function and non is useful after that? this is because you don't see the rest of my code. But I appreciate all the insight, all info were well taken and I really appreciated.

thanks again

Robin2
what you decribe about call the function is exactly correct. I had some sort like that. I decided to experiment to do the way I did and result was fine.

what am saying instead of

int jo=call(); in loop

then

int call()
{
int a,b;
a=10;b=20;
int jo=a*b;
return jo;
}

you can just have call() in loop and return whatever you need just like the way I had on my code and it does work just like standard format of calling function

and Paul

//change of west and east values let call it cwe
  dwe= wsV-esV;

this comment and code are just fine for my side because I was working on multiple pieces testing. so when I was doing copy and paste I forgot to change. It is true it doesn't make sense there.

To the best of my knowledge if you define a void function the "return aaa;" doesn't do anything - assuming aaa is a global variable. It is putting the value in the global variable that does the businees "aaa = 12;"

Try creating a local variable and returning it - for example

void myFunc() {
   int rval = 125;
   return rval;
}

my guess is that the value of rval will just disappear.
...R

that is true

first you can't do return on void function