public
Created

The arduino sketch of the Mapamok + Stepper motor experiment

  • Download Gist
mapamokStepper.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#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();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.