Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- +-----+
- +----[PWR]-------------------| USB |--+
- | +-----+ |
- | GND/RST2 [ ][ ] |
- | MOSI2/SCK2 [ ][ ] A5/SCL[ ] | C5
- | 5V/MISO2 [ ][ ] A4/SDA[ ] | C4
- | AREF[ ] |
- | GND[ ] |
- | [ ]N/C SCK/13[ ] | B5 //usb BT
- | [ ]IOREF MISO/12[ ] | . //usb BT
- | [ ]RST MOSI/11[ ]~| . //usb BT
- | [ ]3V3 +---+ 10[ ]~| .
- | [ ]5v -| A |- 9[ ]~| .
- | [ ]GND -| R |- 8[ ] | B0
- | [ ]GND -| D |- |
- | [ ]Vin -| U |- 7[ ] | D7
- | -| I |- 6[ ]~| .
- | [ ]A0 -| N |- 5[ ]~| .
- | [ ]A1 -| O |- 4[ ] | .
- | [ ]A2 +---+ INT1/3[ ]~| .
- | [ ]A3 INT0/2[ ] | .
- | [ ]A4/SDA RST SCK MISO TX>1[ ] | .
- | [ ]A5/SCL [ ] [ ] [ ] RX<0[ ] | D0
- | [ ] [ ] [ ] |
- | UNO_R3 GND MOSI 5V ____________/
- \_______________________/
- */
- #include <PS4BT.h>
- #include <usbhub.h>
- #ifdef dobogusinclude
- #include <spi4teensy3.h>
- #endif
- #include <SPI.h>
- //routines//
- typedef void (*PFUNC) (void);
- PFUNC myfuncs[] = {&routine1, &routine2, &routine3, &routine4, &routine5, &routine6 ,&routine7};
- #define CNT_PFUNCS (sizeof(myfuncs)/sizeof(myfuncs[0]))
- uint8_t ifunc = 0;
- bool Qstop1 = true;
- bool Qstop2 = true;
- bool playmode = false;
- bool playing = true;
- bool enabled = false;
- //routines end//
- //stepper//
- #define DIR_PIN 4 //stepper one = x
- #define STEP_PIN 3
- #define ENABLE_PIN 5
- int step_speed = 500;
- //new stepper stuff
- #define STEP_HIGH PORTD |= 0b00001000;
- #define STEP_LOW PORTD &= ~0b00001000;
- #define TIMER1_INTERRUPTS_ON TIMSK1 |= (1 << OCIE1A);
- #define TIMER1_INTERRUPTS_OFF TIMSK1 &= ~(1 << OCIE1A);
- unsigned int c0;
- volatile int dir = 0;
- volatile unsigned int maxSpeed = 500;
- volatile unsigned long n = 0;
- volatile float d;
- volatile unsigned long stepCount = 0;
- volatile unsigned long rampUpStepCount = 0;
- volatile unsigned long totalSteps = 0;
- volatile int stepPosition = 0;
- volatile bool movementDone = false;
- //stepper end//
- USB Usb;
- BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
- //PS4BT PS4(&Btd, PAIR); //use this to pair with new device
- PS4BT PS4(&Btd); //use this to keep link and uncomment the pair line of code above.
- bool printAngle, printTouch;
- uint8_t oldL2Value, oldR2Value;
- // _
- // ___ ___| |_ _ _ _ __
- /// __|/ _ \ __| | | | '_ \
- //\__ \ __/ |_| |_| | |_) |
- //|___/\___|\__|\__,_| .__/
- // |_|
- void setup() {
- Serial.begin(115200);
- #if !defined(__MIPSEL__)
- while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
- #endif
- if (Usb.Init() == -1) {
- Serial.print(F("\r\nOSC Aint workin"));
- while(1); // Halt
- }
- Serial.print(F("\r\nPS4 Bluetooth workin Well\n\n"));
- Serial.println("Please press the playstation button to connect\n\n");
- Serial.print("Waiting..");delay(700); Serial.print("for no reason apparantly!.."); delay(500); Serial.print("...");delay(200);Serial.print(".");delay(500);Serial.print("..");delay(500);Serial.print(".....");
- Serial.println("\nButton layout is as follows:");
- Serial.println(" optionsB = key-map");
- Serial.println("optionsB + left or right = changes current mode.");
- Serial.println(" optionsB + up or down = changes stepper speed.");
- Serial.println(" optionsB + cross = enter current mode.");
- Serial.println(" optionsB + circle = enables/disables stepper");
- Serial.println(" optionsB + share = playmode shows all sensor and button info,same button to exit");
- Serial.println(" triangleB = exits current mode playing / note: disables all motors.");
- //stepper//
- pinMode(STEP_PIN, OUTPUT);
- pinMode(DIR_PIN, OUTPUT);
- pinMode(ENABLE_PIN, OUTPUT);
- // digitalWrite(ENABLE_PIN, HIGH); //is off high || low is on
- noInterrupts();
- TCCR1A = 0;
- TCCR1B = 0;
- TCNT1 = 0;
- OCR1A = 1000;
- TCCR1B |= (1 << WGM12);
- TCCR1B |= ((1 << CS11) | (1 << CS10));
- interrupts();
- c0 = 1600; //2000 * sqrt( 1 ) * 0.67703;
- // stepper//
- }
- ISR(TIMER1_COMPA_vect)
- {
- if ( stepCount < totalSteps ) {
- STEP_HIGH
- STEP_LOW
- stepCount++;
- stepPosition += dir;
- }
- else {
- movementDone = true;
- TIMER1_INTERRUPTS_OFF
- }
- if ( rampUpStepCount == 0 ) { // ramp up phase
- n++;
- d = d - (2 * d) / (4 * n + 1);
- if ( d <= maxSpeed ) {
- d = maxSpeed;
- rampUpStepCount = stepCount;
- }
- if ( stepCount >= totalSteps / 2 ) {
- rampUpStepCount = stepCount;
- }
- }
- else if ( stepCount >= totalSteps - rampUpStepCount ) { // ramp down phase
- n--;
- d = (d * (4 * n + 1)) / (4 * n + 1 - 2);
- }
- OCR1A = d;
- }
- void moveNSteps(int steps) {
- digitalWrite(DIR_PIN, steps < 0 ? HIGH : LOW);
- dir = steps > 0 ? 1 : -1;
- totalSteps = abs(steps);
- d = c0;
- OCR1A = d;
- stepCount = 0;
- n = 0;
- rampUpStepCount = 0;
- movementDone = false;
- TIMER1_INTERRUPTS_ON
- }
- void moveToPosition(int p, bool wait = true) {
- moveNSteps(p - stepPosition);
- while ( wait && ! movementDone );
- }
- void Movecount(){
- int count = 0;
- while ( !movementDone ) {
- if ( count > 3 ) {
- movementDone = true;
- TIMER1_INTERRUPTS_OFF
- }
- else {
- Serial.print(count++);
- Serial.print(" ");
- Serial.println( stepPosition );
- delay(500);
- }
- }
- Serial.println("Finished.");
- Serial.println( stepPosition );
- while (true);
- }
- // _
- //| | ___ ___ _ __
- //| |/ _ \ / _ \| '_
- //| | (_) | (_) | |_) |
- //|_|\___/ \___/| .__/
- // |_|
- void loop() {
- checkconection();
- Usb.Task();
- routselect();
- freeplay();
- // Movecount();
- }
- // int count = 0;
- // while ( !movementDone ) {
- // if ( count > 3 ) {
- // movementDone = true;
- // TIMER1_INTERRUPTS_OFF
- // }
- // else {
- // Serial.print(count++);
- // Serial.print(" ");
- // Serial.println( stepPosition );
- // delay(500);
- // }
- // }
- // _ _ _ _
- // ___| |__ ___ ___ | | __ ___ ___ _ __ _ __ ___ ___| |_(_) ___ _ __
- // / __| '_ \ / _ \/ __ | |/ / __/ _ \| '_ \| '_ \ / _ \/ __| __| |/ _ \| '_
- //| (__| | | | __/ (__ | < (_| (_) | | | | | | | __/ (__| |_| | (_) | | | |
- // \___|_| |_|\___|\___ |_|\_ps4\___\___/|_| |_|_| |_|\___|\___|\__|_|\___/|_| |_|
- void checkconection(){
- if (PS4.connected() && Qstop1 == true){
- Serial.println("\nBT-ps4-connected");
- Serial.println("ENJOY....(*_*)....\n\n");
- Qstop1 = false;
- }}
- // __ _
- // / _|_ __ ___ ___ _ __ | | __ _ _ _
- //| |_| '__/ _ \/ _ \ '_ \| |/ _` | | | |
- //| _| | | __/ __/ |_) | | (_| | |_| |
- //|_| |_| \___|\___| .__/|_|\__,_|\__, |
- // |_| |___/
- void freeplay(){
- if (PS4.connected()) {
- if (PS4.getButtonPress(OPTIONS) && PS4.getButtonClick(SHARE)) {
- playmode = !playmode;
- if (Qstop2 == true){
- Serial.print("playmode\n\n");
- }
- if (Qstop2 == false ){
- Serial.print("\n\n");
- Serial.print("playmode deselected\n\n");
- digitalWrite(ENABLE_PIN, HIGH);
- }
- Qstop2 = !Qstop2;
- }}
- if (playmode == true){
- ps4();
- }}
- // _ _
- // _ __ ___| || |
- //| '_ \/ __| || |_
- //| |_) \__ \__ _|
- //| .__/|___/ |_|
- //|_|
- void ps4(){
- if (PS4.connected()) {
- if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) {
- Serial.print(F("\r\nLeftHatX: ")); Serial.print(PS4.getAnalogHat(LeftHatX)); Serial.print(F("\tLeftHatY: "));
- Serial.print(PS4.getAnalogHat(LeftHatY)); Serial.print(F("\tRightHatX: "));
- Serial.print(PS4.getAnalogHat(RightHatX)); Serial.print(F("\tRightHatY: "));- Serial.print(PS4.getAnalogHat(RightHatY));
- }
- if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
- Serial.print(F("\r\nL2: "));
- Serial.print(PS4.getAnalogButton(L2));
- Serial.print(F("\tR2: "));
- Serial.print(PS4.getAnalogButton(R2));
- }
- if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
- PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
- oldL2Value = PS4.getAnalogButton(L2);
- oldR2Value = PS4.getAnalogButton(R2);
- if (PS4.getButtonClick(PS)) {
- Serial.print(F("\r\nPS"));
- PS4.disconnect();
- }
- else {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.print(F("\r\nTraingle"));
- PS4.setRumbleOn(RumbleLow);
- }
- if (PS4.getButtonClick(CIRCLE)) {
- Serial.print(F("\r\nCircle"));
- PS4.setRumbleOn(RumbleHigh);
- }
- if (PS4.getButtonPress(SQUARE)) {
- Serial.print(F("\r\nSquare"));
- }
- if (PS4.getButtonClick(CROSS)) {
- Serial.print(F("\r\nSquare"));
- }
- if (PS4.getButtonClick(UP)) {
- Serial.print(F("\r\nUp"));
- PS4.setLed(Red);
- } if(PS4.getButtonPress(RIGHT)) {
- Serial.print(F("\r\nRight"));
- } if (PS4.getButtonClick(DOWN)) {
- Serial.print(F("\r\nDown"));
- PS4.setLed(Yellow);
- } if (PS4.getButtonPress(LEFT)) {
- Serial.print(F("\r\nLeft"));
- }
- if (PS4.getButtonClick(L1))
- Serial.print(F("\r\nL1"));
- if (PS4.getButtonClick(L3))
- Serial.print(F("\r\nL3"));
- if (PS4.getButtonClick(R1))
- Serial.print(F("\r\nR1"));
- if (PS4.getButtonClick(R3))
- Serial.print(F("\r\nR3"));
- if (PS4.getButtonClick(SHARE)){
- Serial.print(F("\r\nShare")); Serial.print(F("\nGyro X: "));
- Serial.print(PS4.getSensor(gX)); Serial.print(F("\tGyro Y: "));
- Serial.print(PS4.getSensor(gY)); Serial.print(F("\tGyro Z: "));
- Serial.print(PS4.getSensor(gZ)); Serial.print(F("\n\r"));
- }
- if (PS4.getButtonClick(TOUCHPAD)) {
- Serial.print(F("\r\nTouchpad"));
- printTouch = !printTouch;
- }
- if (printAngle){ // Print angle calculated using the accelerometer only
- Serial.print(F("\r\nPitch: "));Serial.print(PS4.getAngle(Pitch));
- Serial.print(F("\tRoll: ")); Serial.print(PS4.getAngle(Roll));
- }
- if (printTouch) { // Print the x, y coordinates of the touchpad
- if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
- Serial.print(F("\r\n"));
- for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
- if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
- Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
- Serial.print(PS4.getX(i));
- Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
- Serial.print(PS4.getY(i));
- Serial.print(F("\t"));
- }}}}}}
- // _ _
- // _ __ ___ _ _| |_(_)_ __ ___ ___
- //| '__/ _ \| | | | __| | '_ \ / _ \/ __|
- //| | | (_) | |_| | |_| | | | | __/\__ \
- //|_| \___/ \__,_|\__|_|_| |_|\___||___/ Stuff...
- void routselect(){
- if (PS4.getButtonPress(OPTIONS) && PS4.getButtonClick(LEFT)) {
- Serial.print(F("\n\r"));
- ifunc--;
- if (ifunc < 0)
- ifunc = CNT_PFUNCS - 1;
- printroutine();
- }
- if (PS4.getButtonPress(OPTIONS) && PS4.getButtonClick(RIGHT)) {
- Serial.print(F("\n\r"));
- ifunc++;
- if (ifunc >= CNT_PFUNCS)
- ifunc = 0;
- printroutine();
- }
- if (PS4.getButtonPress(OPTIONS) && PS4.getButtonClick(CIRCLE)) {
- Serial.print(F("\n\r"));
- Serial.print(F("circle hello"));
- enabled = !enabled;
- if (enabled == false){
- digitalWrite(ENABLE_PIN, LOW);
- Serial.print(F("steppers disabled"));
- }
- if (enabled == true){
- Serial.print(F("steppers enabled "));
- digitalWrite(ENABLE_PIN, HIGH);
- }
- }
- if (PS4.getButtonPress(OPTIONS) && PS4.getButtonClick(CROSS)) {
- playing = true;
- Serial.print(F("\n\n"));
- Serial.println("now running:");
- printroutine();
- (*myfuncs[ifunc])();
- }}
- void printroutine(){
- if (myfuncs[ifunc] == &routine1) Serial.print("\n\nfunc1");
- else if(myfuncs[ifunc] == &routine2) Serial.print("\n\nfunc2");
- else if(myfuncs[ifunc] == &routine3) Serial.println("\n\nstepper control");
- else if(myfuncs[ifunc] == &routine4) Serial.print("\n\nfunc4");
- else if(myfuncs[ifunc] == &routine5) Serial.print("\n\nfunc5");
- else if(myfuncs[ifunc] == &routine6) Serial.print("\n\nfunc6");
- else if(myfuncs[ifunc] == &routine7) Serial.print("\n\nfunc7");
- }
- void routine1(){
- Serial.println("running routine1");
- digitalWrite(ENABLE_PIN, LOW);
- while(playing == true){
- stplft();
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("exiting routine1");
- digitalWrite(ENABLE_PIN, HIGH);
- playing= false;
- }
- }
- }
- }
- void routine2(void)
- {
- while(playing == true){
- Serial.print(F("\n\n"));
- Serial.println("routine2");
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("triangloooo");
- playing= false;
- }
- }
- }}
- // _ _ _ __ __
- // ___| |_ ___ _ __ _ __ (_)_ __ __ _ ___| |_ _ _ / _|/ _|
- /// __| __/ _ \ '_ \| '_ \| | '_ \ / _` | / __| __| | | | |_| |_
- //\__ \ || __/ |_) | |_) | | | | | (_| | \__ \ |_| |_| | _| _|
- //|___/\__\___| .__/| .__/|_|_| |_|\__, | |___/\__|\__,_|_| |_|
- // |_| |_| |___/
- void routine3()
- { digitalWrite(ENABLE_PIN, LOW);
- while(playing == true){
- while (PS4.getButtonPress(RIGHT)) {
- stprght();
- Usb.Task();
- } while (PS4.getButtonPress(LEFT)) {
- stplft();
- Usb.Task();
- }
- if(PS4.getButtonClick(UP)) {
- step_speed=step_speed+20;
- if(step_speed > 5000){
- step_speed = 5000;
- }
- Serial.println(step_speed);
- }
- if (PS4.getButtonClick(DOWN)){
- step_speed=step_speed-20;
- if(step_speed < 300){
- step_speed = 300;
- }
- Serial.println(step_speed);
- }
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("exiting stepper control mode");
- playing= false;
- digitalWrite(ENABLE_PIN, HIGH);
- }
- }
- }}
- void stplft(){//stepping stepper motor left
- digitalWrite(DIR_PIN, LOW);
- digitalWrite(STEP_PIN, HIGH);
- delayMicroseconds(step_speed);
- digitalWrite(STEP_PIN, LOW);
- delayMicroseconds(step_speed);
- }
- void stprght(){//stepping stepper motor right
- digitalWrite(DIR_PIN, HIGH);
- digitalWrite(STEP_PIN, HIGH);
- delayMicroseconds(step_speed);
- digitalWrite(STEP_PIN, LOW);
- delayMicroseconds(step_speed);
- }
- void routine4(void)
- { while(playing == true){
- Serial.print(F("\n\n"));
- Serial.println("routine4");
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("triangloooo");
- playing= false;
- }
- }
- }}
- void routine5(void)
- { while(playing == true){
- Serial.print(F("\n\n"));
- Serial.println("routine5");
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("triangloooo");
- playing= false;
- }
- }
- }}
- void routine6(void)
- { while(playing == true){
- Serial.print(F("\n\n"));
- Serial.println("routine6");
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("triangloooo");
- moveToPosition( 800, false);
- playing= false;
- }
- }
- }}
- void routine7(void)
- { while(playing == true){
- Serial.print(F("\n\n"));
- Serial.println("routine7");
- Usb.Task();
- if (PS4.connected()) {
- if (PS4.getButtonClick(TRIANGLE)) {
- Serial.println("triangloooo");
- playing= false;
- }
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement