Hello, I am new to Arduino coding and am attempting to create flight sim instruments using a stepper and the data from the simulator being pushed to the com port. I want to pull data from the serial monitor to drive a 28byj-48 stepper motor using the accelstepper library. I have the code figured out to read the serial data, I'm just not sure how to drive the stepper to the current position being read. Any help would be greatly appreciated.
// Include the AccelStepper Library
#include <AccelStepper.h>
//#define HALFSTEP 8
#define motorPin1 2 // IN1 on ULN2003 ==> Blue on 28BYJ-48
#define motorPin2 3 // IN2 on ULN2004 ==> Pink on 28BYJ-48
#define motorPin3 4 // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4 5 // IN4 on ULN2003 ==> Orange on 28BYJ-48
// Define step constant
#define MotorInterfaceType 8
// Creates an instance
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper myStepper(MotorInterfaceType, 2, 4, 3, 5);
int altl, spdl, hdgl, bnkl, pitl, varrl, varel, varil, gfol, yawl;
int alth, spdh, hdgh, bnkh, pith, varrh, vareh, varih, gfoh, yawh;
int alt, hdg, bnk, pit, spd;
double varr, vare, vari, gfo, yaw, spdtemp;
int spdval,pos;
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(1000.0);
myStepper.setAcceleration(100.0);
myStepper.setSpeed(500);
Serial.begin(19200);
}
void loop() {
if (Serial.available() > 0) // there is data in the buffer
{
if (Serial.available() > 21) // wait until it is filled with all the bytes we need. (21 total)
{
if (Serial.read() == 255) //we found the start serialdata[0]
{
altl = Serial.read();//serialdata[1] // read altitute
alth = Serial.read();//serialdata[2]
spdl = Serial.read();//serialdata[3]// read speed
spdh = Serial.read();//serialdata[4]
hdgl = Serial.read();//serialdata[5]// read compass
hdgh = Serial.read();//serialdata[6]
pitl = Serial.read();//serialdata[7]// read pitch
pith = Serial.read();//serialdata[8]
bnkl = Serial.read();//serialdata[9]// read bank
bnkh = Serial.read();//serialdata[10]
varrl = Serial.read();//serialdata[11] // read vario raw
varrh = Serial.read();//serialdata[12]
varel = Serial.read();//serialdata[13]// read vario elec
vareh = Serial.read();//serialdata[14]
varil = Serial.read();//serialdata[15]// read vario integrated
varih = Serial.read();//serialdata[16]
gfol = Serial.read();//serialdata[17]// read Gforce
gfoh = Serial.read();//serialdata[18]
yawl = Serial.read(); //serialdata[19] // read yawstringangle
yawh = Serial.read(); //serialdata[20]
//All is read --> convert the bytes back into proper data
/*
decoded.altitudebaro = b[1] << 8 | b[2];
decoded.speedraw = Math.Round(((b[3] << 8 | b[4]) / 100.0),2);
//decoded.speedias = b[3] << 8 | b[4];
decoded.compass = b[5] << 8 | b[6];
decoded.pitch = (b[7] << 8 | b[8]) - 90; // I don't know how to forward and convert the negative values. Hence the addition of the max negative value.
decoded.bank = (b[9] << 8 | b[10]) - 180;
decoded.varioraw = Math.Round(((b[11] << 8 | b[12]) - 10000.0) / 100.0, 2);
decoded.varioelec = Math.Round(((b[13] << 8 | b[14]) -10000.0) / 100.0, 2);
decoded.varioint = Math.Round(((b[15] << 8 | b[16]) - 10000.0) / 100.0, 2);
decoded.gforce = Math.Round(((b[17] << 8 | b[18]) / 10.0) - 10.0, 1);
decoded.yawstring = (b[19] << 8 | b[20])-50 ;
*/
alt = altl << 8 | alth; //decode altitude (m)
spdtemp = ((spdl << 8 | spdh) / 100); //decode speed (in m/s)
hdg = hdgl << 8 | hdgh; //decode compass (deg)
pit = (pitl << 8 | pith) - 90; //decode pitch (deg)
bnk = (bnkl << 8 | bnkh) - 180; ////decode bank (deg)
varr = ((varrl << 8 | varrh) / 10.0) - 100.0; //decode raw vario (m/s)
vare = ((varel << 8 | vareh) / 10.0) - 100.0; //decode elec vario (m/s)
vari = ((varil << 8 | varih) / 10.0) - 100.0; //decode integrated vario (m/s)
gfo = ((gfol << 8 | gfoh) / 10.0) - 10.0; //decode gforce
yaw = ((yawl << 8 | yawh)) - 50.0; //decode yawstringangle (deg) [-99,99] but more in the range of [-10 , 10]
//m/s to kmph **************************
spd = spdtemp * 3.6;
//***************************************
if (spd>maxspeed) spd=maxspeed;
spdval = map(spd, 0, maxspeed, 0, maxsteps);
if (vari < -5) vari = -4.9;
if (vari > 5) vari = 4.9;
}
}
}
if (myStepper.distanceToGo() == 0)
myStepper.moveTo(vare);
myStepper.run();
}```