// include AccelStepper library
#include <AccelStepper.h>
#include <DcsBios.h>
#include <Servo.h>
// DcsBios-related declarations
DcsBios::ProtocolParser parser;
#define STEPS_PER_REVOLUTION 630
#define ZERO_OFFSET 0
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
int currentStepperPosition = 0; // current stepper position (in steps, from 0 to STEPS_PER_REVOLUTION+1)
signed long lastAccelStepperPosition;
void setup()
{
stepper.setMaxSpeed(1500.0);
stepper.setAcceleration(500.0);
stepper.setSpeed(1000);
stepper.runToNewPosition(630); //backwards sweep to find hard stop
stepper.setCurrentPosition(0); //establishes the zero position
{
Serial.begin(250000);
// tell the AccelStepper library that we are at position zero
stepper.setCurrentPosition(0);
lastAccelStepperPosition = 0;
// set stepper acceleration in steps per second per second
// (default is zero)
stepper.setAcceleration(500);
}
}
int targetposLRPM = 0;
int posLRPM = 0;
void loop()
{
// handle DcsBios communication
while (Serial.available()) {
parser.processChar(Serial.read());
}
DcsBios::PollingInput::pollInputs();
}
void onDcsBiosWrite(unsigned int address, unsigned int value) {
//unsigned int LRPM = (value & 0xffff) >> 0;
if (address == 0x10a8) {
int targetposLRPM = map(value, 0, 65535, 0, 600);
{
if(abs(targetposLRPM - posLRPM)> 2){ //if diference is greater than 2 steps.
if(targetposLRPM > posLRPM) {
stepper.move(-1); // move one step to the left.
posLRPM++;
}
if(targetposLRPM < posLRPM){
stepper.move(1); // move one step to the right.
posLRPM--;
}
}
// move stepper motor
stepper.run();
}
}
}Hi,
I'm relatively new to Arduino programming and in the progress of building some instruments for my DCS A-10C simulator.
I have tried using the stepper.h library included in the Arduino with some luck, but can't get 12 steppers to work together. Someone suggested that I should try Accelstepper.
Now I've taken some code from different sources and tried to put them together into a working sketch for the left RPM gauge. But I have the following issues:
1. I can't get the pointer to move fast enough to keep up with the pointer in game.
2. It starts at zero, and if a select a mission where I start in the air with the motors running, I don't get it to go to the desired position.
Can anyone take a look at my sketch and see if there is some obvious errors?
void loop()
{
// handle DcsBios communication
while (Serial.available()) {
parser.processChar(Serial.read());
}
DcsBios::PollingInput::pollInputs();
}
void onDcsBiosWrite(unsigned int address, unsigned int value) {
//unsigned int LRPM = (value & 0xffff) >> 0;
if (address == 0x10a8) {
int targetposLRPM = map(value, 0, 65535, 0, 600);
{
if(abs(targetposLRPM - posLRPM)> 2){ //if diference is greater than 2 steps.
//if(targetposLRPM > posLRPM) {
// stepper.move(-1); // move one step to the left.
// posLRPM++;
// }
//if(targetposLRPM < posLRPM){
// stepper.move(1); // move one step to the right.
// posLRPM--;
// }
stepper.moveTo(targetposLRPM);targetposLRPM
}
// move stepper motor
stepper.run();
}
}
}
void onDcsBiosWrite(unsigned int address, unsigned int value) {
//unsigned int LRPM = (value & 0xffff) >> 0;
if (address == 0x10a8) {
int targetposLRPM = map(value, 0, 65535, 0, 600);
{
if(abs(targetposLRPM - stepper.currentPosition())> 2){ //if diference is greater than 2 steps.
//if(targetposLRPM > posLRPM) {
// stepper.move(-1); // move one step to the left.
// posLRPM++;
// }
//if(targetposLRPM < posLRPM){
// stepper.move(1); // move one step to the right.
// posLRPM--;
// }
stepper.moveTo(targetposLRPM); //set an absolute target
}
// move stepper motor
stepper.run();
}
}
}
// include AccelStepper library#include <AccelStepper.h>#include <DcsBios.h>#include <Servo.h>
// DcsBios-related declarationsDcsBios::ProtocolParser parser;
#define STEPS_PER_REVOLUTION 630#define ZERO_OFFSET 0
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5int currentStepperPosition = 0; // current stepper position (in steps, from 0 to STEPS_PER_REVOLUTION+1)signed long lastAccelStepperPosition;
void setup(){ stepper.setMaxSpeed(1500.0); stepper.setAcceleration(500.0); stepper.runToNewPosition(630); //backwards sweep to find hard stop stepper.setCurrentPosition(0); //establishes the zero position stepper.setSpeed(1000); //set a constant speed. use this only if you use runSpeedToPosition() below
Serial.begin(250000);
// tell the AccelStepper library that we are at position zero //stepper.setCurrentPosition(0); //this line is not necessary, AccelStepper will always start at 0. lastAccelStepperPosition = 0; // set stepper acceleration in steps per second per second // (default is zero) //stepper.setAcceleration(500); //not necessary, you already set that before }
int targetposLRPM = 0;int posLRPM = 0;
void loop(){ // handle DcsBios communication while (Serial.available()) { parser.processChar(Serial.read()); } DcsBios::PollingInput::pollInputs(); //stepper.run(); // move stepper motors (with acceleration) stepper.runSpeedToPosition(); // move stepper motors (constant speed) //more stepper motors, or use the multistepper class //...}
void onDcsBiosWrite(unsigned int address, unsigned int value) { //unsigned int LRPM = (value & 0xffff) >> 0; if (address == 0x10a8) { int targetposLRPM = map(value, 0, 65535, 0, 600);