Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <AccelStepper.h>
  2.  
  3. int leBigMove[2] = {20000, 20000};
  4. int stepperDestination[2];
  5. int commande[2] = {0, 0};
  6. int bottone = 13;
  7. int bottone2 = 12;
  8. int deathButton = 11;
  9. int motor1DirPin = 2;
  10. int motor1StepPin = 3;
  11. int motor2DirPin = 4;
  12. int motor2StepPin = 5;
  13. int movSensore = 10;
  14. int startGame = 0;
  15. int numMovs = 0;
  16. int stopGame = 0;
  17. AccelStepper stepper1(1, motor1StepPin, motor1DirPin);
  18. AccelStepper stepper2(1, motor2StepPin, motor2DirPin);
  19.  
  20. uint32_t ts1[2] = {millis(), millis()};
  21. uint32_t ts2[2] = {millis()+1000001, millis()+1000001};
  22.  
  23. void setup() {
  24. delay(1000);
  25. pinMode(bottone, INPUT);
  26. pinMode(bottone2, INPUT);
  27. pinMode(deathButton, INPUT);
  28. Serial.begin(9600);
  29. stepper1.setSpeed(50);
  30. stepper1.setMaxSpeed(50);
  31. stepper1.setAcceleration(100);
  32. stepper2.setSpeed(50);
  33. stepper2.setMaxSpeed(50);
  34. stepper2.setAcceleration(100);
  35. }
  36.  
  37. void loop() {
  38. if(!stopGame) {
  39. int checkMov = digitalRead(movSensore);
  40.  
  41. if(checkMov == HIGH && numMovs < 3) {
  42. numMovs++;
  43. delay(500);
  44. }
  45.  
  46. if(startGame == 0 && checkMov == HIGH && numMovs > 2) {
  47. Serial.println("Movimento rilevato");
  48. stepper1.setSpeed(50);
  49. stepper1.moveTo(stepperDestination[0]);
  50. stepper1.run();
  51.  
  52. stepper2.setSpeed(50);
  53. stepper2.moveTo(stepperDestination[1]);
  54. stepper2.run();
  55. stepperDestination[0] = leBigMove[0];
  56. stepperDestination[1] = leBigMove[1];
  57. startGame = 1;
  58. }
  59.  
  60. if(startGame) {
  61. int switchs[2] = {digitalRead(bottone), digitalRead(bottone2)};
  62. if (switchs[0] == LOW) {
  63. commande[0] = 0;
  64. } else { // In caso il bottone venga pressato
  65. if(ts2[0]-ts1[0] > 500) {
  66. if (commande[0] == 0) {
  67. stepper1.setSpeed(0);
  68. if (stepperDestination[0] < 0) {
  69. stepper1.moveTo(stepperDestination[0]);
  70. stepper1.run();
  71. stepperDestination[0] = leBigMove[0];
  72. Serial.println("normal");
  73. } else {
  74. stepper1.moveTo(-stepperDestination[0]);
  75. stepper1.run();
  76. stepperDestination[0] = -leBigMove[0];
  77. Serial.println("inverse");
  78. }
  79. commande[0] = 1;
  80. }
  81. ts1[0] = ts2[0];
  82. }
  83.  
  84. }
  85. ts2[0] = millis();
  86.  
  87. if (switchs[1] == LOW) {
  88. commande[1] = 0;
  89. } else { // In caso il bottone venga pressato
  90. if(ts2[1]-ts1[1] > 500) {
  91. if (commande[1] == 0) {
  92. stepper2.setSpeed(0);
  93. if (stepperDestination[1] < 0) {
  94. stepper2.moveTo(stepperDestination[1]);
  95. stepper2.run();
  96. stepperDestination[1] = leBigMove[1];
  97. Serial.println("normal2");
  98. } else {
  99. stepper2.moveTo(-stepperDestination[1]);
  100. stepper2.run();
  101. stepperDestination[1] = -leBigMove[1];
  102. Serial.println("inverse2");
  103. }
  104. commande[1] = 1;
  105. }
  106. ts1[1] = ts2[1];
  107. }
  108.  
  109. }
  110. ts2[1] = millis();
  111.  
  112. int is_death = digitalRead(deathButton);
  113. if(is_death == HIGH) {
  114. stepper1.setSpeed(0);
  115. stepper2.setSpeed(0);
  116. startGame = 0;
  117. numMovs = 0;
  118. Serial.println("morto");
  119. stopGame = 1;
  120. }
  121.  
  122. stepper1.moveTo(stepperDestination[0]);
  123. stepper1.run();
  124. stepper2.moveTo(stepperDestination[1]);
  125. stepper2.run();
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement