Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://www.4tronix.co.uk/arduino/Stepper-Motors.php
- http://developeraspirations.files.wordpress.com/2010/04/l293dne.png
- //declare variables for the stepper motor pins
- int motorPin1 = 8; // Blue - 28BYJ48 pin 1
- int motorPin2 = 9; // Pink - 28BYJ48 pin 2
- int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
- int motorPin4 = 11; // Orange - 28BYJ48 pin 4
- int XmotorPin1 = 2; //pins for the x axis motor
- int XmotorPin2 = 3;
- int YmotorPin1 = 4; //pins for the y axis motor
- int YmotorPin2 = 5;
- int joyClick = A0; //thumbstick click
- int joyPinY = A2; //thumbstick axis
- int joyPinX = A4;
- int loadBtn = A5; //load button pin
- int duration = 0; //how long the button is pressed
- boolean laser = 0; //laser on or off
- int laserPin = 7; //pin for relay for laser
- int motorSpeed = 700; //variable to set stepper speed, lower = faster. minimum should be around 700
- int countsperrev = 256;
- int repeatamount= 0; //how many times to repeat
- int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
- //////////////////////////////////////////////////////////////////////////////
- void setup() {
- //declare the motor pins as outputs
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(motorPin3, OUTPUT);
- pinMode(motorPin4, OUTPUT);
- pinMode(XmotorPin1, OUTPUT);
- pinMode(XmotorPin2, OUTPUT);
- pinMode(YmotorPin1, OUTPUT);
- pinMode(YmotorPin2, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(A0,INPUT);
- pinMode(A2,INPUT);
- pinMode(A4,INPUT);
- pinMode(A5,INPUT);
- Serial.begin(9600);
- }
- //////////////////////////////////////////////////////////////////////////////
- void loop(){
- Serial.print(analogRead(joyPinY));
- Serial.print(" ");
- Serial.print(analogRead(joyPinX));
- Serial.print(" ");
- Serial.print(analogRead(joyClick));
- Serial.print(" ");
- Serial.print(laser);
- Serial.print(" ");
- Serial.println(analogRead(loadBtn));
- if (analogRead(loadBtn) > 1022)
- {
- while(repeatamount <= 512)
- {
- clockwise();
- repeatamount++;
- }
- repeatamount = 0;
- }
- if (analogRead(joyClick) <= 1 && analogRead(joyPinX) > 150) // to avoid accidentally shooting while turning joystick right (it happens)
- {
- while (analogRead(joyClick) <= 1 && analogRead(joyPinX) > 150) //while the thumbstick button is clicked, count
- {
- delay(10);
- duration++;
- Serial.println(duration);
- }
- if (duration < 100) //if the amount counted is under 1 second (100 miliseconds*10), fire the elastic!
- {
- digitalWrite(XmotorPin1, LOW); //turns off rotation
- digitalWrite(XmotorPin2, LOW);
- digitalWrite(YmotorPin1, LOW);
- digitalWrite(YmotorPin2, LOW);
- while(repeatamount <= 512)
- {
- ctclockwise(); //runs counter clockwise stepper movement
- repeatamount++;
- }
- repeatamount = 0;
- }
- else // if the button is held over 1 second, change the state of the laser
- {
- if (laser == 0) //if the laser is off turn it on
- {
- laser = 1;
- digitalWrite(laserPin, HIGH);
- delay(500);
- }
- else
- {
- laser = 0;
- digitalWrite(laserPin, LOW);
- delay(500);
- }
- }
- duration = 0;
- }
- if (analogRead(joyPinY) > 945 && analogRead(joyPinY) < 1023) //If the thumbstick is pushed backward, move the turret up
- {
- digitalWrite(YmotorPin1, HIGH);
- digitalWrite(YmotorPin2, LOW);
- delay(10);
- digitalWrite(YmotorPin1, LOW);
- digitalWrite(YmotorPin2, LOW);
- delay(50);
- analogRead(joyPinY);
- }
- else if (analogRead(joyPinY) > 1022) //If the thumbstick is pushed backward, move the turret up
- {
- digitalWrite(YmotorPin1, HIGH);
- digitalWrite(YmotorPin2, LOW);
- analogRead(joyPinY);
- }
- else if (analogRead(joyPinY) < 850 && analogRead(joyPinY) > 300) //if the thumbstick is pushed forward, move the turret down
- {
- digitalWrite(YmotorPin1, LOW);
- digitalWrite(YmotorPin2, HIGH);
- delay(10);
- digitalWrite(YmotorPin1, LOW);
- digitalWrite(YmotorPin2, LOW);
- delay(50);
- analogRead(joyPinY);
- }
- else if (analogRead(joyPinY) < 301) //if the thumbstick is pushed forward, move the turret down
- {
- digitalWrite(YmotorPin1, LOW);
- digitalWrite(YmotorPin2, HIGH);
- analogRead(joyPinY);
- }
- else
- {
- digitalWrite(YmotorPin1, LOW);
- digitalWrite(YmotorPin2, LOW);
- }
- if (analogRead(joyPinX) > 945 && analogRead(joyPinX) < 1023) //if the thumbstick is pushed left LIGHTLY, move the slowly left
- {
- digitalWrite(XmotorPin1, HIGH);
- digitalWrite(XmotorPin2, LOW);
- delay(10);
- digitalWrite(XmotorPin1, LOW);
- digitalWrite(XmotorPin2, LOW);
- delay(30);
- }
- else if (analogRead(joyPinX) > 1022) //if the thumbstick is pushed left all the way, move the quickly left
- {
- digitalWrite(XmotorPin1, HIGH);
- digitalWrite(XmotorPin2, LOW);
- analogRead(joyPinX);
- }
- else if (analogRead(joyPinX) > 890 && (analogRead(joyPinX) < 945)) //if the thumbstick is neutral, keep the motors still
- {
- digitalWrite(XmotorPin1, LOW);
- digitalWrite(XmotorPin2, LOW);
- analogRead(joyPinX);
- }
- if (analogRead(joyPinX) < 890 && (analogRead(joyPinX) > 200)) //if the thumbstick is pushed right LIGHTLY, move the slowly right
- {
- digitalWrite(XmotorPin1, LOW);
- digitalWrite(XmotorPin2, HIGH);
- delay(10);
- digitalWrite(XmotorPin1, LOW);
- digitalWrite(XmotorPin2, LOW);
- delay(30);
- }
- else if (analogRead(joyPinX) <= 200) //if the thumbstick is pushed right all the way, move the quickly right
- {
- digitalWrite(XmotorPin1, LOW);
- digitalWrite(XmotorPin2, HIGH);
- analogRead(joyPinX);
- }
- else if (analogRead(joyPinX) > 890 && (analogRead(joyPinX) < 945)) //if the thumbstick is neutral, keep the motors still
- {
- digitalWrite(XmotorPin1, LOW);
- digitalWrite(XmotorPin2, LOW);
- analogRead(joyPinX);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- //set pins to ULN2003 high in sequence from 1 to 4
- //delay "motorSpeed" between each pin setting (to determine speed)
- void clockwise() //normal stepper rotation
- {
- for(int i = 7; i >= 0; i--)
- {
- setOutput(i);
- delayMicroseconds(motorSpeed);
- }
- }
- void ctclockwise() //reverse rotation (this fires elastic)
- {
- for(int i = 0; i < 8; i++)
- {
- setOutput(i);
- delayMicroseconds(motorSpeed);
- }
- }
- void setOutput(int out)
- {
- digitalWrite(motorPin1, bitRead(lookup[out], 0));
- digitalWrite(motorPin2, bitRead(lookup[out], 1));
- digitalWrite(motorPin3, bitRead(lookup[out], 2));
- digitalWrite(motorPin4, bitRead(lookup[out], 3));
- }
Advertisement
Add Comment
Please, Sign In to add comment