Skip to content

Instantly share code, notes, and snippets.

@cyrildiagne
Created April 2, 2013 15:46
Show Gist options
  • Save cyrildiagne/5293252 to your computer and use it in GitHub Desktop.
Save cyrildiagne/5293252 to your computer and use it in GitHub Desktop.
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