See, that's what I said about looking at the whole sketch. This definitely won't move. It's about local and global variables. Maybe, you don't know that concept yet.
Some variables need to be declared GLOBALLY, as you need to maintain their values over longer periods of time or read and write them from different locations in your program. All FlightSimFloats (nav1, nav2, adf1 and magHdg) are of this type, as well as the AccelSteppers (stepper1-4). That means, you can read and write them from anywhere within your program.
In contrast, the variables currentPosition, targetPosition, cwSteps and ccwSteps are NOT of this type. You only need them for a brief period of time, when you calculate how much the stepper should move. So, you actually can DECLARE them right in the middle of your loop() function, which makes them LOCAL within this scope. If you try to read or write them in the setup() function or from within stepperHome(), your compiler will report them as unknown there.
Check here for further explanations:
http://ftp.tuwien.ac.at/languages/c/programming-bbrown/c_046.htm
Now, when I posted that first sketch, my idea was that those four local variables are declared and used within the loop() function. That would update them on every loop pass. In your current sketch, currentPosition and targetPosition are declared globally and set only once, when the program starts. Never again. That won't move anything.
Actually, you could do the same with some of the other variables you use as well: hBval, hB2val, hB3val and hB4val, as well as cur_pos, cur_pos2, cur_pos3 and cur_pos4. I'll leave it as an exercise for you.
Another suggestion is to substitute the fixed numbers by declared constants. Makes your code more readable and easier to maintain. And, of course, instead of
Code:
long currentPosition = stepper.currentPosition();
it must be
Code:
long currentPosition = stepper4.currentPosition();
So, here's the modified sketch (and, this time I've compiled it, so at least syntactically it's correct).
Ah, another question: My son just asked "How much did this guy spend on parts for his simulator until now?" Actually, I'd like to know as well.
Jorg
Code:
#include <AccelStepper.h>
const int homeButton = 0;
const int homeButton2 = 1;
const int homeButton3 = 2;
const int homeButton4 = 3;
byte hBval;
byte hB2val;
byte hB3val;
byte hB4val;
AccelStepper stepper(1, 21, 20); //21=Step 20=Dir #31
AccelStepper stepper2(1, 19, 18); //19=Step 18=Dir #32
AccelStepper stepper3(1, 17, 16); //17=Step 16=Dir #33
AccelStepper stepper4(1, 15, 14); //15=Step 14=Dir #34
FlightSimFloat nav1;
FlightSimFloat nav2;
FlightSimFloat adf1;
FlightSimFloat magHdg;
float cur_pos;
float cur_pos2;
float cur_pos3;
float cur_pos4;
const long STEPS_PER_REVOLUTION = 32000;
const float STEPS_PER_DEGREE = ((float) STEPS_PER_REVOLUTION)/360.0;
void setup() {
Serial.begin(38400);
stepper.setMaxSpeed(3000);
stepper.setAcceleration(3000);
stepper2.setMaxSpeed(3000);
stepper2.setAcceleration(3000);
stepper3.setMaxSpeed(3000);
stepper3.setAcceleration(3000);
stepper4.setMaxSpeed(2500);
stepper4.setAcceleration(3000);
nav1 = XPlaneRef("sim/cockpit2/radios/indicators/nav1_relative_bearing_deg");
nav2 = XPlaneRef("sim/cockpit2/radios/indicators/nav2_relative_bearing_deg");
adf1 = XPlaneRef("sim/cockpit2/radios/indicators/adf1_relative_bearing_deg");
magHdg = XPlaneRef("sim/cockpit2/gauges/indicators/heading_electric_deg_mag_pilot");
pinMode(homeButton, INPUT_PULLUP);
pinMode(homeButton2, INPUT_PULLUP);
pinMode(homeButton3, INPUT_PULLUP);
pinMode(homeButton4, INPUT_PULLUP);
stepperHome(); //runs routine to home motors
}
void loop() {
FlightSim.update();
cur_pos = nav1 * STEPS_PER_DEGREE;
stepper.moveTo(cur_pos);
stepper.run(); //ToPosition
cur_pos2 = nav2 * STEPS_PER_DEGREE;
stepper2.moveTo(cur_pos2);
stepper2.run(); //ToPosition
cur_pos3 = adf1 * -STEPS_PER_DEGREE;
stepper3.moveTo(cur_pos3);
stepper3.run(); //ToPosition
long currentPosition = stepper4.currentPosition();
currentPosition = (currentPosition % STEPS_PER_REVOLUTION + STEPS_PER_REVOLUTION) % STEPS_PER_REVOLUTION; // positive modulo
long targetPosition = (long) (magHdg * STEPS_PER_DEGREE);
long cwSteps;
long ccwSteps;
if (targetPosition > currentPosition) {
cwSteps = targetPosition - currentPosition;
ccwSteps = currentPosition - targetPosition + STEPS_PER_REVOLUTION;
} else {
ccwSteps = currentPosition - targetPosition;
cwSteps = targetPosition - currentPosition + STEPS_PER_REVOLUTION;
}
Serial.print("Current position in steps:");
Serial.println(currentPosition);
Serial.print("Target position in degrees: ");
Serial.print(magHdg);
Serial.print(", in steps: ");
Serial.println(targetPosition);
Serial.print("Steps for rotation: clockwise: ");
Serial.print(cwSteps);
Serial.print(", counterclockwise: ");
Serial.println(ccwSteps);
if (cwSteps < ccwSteps) {
Serial.print("Moving clockwise, number of steps: ");
Serial.println(cwSteps);
stepper4.move(cwSteps);
} else {
Serial.print("Moving counterclockwise, number of steps: ");
Serial.println(ccwSteps);
stepper4.move(-ccwSteps);
}
stepper4.run();
}
void stepperHome() { //this routine should run the motor
delay(100);
hBval = digitalRead(homeButton);
while (hBval == LOW)
{
//backwards slowly till it hits the switch and stops
stepper.moveTo(33000);
stepper.run();
hBval = digitalRead(homeButton);
}
stepper.setCurrentPosition(0); //should set motor position to zero and go back to main routine
delay(50);
hB2val = digitalRead(homeButton2);
while (hB2val == LOW)
{
//backwards slowly till it hits the switch and stops
stepper2.moveTo(33000);
stepper2.run();
hB2val = digitalRead(homeButton2);
}
stepper2.setCurrentPosition(0); //should set motor position to zero and go back to main routine
delay(50);
hB3val = digitalRead(homeButton3);
while (hB3val == LOW)
{
//backwards slowly till it hits the switch and stops
stepper3.moveTo(33000);
stepper3.run();
hB3val = digitalRead(homeButton3);
}
stepper3.setCurrentPosition(0); //should set motor position to zero and go back to main routine
delay(50);
hB4val = digitalRead(homeButton4);
while (hB4val == LOW)
{
//backwards slowly till it hits the switch and stops
stepper4.moveTo(33000);
stepper4.run();
hB4val = digitalRead(homeButton4);
}
stepper4.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}