Hello, I've been trying to create a continuous rotation with Stepper library, but no luck. The command (is sent over serial connection - right now I am using Serial Monitor) is in this format: <dir, speed, steps> or dir can be 0 or 1 (direction). So, if I use <1,60,200> I get 200 steps at 60 rpm in 1 direction (ccw or cw, no mather).
I was thinking, why not make continuous rotation until it receives another command over the serial connection? So I use 0 for steps like a code message for this. That means <1,60,0> should make the stepper run continuously at 60 rpm in 1 direction. And when I send the stop message, (steps = 1), like <1,60,1> the stepper should make just one step and then stop. But it doesnt work and dont know why, please help. Thanks in advance.
Code: // libraries#include <Stepper.h>// buffer containing the datachar in - Pastebin.com
or
or bellow.
Thank you!
// libraries
#include <Stepper.h>
// buffer containing the data
char inData[50];
// index used for buffer
byte index;
// set start and end of packet of data sent
#define SOP '<'
#define EOP '>'
// and it's semaphore lights
bool started = false;
bool ended = false;
// led
int led = 13;
// the 3 variables of interest (default values)
int _dir = 0, _speed = 60;
unsigned int _steps = 1;
// the stepper
Stepper myStepper(_steps, 7,8);
// setup
void setup(){
myStepper.setSpeed(_speed);
Serial.begin(9600);
pinMode(led, OUTPUT);
memset(inData, '\0', 50);
}
// loop
void loop() {
readSerialData();
processPacket();
if (_dir == 0 && _speed == 0 && _steps == 0)
{
digitalWrite(led, HIGH);
Serial.println("arduino found");
}
else
{
digitalWrite(led, LOW);
// Print check for values
Serial.print("direction: ");
Serial.print(_dir);
Serial.print(" speed: ");
Serial.print(_speed);
Serial.print(" steps: ");
Serial.println(_steps);
// command the stepper
// continuous rotation (_steps == 0)
if (_steps == 0)
{
myStepper.setSpeed(_speed);
if (_dir == 0)
{
while (_steps != 1)
{
myStepper.step(1);
readSerialData();
processPacket();
resetPacket();
}
}
else
{
while (_steps != 1)
{
myStepper.step(-1);
readSerialData();
processPacket();
resetPacket();
}
}
}
// explicit rotation (_steps == other value than 0)
else {
myStepper.setSpeed(_speed);
if (_dir == 0)
{
myStepper.step(_steps);
delay(500);
}
else
{
myStepper.step(-_steps);
delay(500);
}
}
}
resetPacket();
}
// saving serial data into buffer
void readSerialData()
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if (inChar == EOP)
{
ended = true;
break;
}
else
{
if (index < 49)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
}
// process the packet
void processPacket()
{
// check packet
if (started && ended)
{
// find _dir, _speed and _steps
char * pch;
pch = strtok (inData, ",");
if (pch != NULL)
{
_dir = atoi(pch);
_speed = atoi(strtok(NULL, ","));
_steps = atoi(strtok(NULL, ","));
}
}
}
// reset for the next packet
void resetPacket()
{
started = false;
ended = false;
index = 0;
inData[index] = '\0';
// _steps = 1;
}