Cockpitbuilders.com
Arduino Projects => Sketches - => Topic started by: RayS on June 30, 2017, 05:46:01 PM
Updated the math calculation for the zero-crossing issue. This code is solid.
Note that when I slew the motor left or right, I do it twice per call. The stepper motor is a 720-step, so a 1-degree compass change requires 2 steps.
#include <SoftwareSerial.h>
#include <Stepper.h>
const int stepsPerRevolution = 720; // change this to fit the number of steps per revolution
// for your motor
int Direction = 0; // 0 = CCW, 1 = CW
int sensorVal = 0;
// initialize the stepper library on pins 2 through 5:
Stepper myStepper(stepsPerRevolution, 2,3,4,5);
// X-Plane Stuff
FlightSimFloat aircraftHeading;
int _aircraftHeading = 0;
int compassHeading;
int Slew;
unsigned long previousMillis = 0;
unsigned long interval = 300000;
void setup() {
pinMode(23, INPUT_PULLUP);
pinMode(13, OUTPUT);
// set the speed at 50 rpm:
myStepper.setSpeed(50);
// SET UP X-PLANE REFS
aircraftHeading = XPlaneRef("sim/cockpit/misc/compass_indicated");
//---- Start Seek To Zero ----//
// Test if we're blocking the sensor
sensorVal = digitalRead(23);
if (sensorVal == LOW)
{
Direction = 1;
myStepper.step(80);
}
sensorVal = digitalRead(23);
while (sensorVal == HIGH)
{
Direction = 1;
myStepper.step(1);
sensorVal = digitalRead(23);
}
delay(200);
myStepper.step(195);
delay(1000);
//TEST HARNESS: Manage aircraftHeading
// aircraftHeading = 0;
// compassHeading = 0;
}
void loop() {
FlightSim.update();
_aircraftHeading = (int)aircraftHeading;
// Compensate for zero-crossing
Slew = _aircraftHeading - compassHeading;
if(Slew < -180) {Slew += 360;}
if(Slew > 180) {Slew -= 360;}
// RIGHT TURNS
if(Slew > 0)
{
compassHeading += 1;
myStepper.step(1);
delay(4);
myStepper.step(1);
delay(4);
}
// LEFT TURNS
if(Slew < 0)
{
compassHeading -= 1;
myStepper.step(-1);
delay(4);
myStepper.step(-1);
delay(4);
}
}
void seekToZero()
{
//---- Start Seek To Zero ----//
// Test if we're blocking the sensor
sensorVal = digitalRead(23);
if (sensorVal == LOW)
{
Direction = 1;
myStepper.step(80);
}
sensorVal = digitalRead(23);
while (sensorVal == HIGH)
{
Direction = 1;
myStepper.step(1);
sensorVal = digitalRead(23);
}
delay(5);
myStepper.step(196);
}
:P mind boggling! :D
Ray,
A quick question re compass card. I see you removed bar magnet assembly. Does the brass gear just pull off the rotating disc shaft? Any tips as this seems very fragile. Cant see any other way to remove bar mech.
Thanks
Les
That brass gear is here to stay. She ain't goin' nowhere. :-)
What I did was super-glue a small flat-head machine screw onto the brass gear so the pointy end is facing rearwards.
Then, after gauging where shaft would align on the back plate, I drilled a hole in the back plate where the stepper shaft will fit through. I used heat shrink tubing to link the stepper shaft to the screw on the brass gear. After everything was aligned, I used rubber cement to mount the stepper motor to the rear of the compass housing.
It's a little tricky because it's pretty much a one-way process. Once everything is mounted there's little option to go back in and align or work on the internals.
Thanks,
I gathered that the brass gear is fixed and thought you may have had a trick to
get the screw centered on the the gear as if this is off center then the disc wont rotate freely.
Les