Hello people !
I'm new on the forum and I need now your help after searching for few weeks a solution to my problem... I tried to find a solution by myself, reading some topics everywhere and espacially on the forum, nothing to answer exactly to this...
My final project is to make a weather station, I found all the dedicated code for my equipments. But I would like to display on an indicator the wind direction (like a compass) using a stepper motor (I tried already to use DC motor, unsucessfully..).
I have a wind vane which can send me a wind direction with an accuracy of one degree. The following code is working to have one degree accuracy on my stepper motor :
#include <HalfStepper.h>
#include <math.h>
#define STEPS 200 //number of steps on the motor
HalfStepper stepper(STEPS, 8, 9, 10, 11);
float memory = 0;
float order = 0;
float delta = 0;
//________________________________________________________________________
void setup() {
Serial.begin(9600);
Serial.println("Ready");
}
//________________________________________________________________________
void loop() {
if (Serial.available()>! 0 ) {
float measure=Serial.parseFloat();
delta = measure - memory;
//Check the shorter distance to reach the measure + allow passing NE to NW & NW to NE
if (delta > 0){
if ((abs(delta) >= 180)){order = delta - 360;}
else {order = delta;}
}
else {
if (abs(delta) <= 180) {order = delta;}
else {order = delta+360;}
}
//Debug
Serial.print("order :"),Serial.println(order);
//Keep the measure in memory
memory = measure;
//send order to stepper motor
stepper.step(order*STEPS/180);
}
}
Now I would like a smooth motion when the needle direction on the stepper changes. For that I'm using Accelstepper library. I tried many configuration but still same issues as followed.
It's working for big direction difference, but not for a difference less than 10° .... Sometimes for big changes, some steps are skipped, I don't know why.... And when I reduce the angle difference (order), I can see the needle moving backward before to go forward and conversely... At the time moving my home position (000° - North)...
To resume :
-
angle of 1 degree : 1st order, moving; 2nd order, nothing; 3rd nothing; etc
-
angle more than 10 :
-
ok but ...
-
Sometimes some steps are skipped
-
Sometimes moving backward before to go forward for positiv order // for negative, moving forward before to go backward
In both cases: North position (order 0) is changing because some steps were skipped
My config :
This is the code :
#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::HALF4WIRE,8,9,10,11);
float memory = 0;
float order = 0;
float delta = 0;
//_____________________________________________________________
void setup() {
Serial.begin(9600);
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(500.0);
Serial.println("Ready");}
//_____________________________________________________________
void loop() {
if (Serial.available()>! 0 ) {
float measure=Serial.parseFloat();
delta = measure - memory;
//Check the shorter distance to reach the measure + allow passing NE to NW & NW to NE
if (delta > 0){
if ((abs(delta) >= 180)){order = delta - 360;}
else {order = delta;}
}
else {
if (abs(delta) <= 180) {order = delta;}
else {order = delta+360;}
}
int order2=order/0.9;
Serial.print("order :"),Serial.println(order);
Serial.print("order :"),Serial.println(order2);
//Send order to stepper motor
stepper.moveTo(order2);
stepper.runToPosition();
stepper.setCurrentPosition(0);
memory = measure; //Keep measure in memory
}
}
Thank a lot for your precious help that you can give !