Created
June 4, 2013 19:36
-
-
Save anonymous/5708849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment