Advertisement
kohag

Help

May 4th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <Stepper.h>
  2. #include <PCF8574.h>
  3.  
  4. PCF8574 stepperExpander(0x20); //Create the PCF8574 object at the hex address the PCF8574 chip is at
  5. const int stepsPerRevolution = 2048; // Steps per revolution for your stepper motor
  6.  
  7. Stepper myStepperX(stepperExpander,stepsPerRevolution, P0, P2, P1, P3); //Declaring stepper motor inputs 0 = P0, 1 = P1,...
  8. Stepper myStepperY(stepperExpander,stepsPerRevolution, 4, 6, 5, 7);
  9.  
  10. float xcoordinates[10] = {0, 37.5, 50, 37.5, 0, -37.5, -50, -37.5, 0, 0}; //X coordinates for circle
  11. float ycoordinates[10] = {50,37.5, 0, -37.5, -50, -37.5, 0, 37.5, 50, 0}; //Y coordinates for circle
  12. int ArrayLength = sizeof(xcoordinates) / sizeof(int); //Getting length of coordinates array
  13.  
  14. double xdiff = 0, ydiff = 0, stepX = 0, stepY = 0, smaller = 0;
  15.  
  16. int speed = 16; // setting RPM for motor (for 28BYJ-48 is 16 rpm max for 2048 spr)
  17. void setup() {
  18. myStepperX.setSpeed(speed);
  19. myStepperY.setSpeed(speed);
  20. Serial.begin(9600);
  21. pinMode(10,OUTPUT);
  22. pinMode(12,INPUT); //endstop
  23. pinMode(11,INPUT); //endstop
  24. stepperExpander.begin(); //start of PCF8574
  25. }
  26.  
  27. void loop() {
  28. for (int i = 0; i < ArrayLength; i++)
  29. {
  30. if (i==0){
  31. xdiff = xcoordinates[i];
  32. ydiff = ycoordinates[i];
  33. }
  34. else{
  35. xdiff = xcoordinates[i] - xcoordinates[i-1];
  36. ydiff = ycoordinates[i] - ycoordinates[i-1];
  37. }
  38. if (abs(xdiff) < abs(ydiff) and xdiff != 0){
  39. stepY = (ydiff / abs(xdiff));
  40. if (xdiff <0){
  41. stepX = -1;
  42. }
  43. else{
  44. stepX = 1;
  45. }
  46. }
  47. else if (abs(ydiff) < abs(xdiff) and ydiff != 0){
  48. stepX = (xdiff / abs(ydiff));
  49. if (ydiff <0){
  50. stepY = -1;
  51. }
  52. else{
  53. stepY = 1;
  54. }
  55. }
  56.  
  57. else if (xdiff == 0){
  58. stepX = 0;
  59. stepY = ydiff;
  60. smaller = 1;
  61. }
  62.  
  63. else if (ydiff == 0){
  64. stepX = xdiff;
  65. stepY = 0;
  66. smaller = 1;
  67. }
  68. else{
  69. smaller = xdiff;
  70. if (xdiff<0)
  71. {
  72. stepX = -1;
  73. }
  74. else{
  75. stepX = 1;
  76. }
  77. if (ydiff<0)
  78. {
  79. stepY = -1;
  80. }
  81. else{
  82. stepY = 1;
  83. }
  84. }
  85. if (abs(ydiff) < abs(xdiff)){
  86. if (ydiff == 0){
  87. smaller = 1;
  88. }
  89. else{
  90. smaller = abs(ydiff);
  91. }
  92. }
  93. else
  94. if (xdiff == 0){
  95. smaller = 1;
  96. }
  97. else{
  98. smaller = abs(xdiff);
  99. }
  100. for (int n = 0; n < smaller; n++){
  101. digitalWrite(10,LOW); //Switch laser off
  102. myStepperX.step(stepX);
  103. myStepperY.step(stepY);
  104. digitalWrite(10,HIGH); //Switch laser on
  105. delay(1);
  106. }
  107. }
  108. delay(1000);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement