Hello!
My project is making an Alt/Az telescope mount which receives commands from a WinForm C# app. The commands are ok, motors rotate ok. Now I want to implement Manual Control, namely, make the motors microstep X steps to better position the telescope and make also a sort of semi-auto tracking with this.
In my mind the below code should do the following:
- enter "AMOV";
- read direction;
- according to the direction, move motor X microsteps;
- basically at every click, let's say "NORT" in the WinForm app, the AZ_stepper should move X microsteps upwards;
- exit the "AMOV" only if "DMOV" is received inside;
So the code, in loop(), according to the received command, is executed. When I send "AMOV", it goes as far as receiving the direction, starts moving the motor but another direction is not received and the motor is spinning forever. Thus, the Arduino and the motor can be stopped only by unplugging it from the power supply.
Worth mentioning is that I already have a function serialEvent that listens for commands like "AMOV", "SLEW", "HOME", "PARK", OUTSIDE the loop() function. I try to implement another kind of serial listening inside loop(), inside a received command. Thus, I am wondering if it is possible. If not, other suggestions would be welcomed! If yes, help on this situation would be great!
Here is the loop() code:
else if(commandString.equals("AMOV"))
{
ManualMovement = true;
double DegPerStep = 0.01125;
double DegToMove;
//DegToMove = 0.1;//DegPerStep * 5;
int ms = 20;
//Manual Control of the telescope
while(ManualMovement == true)
{
turnLedOn(led2Pin);
if(Serial.available() && ReceivedDirection == false)
{
//char c = (char)Serial.read();
direction = Serial.readString();
//if(c == '\n')
//{
if(direction.length() > 0)
ReceivedDirection = true;
//}
}
if(ReceivedDirection == true)
{
getDirection();
turnLedOff(led2Pin);
AZ_stepper.setMicrostep(32);
ALT_stepper.setMicrostep(32);
if(movement.equals("NORT"))
{
for(int i=0; i<2; i++)
{
ALT_stepper.move(1);
delay(5);
turnLedOn(led2Pin);
}
turnLedOff(led2Pin);
}
else if(movement.equals("SOUT"))
{
ALT_stepper.move(-ms);
delay(5);
turnLedOff(led2Pin);
}
else if(movement.equals("EAST"))
{
AZ_stepper.move(-ms);
delay(5);
}
else if(movement.equals("WEST"))
{
AZ_stepper.move(ms);
delay(5);
}
else if(movement.equals("DMOV"))
{
ManualMovement = false;
ReceivedDirection == false;
}
direction = "";
}
}
}
And here the serialEvent code outside loop():
void serialEvent()
{
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n')
{
stringComplete = true;
}
}
}
Thank you for reading and for helping!