SHARE
TWEET

Guts of my Hot Rodded 3D Printer

a guest Nov 19th, 2014 174 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // by Les Hall
  2. // started Mon Nov 17 2014
  3. //
  4.  
  5. int controlPin = 2;
  6. int coilPins[] = {0, 1, 4, 3};
  7. int fullStep[][4] = {  // two bit Gray code
  8.     {0, 1, 0, 1},  // - + - +
  9.     {0, 1, 1, 0},  // - + + -
  10.     {1, 0, 1, 0},  // + - + -
  11.     {1, 0, 0, 1}}; // + - - +
  12. int steps = 0;
  13. long pos = 0;
  14. long command = 0;
  15.  
  16.  
  17. void setup()
  18. {
  19.   pinMode(controlPin, INPUT);
  20.   for(int i=0; i<sizeof(coilPins); ++i)
  21.     pinMode(coilPins[i], OUTPUT);
  22.    
  23.   // reset sets pos to zero
  24.   pos = 0;
  25.   command = 0;
  26. }
  27.  
  28.  
  29. void loop()
  30. {
  31.   // read the high and low pulse durations
  32.   unsigned long pulseHigh = pulseIn(controlPin, HIGH, 20000);
  33.   unsigned long pulseLow = pulseIn(controlPin, LOW, 20000);
  34.  
  35.   // set commanded position if the control period matches
  36.   unsigned long T = pulseHigh + pulseLow;
  37.   if ( (T > 9500) && (T < 10500) )  // check for frequency
  38.     command = pulseHigh - pulseLow;
  39.  
  40.   // take a step toward the commanded position if not there yet
  41.   if (pos < (command - 25) )
  42.   {
  43.     --steps;
  44.     if (steps < 0) steps = 3;
  45.     pos += 10;
  46.   }
  47.   else if (pos > (command + 25) )
  48.   {
  49.     ++steps;
  50.     steps %= 4;
  51.     pos -= 10;
  52.   }
  53.  
  54.   // execute the next step
  55.   for (int i=0; i<4; ++i)
  56.     if (fullStep[steps][i] == 1)
  57.       digitalWrite(coilPins[i], LOW);
  58.     else
  59.       digitalWrite(coilPins[i], HIGH);
  60. }
RAW Paste Data
Top