Hi All,
I have a software question relating to the Motor Shield - Arduino motor/stepper/servo control
Basically, all I want to do is accept the following commands from a terminal program.
$MM1, 50, 100,*
$SM1*
The first command would move stepper motor channel 1 100 steps at a rate of 50 steps per second.
The second command would Stop Motor Channel 1.
I can spin the motor using my $MM1 command, however, after the motor begins to spin and I send a $SM1* command I hear an audible noise like the motor is stopping for a few steps, but it keeps spinning.
Code:
#include <AFMotor.h>
char USARTBuffer[80];
void HitISR(void);
int status;
int BuffIndex;
AF_Stepper motor1(64, 1);
void setup()
{
Serial.begin(115200);
}
void loop()
{
delay(100);
if(!strncmp(USARTBuffer, "MM1", 3)){ProcessMM1();}
ClearUARTArray();
}
void ProcessMM1(void)
{
Serial.println("HIT");
int M1Speed = atoi(subStr(USARTBuffer, ",", 2));
int M1Steps = atoi(subStr(USARTBuffer, ",", 3));
ProcessStepperMotors(M1Speed, M1Steps);
}
void ProcessStepperMotors(int M1Speed, int M1Steps)
{
motor1.setSpeed(M1Speed);
if (M1Steps > 0){motor1.step(M1Steps, FORWARD, SINGLE);}
else {motor1.step(M1Steps, BACKWARD, SINGLE);}
motor1.release();
}
void ClearUARTArray(){for(int i = 0; i < 80; i++){USARTBuffer[i] = ' ';}}
//Interrupt Service Routines
ISR(USART_RX_vect)
{
char data = UDR0;
switch(data)
{
case ('
I've tried modifying the AFMotor.cpp/AFMotor.h library code by adding another argument to:
AF_Stepper::AF_Stepper(uint16_t steps, uint8_t num, bool run)
and changing:
if (run == 1)
{
while (steps--)
{
ret = onestep(dir, style);
delay(uspers/1000); // in ms
steppingcounter += (uspers % 1000);
if (steppingcounter >= 1000)
delay(1);
steppingcounter -= 1000;
}
}
else if (run == 0)
{
steps = 0;
steppingcounter = 0;
}
But the motor still spins when $SM1* is sent.
Any ideas on what I'm doing incorrectly ?
Thanks in advance!!
):
BuffIndex = 0;
status = 1;
break;
case ('*'):
BuffIndex++;
HitISR();
status = 0;
break;
default:
if(status == 0){break;}
USARTBuffer[BuffIndex++] = data;
}
}
void HitISR(void)
{
Serial.println("ISR WAS HIT!");
if(!strncmp(USARTBuffer, "SM1", 3))
{
Serial.println("SM1$ WAS HIT!");
motor1.release();
}
}
//See the following website for more information
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231961642
char* subStr (char* str, char *delim, int index)
{
char *act, *sub, *ptr;
static char copy[80];
int i;
// Since strtok consumes the first arg, make a copy
strcpy(copy, str);
for (i = 1, act = copy; i <= index; i++, act = NULL)
{
//Serial.print(".");
sub = strtok_r(act, delim, &ptr);
if (sub == NULL) break;
}
return sub;
}
I've tried modifying the AFMotor.cpp/AFMotor.h library code by adding another argument to:
§DISCOURSE_HOISTED_CODE_1§
and changing:
§DISCOURSE_HOISTED_CODE_2§
But the motor still spins when $SM1* is sent.
Any ideas on what I'm doing incorrectly ?
Thanks in advance!!