Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // by Les Hall
- // started Mon Nov 17 2014
- //
- int controlPin = 2;
- int coilPins[] = {0, 1, 4, 3};
- int fullStep[][4] = { // two bit Gray code
- {0, 1, 0, 1}, // - + - +
- {0, 1, 1, 0}, // - + + -
- {1, 0, 1, 0}, // + - + -
- {1, 0, 0, 1}}; // + - - +
- int steps = 0;
- long pos = 0;
- long command = 0;
- void setup()
- {
- pinMode(controlPin, INPUT);
- for(int i=0; i<sizeof(coilPins); ++i)
- pinMode(coilPins[i], OUTPUT);
- // reset sets pos to zero
- pos = 0;
- command = 0;
- }
- void loop()
- {
- // read the high and low pulse durations
- unsigned long pulseHigh = pulseIn(controlPin, HIGH, 20000);
- unsigned long pulseLow = pulseIn(controlPin, LOW, 20000);
- // set commanded position if the control period matches
- unsigned long T = pulseHigh + pulseLow;
- if ( (T > 9500) && (T < 10500) ) // check for frequency
- command = pulseHigh - pulseLow;
- // take a step toward the commanded position if not there yet
- if (pos < (command - 25) )
- {
- --steps;
- if (steps < 0) steps = 3;
- pos += 10;
- }
- else if (pos > (command + 25) )
- {
- ++steps;
- steps %= 4;
- pos -= 10;
- }
- // execute the next step
- for (int i=0; i<4; ++i)
- if (fullStep[steps][i] == 1)
- digitalWrite(coilPins[i], LOW);
- else
- digitalWrite(coilPins[i], HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement