Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* setup to control a stepper motor with a A4988 polulu driver and Serial controlled
- Made by Luc Francoeur (push28@gmail.com). As usual feel free to contact me if you
- have any question
- direction step precision controller reset step count
- F = forward F = full e= enabled R=reset data[4,5,6,7]
- R = reverse H= half d= disabled N=null
- Q= quarter S= sleep
- E= eight
- S= sixteen
- 1.8 degree per step at full / 200steps
- 0,9 degree per step at half / 400steps
- 0,45 degree per step at quarter / 800steps
- 0,225 degree per step at eight / 1600steps
- 0,1025degree per step at sixteenth / 3200steps
- */
- #include <Wire.h>;
- char ds[8];
- char buffer[25];
- const int stepPin = 2;
- const int dirPin = 3;
- const int ms1 = 11;
- const int ms2 = 12;
- const int ms3 = 13;
- const int enable = A1;
- const int reset = A2;
- const int sleep = A3;
- int stepcount = 0;
- int var = 0;
- void setup() {
- pinMode(stepPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- pinMode(ms1, OUTPUT);
- pinMode(ms2, OUTPUT);
- pinMode(ms3, OUTPUT);
- pinMode(enable, OUTPUT);
- pinMode(reset, OUTPUT);
- digitalWrite(reset,LOW);
- digitalWrite(enable,HIGH);
- digitalWrite(sleep,LOW);
- Serial.begin(19200); // Speed of serial port
- Serial.flush();
- Wire.begin(0x02); // join i2c bus (address optional for master)
- Wire.onRequest(requestEvent); // register event
- }
- void loop() {
- while (var < stepcount) {
- digitalWrite(stepPin, HIGH);
- delay(3);
- digitalWrite(stepPin, LOW);
- var++;
- }
- if (Serial.available() > 0) {
- byte index = 0;
- delay(100); // let the buffer fill up
- byte numChar = Serial.available();
- if (numChar > 20) {
- numChar = 20;
- }
- while (numChar--) {
- buffer[index++] = Serial.read();
- }
- splitString(buffer);
- }
- }
- void splitString(char* data) {
- char * parameter;
- parameter = strtok (data, " ,");
- while (parameter != NULL) {
- setStepper(parameter);
- calcStepper(parameter);
- parameter = strtok (NULL, " ,");
- }// Clear the text and serial buffers
- for (byte x = 0; x < 20; x++) {
- buffer[x] = '\0';
- }
- Serial.flush();
- }
- void setStepper(char *data) {
- ds[0] = data[0];
- ds[1] = data[1];
- ds[2] = data[2];
- ds[3] = data[3];
- ds[4] = data[4];
- ds[5] = data[5];
- ds[6] = data[6];
- ds[7] = data[7];
- if ((data[0] == 'F') || (data[0] == 'f')) { //clockwise rotation
- digitalWrite(dirPin, LOW);
- Serial.flush();
- Serial.print("clockwise rotation, ");
- }
- if ((data[0] == 'R') || (data[0] == 'r')) { //counter-clockwise rotation
- digitalWrite(dirPin, HIGH);
- Serial.print("counter rotation, ");
- }
- if ((data[1] == 'F') || (data[1] == 'f')) { //step precision Full
- digitalWrite(ms1, LOW);
- digitalWrite(ms2, LOW);
- digitalWrite(ms3, LOW);
- Serial.print("full-step, ");
- }
- if ((data[1] == 'H') || (data[1] == 'h')) { //step precision Half
- digitalWrite(ms1, HIGH);
- digitalWrite(ms2, LOW);
- digitalWrite(ms3, LOW);
- Serial.print("half-step, ");
- }
- if ((data[1] == 'Q') || (data[1] == 'q')) { //step precision Quarter
- digitalWrite(ms1, LOW);
- digitalWrite(ms2, HIGH);
- digitalWrite(ms3, LOW);
- Serial.print("quarter-step, ");
- }
- if ((data[1] == 'E') || (data[1] == 'e')) { //step precision Eight
- digitalWrite(ms1, HIGH);
- digitalWrite(ms2, HIGH);
- digitalWrite(ms3, LOW);
- Serial.print("eight-step, ");
- }
- if ((data[1] == 'S') || (data[1] == 's')) { //step precision Sixteenth
- digitalWrite(ms1, HIGH);
- digitalWrite(ms2, HIGH);
- digitalWrite(ms3, HIGH);
- Serial.print("sixteenth-step, ");
- }
- if ((data[2] == 'E') || (data[2] == 'e')) { //controller Enabled
- digitalWrite(enable, LOW);
- digitalWrite(sleep,HIGH);
- Serial.print("controller Enabled, ");
- }
- if ((data[2] == 'D') || (data[2] == 'd')) { //controller Disabled
- digitalWrite(enable, HIGH);
- digitalWrite(sleep,HIGH);
- Serial.print("controller Disabled, ");
- }
- if ((data[2] == 'S') || (data[2] == 's')) { //controller in Sleep Mode
- digitalWrite(enable, LOW);
- digitalWrite(sleep,LOW);
- Serial.print("controller Sleeping, ");
- }
- if ((data[3] == 'R') || (data[3] == 'r')) { //Reset Enabled
- digitalWrite(reset, LOW);
- delay(50);
- digitalWrite(reset,HIGH);
- Serial.println("resetting, ");
- }
- if ((data[3] == 'N') || (data[3] == 'n')) { //Reset Disabled
- digitalWrite(reset, HIGH);
- Serial.println("reset Disabled, ");
- }
- }
- void requestEvent() {
- Wire.write(ds); // respond with message of 6 bytes
- // as expected by master
- }
- void calcStepper(char *ds) {
- Serial.flush();
- int Ans = strtol(ds + 4, NULL, 10);
- Ans = constrain(Ans, 0, 6400);
- stepcount = Ans;
- Serial.print("initiating ");
- Serial.print(Ans);
- Serial.println(" steps rotation");
- Serial.print("DataStream received by Controller: ");
- Serial.println(ds);
- var = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement