Switec X25 stepper motor code questions

Hello, all.
This is my first post in the forum. I discovered Arduino earlier this year after a lucky internet search.

I would really appreciate some assistance here with coding which is giving me major headaches!

I am in the process of interfacing real aircraft gauges with Switec X25 stepper motors for use with Microsoft Flight Simulator 9. My aircraft is a Boeing 727-200.

There is a separate interface program called ‘Link2FS’ which is a true blessing and FSUIPC (which captures ‘numbers’ from the flight simulator program itself) in integrates them into the Link2FS program.

I’ve previously interfaced using 28BYJ48 stepper motors using these applications/programs but do not like them for my purposes; they operate at high temperatures and their accuracy is less than stellar.
Also, their ‘re-set to zero’ at the beginning of the application leaves something to be desired (its never the same twice).

As a result, I’ve discovered automotive gauges (in the Switec X25 range of products and similar stepper motors manufactured by a company called ‘MCR.’ I’m using the MCR MR1109 steppers which have an internal stop and offer 315-degrees of movement.

I’ve been fortunate enough to also discover the Switec X25 library, courtesy of Guy Carpenter.
I am using his library for my project.

After numerous attempts, I’m happy to say that I am close…VERY close. Yet, I’m having an issue in the actual operation of the gauge I’m currently attempting to operate.

In the following code, it compiles correctly to my Arduino Uno (which I’m just using as a testbed for now…down-the-road, I will move to one of the many Arduino Mega 2560s I have).

Here’s my code:

#include "SwitecX25.h"
#define STEPS (315*3)
SwitecX25 motor1(STEPS, 8, 9, 10, 11);

int CodeIn;
int motor1Position;
int motor1Delay = 1100;

void setup() {
 Serial.begin(115200);
 Serial.println("LET'S GO!");
 motor1.zero();

}
void loop() {
 motor1.update();
 if (Serial.available()) {
   CodeIn = getChar();
   if (CodeIn == '#') {POUND();}
 }
}
char getChar()
{
 while (Serial.available() == 0);
 return((char)Serial.read());
}

void POUND() {
 CodeIn = getChar();
 switch(CodeIn) {

   case 'a':
     String motor1Link2FS;
     int motor1Position;
     motor1Link2FS += getChar();
     motor1Link2FS += getChar();
     motor1Link2FS += getChar();
     motor1Position = motor1Link2FS.toInt();
     stepTo(motor1Position);
     motor1Position = map(motor1Position, 0, 100, 0, 250);
 }
}
void stepTo(int targetPosition) {
 do {
   if (motor1Position < targetPosition) {
     clockwise();
     motor1Position ++;
   }
   else if (motor1Position > targetPosition) {
     anticlockwise();
     motor1Position --;
   }
 }
 while (motor1Position != targetPosition);
}
void clockwise() {
 for (int i = 7; i >= 0; i--)
 {
   motor1.setPosition(i);
   motor1.updateBlocking();
 }
}
void anticlockwise() {
 for (int i = 0; i < 8; i++)
 {
   motor1.setPosition(i);
   motor1.updateBlocking();
}
}

It compiles and when activated, resets to ZERO on start-up, which is exactly what I want.
However, when I advance my aircraft’s thrust lever for this particular stepper (motor1), the gauge and needles just vibrate…there is no needle movement to the correct indication.

I know its probably something ridiculously simple and something I’ve definitely overlooked.

I am asking for assistance and feedback on this.

In advance, I thank you for the set of eyes on my project.

Thanks!

Jay
KFLL

Hi, welcome to the forum.

Could you read this : http://forum.arduino.cc/index.php/topic,148850.0.html Number 7 is about showing a sketch. Could you click 'Modify' for your first post and add those code tags ?

Could you give a link to the SwitecX25 library you use ? Your question seems to be how to use the SwitecX25 library.

Hi and thanks for the reply.

https://github.com/clearwater/SwitecX25

I am trying to get the needle on the gauge to move.

The code of that library seems written well, but I don't know if it will work.

Can you try the example that comes with the library ? That would be the first step.

In you sketch you could print more information to the serial monitor. At the least the new position. When for example 'a' is received, a message could be printed that it was received. With this sketch you don't know if even an 'a' was received at all.

This code stops everything, until something is available : while ( Serial.available() == 0 ) ; It is better to let the sketch run, and avoid that it will be stopped without reason.

Could you tell the format of the text that is received. Is there a CarriageReturn or LineFeed at the end ? or both ?

What do you use as driver between the Arduino and the stepper motor ? Is the power supply okay ?

There is neither a Carriage Return nor LineFeed.

The stepper motor operates perfectly from the Arduino Uno using the stepper motor examples; no additional power supply is required.

For what its worth, this code was the original coding successfully used with the ‘old’ stepper.

int CodeIn; // Used for all serial reads
int engOneN1StepperPosition; // Tracks current step count for stepper motor
int engOneN1StepperPin1 = 8; // Blue - 28BYJ48 pin 1
int engOneN1StepperPin2 = 9; // Pink - 28BYJ48 pin 2
int engOneN1StepperPin3 = 10; // Yellow - 28BYJ48 pin 3
int engOneN1StepperPin4 = 11; // Orange - 28BYJ48 pin 4
int engOneN1StepperLookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
int engOneN1StepperDelay = 1100;
float engOneMultiplier = 3.9;

void setup() {
pinMode(engOneN1StepperPin1, OUTPUT);
pinMode(engOneN1StepperPin2, OUTPUT);
pinMode(engOneN1StepperPin3, OUTPUT);
pinMode(engOneN1StepperPin4, OUTPUT);
Serial.begin(115200);

engOneN1StepperPosition = 0; 
stepTo(0);
Serial.println("Gauge reset and configured.");
}
void loop(){
//INPUTS();
if (Serial.available()) {
CodeIn = getChar();
if (CodeIn == '#') {POUND();}
}
}
char getChar()// Get a character from the serial buffer
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());// Thanks Doug
}
void POUND(){ // The first identifier was "#"
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it
case 'a': 

String EngOneN1Link2FS; 
int engOneN1StepperPosition; 

EngOneN1Link2FS += getChar(); //Get three characters for EngOne N1 data from Link2fs
EngOneN1Link2FS += getChar();
EngOneN1Link2FS += getChar();

engOneN1StepperPosition = EngOneN1Link2FS.toInt() *engOneMultiplier; 
stepTo(engOneN1StepperPosition);
engOneN1StepperPosition = map(engOneN1StepperPosition, 0, 100, 0, 250);
}
}

void stepTo(int targetPosition){
do{
if(engOneN1StepperPosition < targetPosition){
clockwise();
engOneN1StepperPosition ++;
}
else if(engOneN1StepperPosition > targetPosition){
anticlockwise();
engOneN1StepperPosition --;
}
} while (engOneN1StepperPosition != targetPosition );
}

void anticlockwise(){
// step motor 1 step anti-clockwise
for(int i = 0; i < 8; i++)
{
engOneN1SetOutput(i);
delayMicroseconds(engOneN1StepperDelay);
}
}

void clockwise(){
// step motor 1 step clockwise
for(int i = 7; i >= 0; i--)
{
engOneN1SetOutput(i);
delayMicroseconds(engOneN1StepperDelay);
}
}

void engOneN1SetOutput(int out)
{
digitalWrite(engOneN1StepperPin1, bitRead(engOneN1StepperLookup[out], 0));
digitalWrite(engOneN1StepperPin2, bitRead(engOneN1StepperLookup[out], 1));
digitalWrite(engOneN1StepperPin3, bitRead(engOneN1StepperLookup[out], 2));
digitalWrite(engOneN1StepperPin4, bitRead(engOneN1StepperLookup[out], 3));
}

Perhaps this might shed some light?

Thanks again!

Sorry, too confusing for me. Did you know that there is a auto-text-formatting in the menu.

Hi, How are you supplying power to the servos and do you have a driver circuit between the arduino and the stepper?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom..... :)

Peter; Yes, I know there's an auto-format.

Tom; The power to the stepper motor is being supplied directly from the Arduino Uno.

The stepper motor works just fine on 5V. The four (4) pins on the stepper are connected directly to the Uno.

Power/circuitry aren't the issues.

They are stepper motors for panel meters and some use 20mA or less. I can't find the specifications for the MCR MR1109, but I assume the current is in that range. Still, it is very strange for most of us to connect a stepper motor directly to Arduino pins.

EAL727Capt, I'm still confused by the sketch. You could try to find the difference between the example and your sketch. Print many more messages to the serial monitor, and try to make the stepper move without using the Serial input. The while-loop that could run forever, as I mentioned in replay #3, that really scares me. And you don't know if even the right text was received, so you must add many more messages, so that you will know what is going on.

Hi,

Do you have flyback diodes fitted, to prevent damage from back EMF from the stepper coils. Have you used a DMM to find the coil pairs and are they in the right sequence when connected to the arduino?

Tom..... :)

Good morning, all.

Tom; Flyback diodes are not a consideration of mine at this time. In numerous forums and discussions I've read prior to posting here, flyback diodes have been discussed at length. Some users favor them; others advise they are not required as the amperage is low enough not to cause concern. I will re-visit this once all has been resolved with the stepper script I need. At this point, I am more interested in the code/script I need to get the needle to mimic what is on the Flight Simulator screen pertaining to this particular gauge I'm trying to mimic.

Peter; The 'while-loop' needs to run forever (as long as there is power being supplied to my aircraft's 'engine') as there are constant, if not small changes with RPM and the applications I use track these changes and 'display' them on the gauges. This particular aircraft instrument/gauge, known as the 'N1,' measures the RPM of the first stage compressor. Down the road, I will be incorporating other engine instruments which measure Fuel Flow, N2 (2nd stage compressor RPM), Exhaust Pressure Ratio and Exhaust Gas Temperature. In all, each engine has five (5) different gauges for a total of 15 gauges just for the engines.

I have tested my SwitecX25 steppers with the stepper motor examples in Arduino and they all work perfectly...doing exactly what they're designed to do. On the 'old' steppers, there is a controller which attaches to pins on the Arduino on one end and to the actual stepper on the other end. With the 2nd code/script, they worked fine with the 'while-loop.'

As it stands right now, when powered-up and activated, the stepper resets to zero so there is current being supplied from the Arduino to the stepper; there is tension on the shaft from the stepper to the needle itself. When the RPM increases/decreases, the stepper attempts to move the needle to reflect the current RPM reading but...only vibrates--like its trying to move the needle. In the 1st code/script, I am not seeing anything which tells Arduino to supply the proper HIGH/LOW sequences to each of the four (4) pins on the stepper.

Thus my frustration. LOL

Thank you again.

In numerous forums and discussions I've read prior to posting here, flyback diodes have been discussed at length. Some users favor them; others advise they are not required as the amperage is low enough not to cause concern.

You always need flyback diodes with any inductive load, any one who says any different is an idiot who knows little about electronics.

I will re-visit this once all has been resolved with the stepper script I need.

No the advice here is deal with it now before your arduino gets damaged, if it is not already too late.

Great. And what will flyback diodes do to help the scripting issue, which was my reason for posting in the first place.

I truly appreciate the advice given thus far but can we please focus on the original topic?

If I fry an Arduino board, I fry an Arduino board. The world will continue to turn and I will purchase a replacement.

I am looking for assistance with a script and nothing else.

Thanks again.

If I fry an Arduino board, I fry an Arduino board. The world will continue to turn and I will purchase a replacement

With this attitude I am not willing to help you.

Goodbye.

Considering you've provided absolutely no guidance to my initial query, I won't be losing any sleep over your lack of 'help.'

But, let it be known, as previously mentioned, I do appreciate the advice.

I came here asking a question from intelligent participants who might know answers to my questions, not to get lectured.

I came here seeking advice and assistance from those willing to provide it, not to be told I have an attitude.

If you are unwilling and/or unable to volunteer assistance, then don't reply.

Because you may do things differently than I, does not make either one of us right or wrong. It just makes us different. Period.

I know this is really old, but I just have to say .... if you've fried your Arduino by not using flyback diodes, that might be why it just vibrates.

So you want to know why your car drives badly and want to fix the steering, then yell at the guy who tries to tell you to remove the nails from your tires, that you'll fix the tires later, you want to fix the steering now.

I'm amused when people come here looking for help, then treat the people trying to help them FOR FREE like jerks.