
But when you get it working please share what you got so others can use it. If it's ok for you ofc.
I think the control and software part is the most difficult for most of us
Will do.olegodo wrote:Thanks for the offer! I really appreciate it, but I am focusing on Moki's solution now. All components have arrived and waiting to get connected![]()
But when you get it working please share what you got so others can use it. If it's ok for you ofc.
I think the control and software part is the most difficult for most of us
Code: Select all
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
This sketch creates a fun motor party on your desk *whiirrr*
Connect a unipolar/bipolar stepper to M3/M4
Connect a DC motor to M1
Connect a hobby servo to SERVO1
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Servo.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// And connect a DC motor to port M1
Adafruit_DCMotor *rightMotor = AFMS.getMotor(1);
Adafruit_DCMotor *leftMotor = AFMS.getMotor(2);
// We'll also test out the built in Arduino Servo library
Servo servo1;
/*
Example sketch for the Xbox 360 USB library - developed by Kristian Lauszus
For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com
*/
#include <XBOXUSB.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
XBOXUSB Xbox(&Usb);
void setup() {
Serial.begin(115200);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Attach a servo to pin #10
//servo1.attach(10);
#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\nXBOX USB Init did not start"));
while (1); //halt
}
Serial.print(F("\r\nXBOX USB Library Started"));
}
void loop() {
Usb.Task();
int DeadZone=500;
if (Xbox.Xbox360Connected) {
/*
if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) {
Serial.print("L2: ");
Serial.print(Xbox.getButtonPress(L2));
Serial.print("\tR2: ");
Serial.println(Xbox.getButtonPress(R2));
Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2));
} else
Xbox.setRumbleOff();
*/
// Xbox.setRumbleOn(0, 0);
/*if (Xbox.getAnalogHat(RightHatX) == 0){
servo1.write(90);
}
if (Xbox.getAnalogHat(RightHatX)>0){
servo1.write(map(Xbox.getAnalogHat(RightHatX), 1, 32767, 91, 180));
}
if (Xbox.getAnalogHat(RightHatX)<0){
servo1.write(map(Xbox.getAnalogHat(RightHatX), -32767,-1, 0, 89));
}*/
// if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) {
//if (Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) {
//leftMotor->run(FORWARD);
// leftMotor->setSpeed(i);
// rightMotor->run(FORWARD);
// rightMotor->setSpeed(i);
//}
// left hat is for motors, right hat is for steering/servo for now
int j;
if ((Xbox.getAnalogHat(LeftHatY) <= DeadZone) && (Xbox.getAnalogHat(LeftHatY) >= (DeadZone*-1)))
{
leftMotor->run(RELEASE);
rightMotor->run(RELEASE);
}
if (Xbox.getAnalogHat(LeftHatY) > DeadZone)
{
// map(Xbox.getAnalogHat(RightHatX), -32767, 32767, 0, 180)
j=(map(Xbox.getAnalogHat(LeftHatY),DeadZone+1,32767,1,255));
leftMotor->setSpeed(j);
rightMotor->setSpeed(j);
leftMotor->run(BACKWARD);
rightMotor->run(BACKWARD);
//leftMotor->run(RELEASE);
Serial.print(F("LeftHatY also : "));
Serial.print(j);
Serial.print("\t");
}
if (Xbox.getAnalogHat(LeftHatY) < (-1*DeadZone))
{
// map(Xbox.getAnalogHat(RightHatX), -32767, 32767, 0, 180)
// turn negative into positive, but reverse motor direction
int k=(Xbox.getAnalogHat(LeftHatY)*(-1));
j=(map(k,DeadZone+1,32767,1,255));
leftMotor->setSpeed(j);
leftMotor->run(FORWARD);
rightMotor->setSpeed(j);
rightMotor->run(FORWARD);
//leftMotor->run(RELEASE);
Serial.print(F("LeftHatY also : "));
Serial.print(j);
Serial.print("\t");
}
// right hat is for steering*********************
int r;
if ((Xbox.getAnalogHat(RightHatX) <= DeadZone) && (Xbox.getAnalogHat(RightHatX) >= (DeadZone*-1)))
{
servo1.write(90);
}
if (Xbox.getAnalogHat(RightHatY) > DeadZone)
{
// map(Xbox.getAnalogHat(RightHatX), -32767, 32767, 0, 180)
servo1.write(map(Xbox.getAnalogHat(RightHatX), DeadZone+1,32767,91,255));
}
if (Xbox.getAnalogHat(RightHatX) < (-1*DeadZone))
{
int k=(Xbox.getAnalogHat(RightHatX)*(-1));
j=(map(k,DeadZone+1,32767,1,89));
servo1.write(j);
}
// ************end of right hat code*************************
//}
/*if (Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500) {
Serial.print(F("LeftHatY: "));
Serial.print(Xbox.getAnalogHat(LeftHatY));
Serial.print("\t");
}*/
/*if (Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500) {
Serial.print(F("RightHatX: "));
Serial.print(Xbox.getAnalogHat(RightHatX));
Serial.print("\t");
servo1.write(map(Xbox.getAnalogHat(RightHatX), -32767, 32767, 0, 180));
}*/
/*
if (Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) {
Serial.print(F("RightHatY: "));
Serial.print(Xbox.getAnalogHat(RightHatY));
}
// Serial.println();
// }
if (Xbox.getButtonClick(UP)) {
Xbox.setLedOn(LED1);
Serial.println(F("Up"));
}
if (Xbox.getButtonClick(DOWN)) {
Xbox.setLedOn(LED4);
Serial.println(F("Down"));
}
if (Xbox.getButtonClick(LEFT)) {
Xbox.setLedOn(LED3);
Serial.println(F("Left"));
}
if (Xbox.getButtonClick(RIGHT)) {
Xbox.setLedOn(LED2);
Serial.println(F("Right"));
}
if (Xbox.getButtonClick(START)) {
Xbox.setLedMode(ALTERNATING);
Serial.println(F("Start"));
}
if (Xbox.getButtonClick(BACK)) {
Xbox.setLedBlink(ALL);
Serial.println(F("Back"));
}
if (Xbox.getButtonClick(L3))
Serial.println(F("L3"));
if (Xbox.getButtonClick(R3))
Serial.println(F("R3"));
if (Xbox.getButtonClick(L1))
Serial.println(F("L1: Left Shoulder"));
if (Xbox.getButtonClick(R1))
Serial.println(F("R1: Right Shoulder"));
if (Xbox.getButtonClick(XBOX)) {
Xbox.setLedMode(ROTATING);
Serial.println(F("Xbox"));
}
if (Xbox.getButtonClick(A))
Serial.println(F("A"));
if (Xbox.getButtonClick(B))
Serial.println(F("B"));
if (Xbox.getButtonClick(X))
Serial.println(F("X"));
if (Xbox.getButtonClick(Y))
Serial.println(F("Y"));
*/
}
delay(1);
}