Guest User

Arduino rubber band turret code

a guest
Dec 15th, 2013
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.69 KB | None | 0 0
  1. http://www.4tronix.co.uk/arduino/Stepper-Motors.php
  2. http://developeraspirations.files.wordpress.com/2010/04/l293dne.png
  3.  
  4.  
  5.  
  6. //declare variables for the stepper motor pins
  7. int motorPin1 = 8;     // Blue   - 28BYJ48 pin 1
  8. int motorPin2 = 9;     // Pink   - 28BYJ48 pin 2
  9. int motorPin3 = 10;    // Yellow - 28BYJ48 pin 3
  10. int motorPin4 = 11;    // Orange - 28BYJ48 pin 4
  11.                      
  12. int XmotorPin1 = 2; //pins for the x axis motor
  13. int XmotorPin2 = 3;
  14.  
  15. int YmotorPin1 = 4; //pins for the y axis motor
  16. int YmotorPin2 = 5;
  17.  
  18. int joyClick = A0; //thumbstick click
  19. int joyPinY = A2; //thumbstick axis
  20. int joyPinX = A4;
  21.  
  22. int loadBtn = A5; //load button pin
  23. int duration = 0; //how long the button is pressed
  24.  
  25. boolean laser = 0; //laser on or off
  26. int laserPin = 7; //pin for relay for laser
  27.                        
  28. int motorSpeed = 700;  //variable to set stepper speed, lower = faster. minimum should be around 700
  29. int countsperrev = 256;
  30.  
  31.        
  32. int repeatamount= 0; //how many times to repeat
  33. int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
  34.  
  35. //////////////////////////////////////////////////////////////////////////////
  36. void setup() {
  37.   //declare the motor pins as outputs
  38.   pinMode(motorPin1, OUTPUT);
  39.   pinMode(motorPin2, OUTPUT);
  40.   pinMode(motorPin3, OUTPUT);
  41.   pinMode(motorPin4, OUTPUT);
  42.  
  43.   pinMode(XmotorPin1, OUTPUT);
  44.   pinMode(XmotorPin2, OUTPUT);
  45.   pinMode(YmotorPin1, OUTPUT);
  46.   pinMode(YmotorPin2, OUTPUT);
  47.  
  48.   pinMode(7, OUTPUT);
  49.  
  50.   pinMode(A0,INPUT);
  51.   pinMode(A2,INPUT);
  52.   pinMode(A4,INPUT);
  53.   pinMode(A5,INPUT);
  54.  
  55.   Serial.begin(9600);
  56. }
  57.  
  58. //////////////////////////////////////////////////////////////////////////////
  59. void loop(){
  60.  
  61. Serial.print(analogRead(joyPinY));
  62. Serial.print(" ");
  63. Serial.print(analogRead(joyPinX));
  64. Serial.print(" ");
  65. Serial.print(analogRead(joyClick));
  66. Serial.print(" ");
  67. Serial.print(laser);
  68. Serial.print(" ");
  69. Serial.println(analogRead(loadBtn));
  70.  
  71.  
  72.   if (analogRead(loadBtn) > 1022)
  73.     {
  74.          while(repeatamount <= 512)
  75.             {
  76.                 clockwise();
  77.                 repeatamount++;
  78.             }
  79.          repeatamount = 0;
  80.     }
  81.  
  82.  
  83.   if (analogRead(joyClick) <= 1 && analogRead(joyPinX) > 150) // to avoid accidentally shooting while turning joystick right (it happens)
  84.   {
  85.     while (analogRead(joyClick) <= 1 && analogRead(joyPinX) > 150) //while the thumbstick button is clicked, count
  86.     {
  87.       delay(10);
  88.         duration++;
  89.         Serial.println(duration);
  90.     }
  91.     if (duration < 100) //if the amount counted is under 1 second (100 miliseconds*10), fire the elastic!
  92.       {
  93.         digitalWrite(XmotorPin1, LOW); //turns off rotation
  94.         digitalWrite(XmotorPin2, LOW);
  95.         digitalWrite(YmotorPin1, LOW);
  96.         digitalWrite(YmotorPin2, LOW);
  97.       while(repeatamount <= 512)
  98.         {
  99.           ctclockwise(); //runs counter clockwise stepper movement
  100.           repeatamount++;
  101.         }
  102.       repeatamount = 0;
  103.       }
  104.      
  105.       else // if the button is held over 1 second, change the state of the laser
  106.       {
  107.         if (laser == 0) //if the laser is off turn it on
  108.          {
  109.            laser = 1;
  110.            digitalWrite(laserPin, HIGH);
  111.            delay(500);
  112.          }
  113.          else
  114.          {
  115.            laser = 0;
  116.            digitalWrite(laserPin, LOW);
  117.            delay(500);
  118.          }
  119.       }
  120.     duration = 0;
  121.   }
  122.  
  123.  
  124. if (analogRead(joyPinY) > 945 && analogRead(joyPinY) < 1023) //If the thumbstick is pushed backward, move the turret up
  125. {
  126.   digitalWrite(YmotorPin1, HIGH);
  127.   digitalWrite(YmotorPin2, LOW);
  128.   delay(10);
  129.   digitalWrite(YmotorPin1, LOW);
  130.   digitalWrite(YmotorPin2, LOW);
  131.   delay(50);
  132.   analogRead(joyPinY);
  133. }
  134. else if (analogRead(joyPinY) > 1022) //If the thumbstick is pushed backward, move the turret up
  135. {
  136.   digitalWrite(YmotorPin1, HIGH);
  137.   digitalWrite(YmotorPin2, LOW);
  138.   analogRead(joyPinY);
  139. }
  140.   else if (analogRead(joyPinY) < 850 && analogRead(joyPinY) > 300) //if the thumbstick is pushed forward, move the turret down
  141.   {
  142.   digitalWrite(YmotorPin1, LOW);
  143.   digitalWrite(YmotorPin2, HIGH);
  144.   delay(10);
  145.   digitalWrite(YmotorPin1, LOW);
  146.   digitalWrite(YmotorPin2, LOW);
  147.   delay(50);
  148.   analogRead(joyPinY);
  149.   }
  150.   else if (analogRead(joyPinY) < 301) //if the thumbstick is pushed forward, move the turret down
  151.   {
  152.   digitalWrite(YmotorPin1, LOW);
  153.   digitalWrite(YmotorPin2, HIGH);
  154.   analogRead(joyPinY);
  155.   }
  156.   else
  157.   {
  158.   digitalWrite(YmotorPin1, LOW);
  159.   digitalWrite(YmotorPin2, LOW);
  160.   }
  161.  
  162. if (analogRead(joyPinX) > 945  && analogRead(joyPinX) < 1023) //if the thumbstick is pushed left LIGHTLY, move the slowly left
  163. {
  164.   digitalWrite(XmotorPin1, HIGH);
  165.   digitalWrite(XmotorPin2, LOW);
  166.   delay(10);
  167.   digitalWrite(XmotorPin1, LOW);
  168.   digitalWrite(XmotorPin2, LOW);
  169.   delay(30);
  170. }
  171.  
  172. else if (analogRead(joyPinX) > 1022) //if the thumbstick is pushed left all the way, move the quickly left
  173. {
  174.   digitalWrite(XmotorPin1, HIGH);
  175.   digitalWrite(XmotorPin2, LOW);
  176.   analogRead(joyPinX);
  177. }
  178.   else if (analogRead(joyPinX) > 890 && (analogRead(joyPinX) < 945)) //if the thumbstick is neutral, keep the motors still
  179.   {
  180.   digitalWrite(XmotorPin1, LOW);
  181.   digitalWrite(XmotorPin2, LOW);
  182.   analogRead(joyPinX);
  183.   }
  184.  
  185.  if (analogRead(joyPinX) < 890 && (analogRead(joyPinX) > 200)) //if the thumbstick is pushed right LIGHTLY, move the slowly right
  186.   {
  187.   digitalWrite(XmotorPin1, LOW);
  188.   digitalWrite(XmotorPin2, HIGH);
  189.   delay(10);
  190.   digitalWrite(XmotorPin1, LOW);
  191.   digitalWrite(XmotorPin2, LOW);
  192.   delay(30);
  193.   }
  194.   else if (analogRead(joyPinX) <= 200) //if the thumbstick is pushed right all the way, move the quickly right
  195.   {
  196.   digitalWrite(XmotorPin1, LOW);
  197.   digitalWrite(XmotorPin2, HIGH);
  198.   analogRead(joyPinX);
  199.   }
  200.   else if (analogRead(joyPinX) > 890 && (analogRead(joyPinX) < 945)) //if the thumbstick is neutral, keep the motors still
  201.   {
  202.   digitalWrite(XmotorPin1, LOW);
  203.   digitalWrite(XmotorPin2, LOW);
  204.   analogRead(joyPinX);
  205.   }
  206. }
  207.  
  208. //////////////////////////////////////////////////////////////////////////////
  209. //set pins to ULN2003 high in sequence from 1 to 4
  210. //delay "motorSpeed" between each pin setting (to determine speed)
  211.  
  212.  
  213. void clockwise() //normal stepper rotation
  214. {
  215.   for(int i = 7; i >= 0; i--)
  216.   {
  217.     setOutput(i);
  218.     delayMicroseconds(motorSpeed);
  219.   }
  220. }
  221.  
  222. void ctclockwise() //reverse rotation (this fires elastic)
  223. {
  224.   for(int i = 0; i < 8; i++)
  225.   {
  226.     setOutput(i);
  227.     delayMicroseconds(motorSpeed);
  228.   }
  229. }
  230.  
  231. void setOutput(int out)
  232. {
  233.   digitalWrite(motorPin1, bitRead(lookup[out], 0));
  234.   digitalWrite(motorPin2, bitRead(lookup[out], 1));
  235.   digitalWrite(motorPin3, bitRead(lookup[out], 2));
  236.   digitalWrite(motorPin4, bitRead(lookup[out], 3));
  237. }
Advertisement
Add Comment
Please, Sign In to add comment