Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
The arduino sketch of the Mapamok + Stepper motor experiment
#include <Streaming.h>
#include <SerialCommand.h>
#define DIR_PIN 10
#define STEP_PIN 11
float speed = 0.0;
float rotation = 0.0;
SerialCommand scmd;
void setup() {
Serial.begin(9600);
Serial << "# Serial port connected at 9600 bauds" << endl;
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
scmd.addCommand("S", cmd_setspeed);
scmd.addCommand("R", cmd_getrotation);
scmd.addCommand("0", cmd_reset);
}
void loop() {
scmd.readSerial();
if(speed < 0.0001) return;
digitalWrite(DIR_PIN, HIGH);
float deg = 0.51;
int steps = abs(deg)*(1.0/0.225);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
rotation += 0.225;
}
}
void assignNextCommandValueTo(int& prop) {
char* arg;
arg = scmd.next();
if(arg != NULL) {
prop = atoi(arg);
}
}
int getNextCommandValueAsInt() {
int value;
assignNextCommandValueTo(value);
return value;
}
void acknowledge() {
Serial << '$' << endl;
}
void cmd_setspeed() {
speed = (float)getNextCommandValueAsInt()/100;
acknowledge();
}
void cmd_getrotation() {
Serial << "@" << (int)rotation % 360 << endl;
acknowledge();
}
void cmd_reset() {
speed = 0;
rotation = 0;
acknowledge();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.