Advertisement
Guest User

A little refactoring

a guest
Jan 26th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include <AFMotor.h>
  2.  
  3. AF_Stepper motorL(200, 1);  // Left Motor, M1 & M2
  4. AF_Stepper motorR(200, 2);  // Right Motor, M3 & M4
  5. // Forward is Up on both motors.
  6.  
  7. // button holds the sketch in setup, until pressed. This stops the
  8. // motors from moving under USB power while uploading.
  9. const int button = 2;
  10. const int LED = 14; // LED indicates end of sketch
  11.  
  12. int progress = 0; // iteration/cycle
  13. // uses the "progress" to look into the array, and present the switch
  14. // progress, so it knows what basic to do.
  15. int current = 0;
  16. const int pixelsteps = 32; // size of a pixel, in steps
  17. //pixelsteps needs to be 2,4,6,8,16,32,or 64 else will skew image...
  18.  
  19. // this basic and format works
  20. // dont fiddle with it!
  21. int  basic[]{
  22.  
  23. 5,5,7,3,
  24. 5,4,4,5,7,5,
  25. 5,4,3,3,4,5,7,7,
  26. 5,4,3,2,2,3,4,5,7,9,
  27. 5,4,3,2,1,1,2,3,4,5,7,11,
  28. 5,4,3,2,1,1,1,1,2,3,4,5,7,13,
  29. 5,4,3,2,1,1,1,1,1,1,2,3,4,5,7,15,
  30. 5,4,3,2,1,1,1,1,1,1,1,1,1,3,4,5,7,17,
  31. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,19,
  32. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,21,
  33. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,23,
  34. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,25,
  35. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,27,
  36. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,29,
  37. 5,4,3,2,1,1,1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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,31,
  52. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,29,
  53. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,27,
  54. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,25,
  55. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,23,
  56. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,21,
  57. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,19,
  58. 5,4,3,2,1,1,1,1,1,1,1,1,1,1,2,3,4,5,7,17,
  59. 5,4,3,2,1,1,1,1,1,1,1,1,2,3,4,5,7,15,
  60. 8
  61.  
  62. };// end of basic
  63. /* Comma separate pixels or instructions: 1 is none, 2 is lightest,
  64.  * 6 is Darkest.
  65.  7 is return while the number following any 7 is the distance to return
  66.  *  in pixels,
  67.  8 is End. pixels are currently squares of 40 steps
  68.  **********************************************************************/
  69.  
  70. void setup() {
  71.  
  72.   pinMode (LED, OUTPUT);
  73.   pinMode (button, INPUT);
  74.  
  75.   motorL.setSpeed(50);  // 10 rpm  
  76.   motorR.setSpeed(50);  // 10 rpm  
  77.  
  78.   //hold
  79.   while (digitalRead (button) == HIGH){
  80.     // stops script. Its waiting for a button press (LOW on "button")
  81.   }
  82. }
  83.  
  84.  
  85. void loop() {
  86.   current = basic[progress]; // ****************************************
  87.  
  88.   switch (current) {
  89.     case 1: // nil, straight to next pixel
  90.       motorL.step(pixelsteps , FORWARD, INTERLEAVE);
  91.     break;
  92.  
  93.   case 2: // lightest, 1 step
  94.     pixel(2);
  95.     break;
  96.  
  97.   case 3: //  light, 2 steps
  98.     pixel(4);
  99.     break;
  100.  
  101.   case 4: // Medium, 4 steps
  102.     pixel(8);
  103.     break;
  104.  
  105.   case 5: // Darker, 10 steps
  106.     pixel(16);
  107.     break;
  108.  
  109.   case 6: // Darkest 20 steps
  110.     pixel(32);
  111.     break;
  112.  
  113.   case 7: // Carriage Return.
  114.     motorR.step((pixelsteps +1), FORWARD, INTERLEAVE);
  115.     ++progress;
  116.     current = basic[progress]; // **************************************
  117.     //The number following any 7 in the array is used only to tell it
  118.     // how far to return,
  119.     motorL.step((current * pixelsteps), BACKWARD, INTERLEAVE);
  120.     //and is then discarded. It is not a case, nor a shade.
  121.     motorR.step(1, FORWARD, INTERLEAVE);
  122.     break;
  123.  
  124.   // Lights the LED and waits for a button press as a method of stop
  125.   case 8:
  126.     digitalWrite (LED, HIGH);
  127.     //hold
  128.     while (digitalRead (button) == HIGH){
  129.       // stops script. Its waiting for a button press (LOW on "button")
  130.     }
  131.     break;
  132.   }
  133.  
  134.   ++progress; // move to the next instruction in the array!
  135. }
  136.  
  137. void pixel(int shade) {
  138.     for (int i = 0; i < (shade / 2); ++i) {    
  139.       motorL.step((pixelsteps / shade), FORWARD, INTERLEAVE);
  140.       motorR.step((pixelsteps - 2), FORWARD, INTERLEAVE);
  141.       motorL.step((pixelsteps / shade), FORWARD, INTERLEAVE);
  142.       motorR.step((pixelsteps - 2), BACKWARD, INTERLEAVE);  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement