Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I think this is my installed Padawan 360 sketch with teeces/filthy/adafruit LEDs.... not 100% sure... its been a while..............
// psybertech's modified version 1.61
// BASED OFF Padawan360 Body Code - Mega I2C v1.0
/////////////////////////////////////////////////
//INCLUDES
#include <Adafruit_PWMServoDriver.h>
#include <MP3Trigger.h>
#include <Sabertooth.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <XBOXRECV.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
//DRIVE CONFIG
const int SABERTOOTHBAUDRATE = 9600;
const byte DRIVESPEED1 = 40; // 0-stop, 127-full speed.
const byte DRIVESPEED2 = 80; //Recommend beginner: 50 to 75, experienced: 100 to 127, I like 100.
const byte DRIVESPEED3 = 120; //Set to 0 if you only want 2 speeds.
byte drivespeed = DRIVESPEED1;
const byte TURNSPEED = 70; // Recommend beginner: 40 to 50, experienced: 50 $ up, I like 70 // the higher this number the faster the droid will spin in place, lower - easier to control.
const byte RAMPING = 5; // Ramping- the lower this number the longer R2 will take to speedup or slow down,// change this by incriments of 1
Sabertooth Sabertooth2x(128, Serial1);
Sabertooth Syren10(128, Serial2);
//DOME CONFIG
const int DOMEBAUDRATE = 2400;
const byte DOMESPEED = 90; // Use a number up to 127 for serial
int turnDirection = 20; //How much the dome may turn during automation.
//CONTROLLER L&R DEADZONE CONFIG
const byte DOMEDEADZONERANGE = 20; // DOMEDEADZONERANGE for the left stick
const byte DRIVEDEADZONERANGE = 20; // DRIVEDEADZONERANGE for the right stick
//SOUND CONFIG
byte vol = 75; // 0 = full volume, 255 off
byte automateDelay = random(10, 30); // set this to min and max seconds between sounds
//FLTHY HPS CONFIG
#define FlthyTXPin 13
#define FlthyRXPin 12
const int FlthyBAUD = 9600;
SoftwareSerial FlthySerial(FlthyRXPin, FlthyTXPin);
const int TeceesMDBAUD = 38400;
//SERVOS CONFIG
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // 0x40 default
#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 60
int pulseWidth(int angle){
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
Serial.println(analog_value);
return analog_value;
}
uint8_t servonum_hinge1 = 0;
const int H1L1A = 0;
const int H1L2A = 90;
const int H1R1A = 120;
uint8_t servonum_hinge2 = 1;
const int H2L1A = 0;
const int H2L2A = 90;
const int H2R1A = 120;
//INITS
boolean firstLoadOnConnect = false;
boolean isDriveEnabled = false; // 0 = drive motors off ( right stick disabled ) at start
boolean isHPOn = false;
boolean isInAutomationMode = false;
byte automateAction = 0; // Action number used to randomly choose a sound effect or a dome turn
int domeThrottle = 0;
int driveThrottle = 0;
int rightStick = 0;
int turnThrottle = 0;
unsigned long automateMillis = 0;
MP3Trigger mp3Trigger;
USB Usb;
XBOXRECV Xbox(&Usb);
/////////////////////////////////// SETUP ///////////////////////////////////
void setup() {
Serial1.begin(SABERTOOTHBAUDRATE);
Serial2.begin(DOMEBAUDRATE);
FlthySerial.begin(FlthyBAUD);
FlthySerial.print("S9\r\n");
Serial3.begin(TeceesMDBAUD);
pwm.begin(); //SERVOS
pwm.setPWMFreq(FREQUENCY); //SERVOS
#if defined(SYRENSIMPLE)
Syren10.motor(0);
#else
Syren10.autobaud();
#endif
Sabertooth2x.autobaud();
Sabertooth2x.drive(0);
Sabertooth2x.turn(0);
Sabertooth2x.setTimeout(950);
Syren10.setTimeout(950);
mp3Trigger.setup();
mp3Trigger.setVolume(vol);
Wire.begin(); // Start I2C Bus. The body is the master.
//Serial.begin(115200); // WHY IS THIS REMMED?
while (!Serial);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); // HALT
}
mp3Trigger.trigger(255);
Serial.print(F("\r\nSETUP PASSED\r\nLETS LOOP!"));
}
/////////////////////////////////// LOOP ///////////////////////////////////
void loop() {
Usb.Task();
if (!Xbox.XboxReceiverConnected || !Xbox.Xbox360Connected[0]) {
Sabertooth2x.drive(0);
Sabertooth2x.turn(0);
Syren10.motor(1, 0);
firstLoadOnConnect = false;
return;
}
if (!firstLoadOnConnect) {
firstLoadOnConnect = true;
mp3Trigger.play(21);
Xbox.setLedMode(ROTATING, 0);
}
if (Xbox.getButtonClick(XBOX, 0)) {
if(Xbox.getButtonPress(L1, 0) && Xbox.getButtonPress(R1, 0)){
Xbox.disconnect(0);
}
}
if (Xbox.getButtonClick(START, 0)) {
if (isDriveEnabled) {
isDriveEnabled = false;
Xbox.setLedMode(ROTATING, 0);
mp3Trigger.play(53);
} else {
isDriveEnabled = true;
mp3Trigger.play(52);
if (drivespeed == DRIVESPEED1) {
Xbox.setLedOn(LED1, 0);
} else if (drivespeed == DRIVESPEED2 && (DRIVESPEED3 != 0)) {
Xbox.setLedOn(LED2, 0);
} else {
Xbox.setLedOn(LED3, 0);
}
}
}
if (Xbox.getButtonClick(BACK, 0)) {
if (isInAutomationMode) {
isInAutomationMode = false;
automateAction = 0;
mp3Trigger.play(53);
} else {
isInAutomationMode = true;
mp3Trigger.play(52);
}
}
if (isInAutomationMode) {
unsigned long currentMillis = millis();
if (currentMillis - automateMillis > (automateDelay * 1000)) {
automateMillis = millis();
automateAction = random(1, 5);
if (automateAction > 1) {
mp3Trigger.play(random(1,45));
}
if (automateAction < 4) {
#if defined(SYRENSIMPLE)
Syren10.motor(turnDirection);
#else
Syren10.motor(1, turnDirection);
#endif
delay(750);
#if defined(SYRENSIMPLE)
Syren10.motor(0);
#else
Syren10.motor(1, 0);
#endif
if (turnDirection > 0) {
turnDirection = -45;
} else {
turnDirection = 45;
}
}
automateDelay = random(3,10);
}
}
if (Xbox.getButtonClick(A, 0)) {
if (Xbox.getButtonPress(L1, 0)) {
mp3Trigger.trigger(184);
Serial3.print("0T4\r");
FlthySerial.print("A0051\r"); // HPEvent - SystemFailure
pwm.setPWM(servonum_hinge1, 0, pulseWidth(H1L1A)); //OPEN
pwm.setPWM(servonum_hinge2, 0, pulseWidth(H2L1A)); //OPEN
} else if (Xbox.getButtonPress(L2, 0)) {
mp3Trigger.trigger(127);
Serial3.print("0T3\r"); //logic lights, alarm
FlthySerial.print("A0051\r"); // HPEvent alarm
pwm.setPWM(servonum_hinge1, 0, pulseWidth(H1L2A)); //CLOSE
pwm.setPWM(servonum_hinge2, 0, pulseWidth(H2L2A)); //CLOSE
} else if (Xbox.getButtonPress(R1, 0)) {
mp3Trigger.trigger(191);
Serial3.print("0T92\r"); //logic lights, alarm
FlthySerial.print("A0020\r");
} else if (Xbox.getButtonPress(R2, 0)) {
} else {
mp3Trigger.play(random(20,39));
Serial3.print("0T1\r"); //logic lights, random
}
}
if (Xbox.getButtonClick(B, 0)) {
if (Xbox.getButtonPress(L1, 0)) {
mp3Trigger.play(7);
Serial3.print("0T1\r"); //logic lights, random
} else if (Xbox.getButtonPress(L2, 0)) {
mp3Trigger.play(3);
Serial3.print("0T1\r"); //logic lights, random
} else if (Xbox.getButtonPress(R1, 0)) {
mp3Trigger.play(58);
Serial3.print("0T1\r"); //logic lights, random
FlthySerial.print("A007\r"); // HPEvent - Disco
} else if (Xbox.getButtonPress(R2, 0)) {
} else {
mp3Trigger.play(random(38, 45));
Serial3.print("0T1\r"); //logic lights, random
}
}
if (Xbox.getButtonClick(X, 0)) {
if (Xbox.getButtonPress(L1, 0)) {
mp3Trigger.trigger(151); // leia message L1+X
Serial3.print("0T11\r"); //logic lights, leia message
FlthySerial.print("F001\r"); // Front HPEvent leia message
} else if (Xbox.getButtonPress(L2, 0)) {
mp3Trigger.play(4);
Serial3.print("0T10\r"); //logic lights
} else if (Xbox.getButtonPress(R1, 0)) {
mp3Trigger.play(12);
Serial3.print("0T1\r"); //logic lights, random
} else if (Xbox.getButtonPress(R2, 0)) {
} else {
mp3Trigger.play(random(45,49));
Serial3.print("0T1\r"); //logic lights, random
}
}
if (Xbox.getButtonClick(Y, 0)) {
if (Xbox.getButtonPress(L1, 0)) {
mp3Trigger.play(8);
Serial3.print("0T1\r"); //logic lights, random
} else if (Xbox.getButtonPress(L2, 0)) {
mp3Trigger.play(2);
Serial3.print("0T1\r"); //logic lights, random
} else if (Xbox.getButtonPress(R1, 0)) {
mp3Trigger.play(9);
Serial3.print("0T1\r"); //logic lights, random
} else if (Xbox.getButtonPress(R2, 0)) {
} else {
mp3Trigger.play(random(52,55));
Serial3.print("0T1\r"); //logic lights, random
}
}
if (Xbox.getButtonClick(UP, 0)) {
if (Xbox.getButtonPress(L1, 0)) {
// Logic display brightness.
}
else if (Xbox.getButtonPress(R1, 0)) {
if (vol > 0) {
vol--;
mp3Trigger.setVolume(vol);
}
}
}
if (Xbox.getButtonClick(DOWN, 0)) {
if (Xbox.getButtonPress(L1, 0)) {
// Logic display brightness.
}
else if (Xbox.getButtonPress(R1, 0)) {
if (vol < 255) {
vol++;
mp3Trigger.setVolume(vol);
}
}
}
if (Xbox.getButtonClick(LEFT, 0)) {
if (Xbox.getButtonPress(R2, 0)) {
mp3Trigger.reverse();
}
}
if (Xbox.getButtonClick(RIGHT, 0)) {
if (Xbox.getButtonPress(R2, 0)) {
mp3Trigger.forward();
}
}
if (Xbox.getButtonClick(L3, 0)) {
if (isHPOn) {
isHPOn = false;
FlthySerial.print("S8\r"); // if hp light is on, turn it off
} else {
isHPOn = true;
FlthySerial.print("S9\r");
}
}
if (Xbox.getButtonClick(R3, 0) && isDriveEnabled) {
if (drivespeed == DRIVESPEED1) {
drivespeed = DRIVESPEED2;
Xbox.setLedOn(LED2, 0);
mp3Trigger.play(52);
Serial3.print("0T3\r");
} else if (drivespeed == DRIVESPEED2 && (DRIVESPEED3 != 0)) {
drivespeed = DRIVESPEED3;
Xbox.setLedOn(LED3, 0);
mp3Trigger.play(53);
Serial3.print("0T4\r");
} else {
drivespeed = DRIVESPEED1;
Xbox.setLedOn(LED1, 0);
mp3Trigger.play(54);
Serial3.print("0T1\r");
}
}
rightStick = (map(Xbox.getAnalogHat(RightHatY, 0), -32768, 32767, -drivespeed, drivespeed));
if (rightStick > -DRIVEDEADZONERANGE && rightStick < DRIVEDEADZONERANGE) {
driveThrottle = 0; // stick is in dead zone - don't drive
} else {
if (driveThrottle < rightStick) {
if (rightStick - driveThrottle < (RAMPING + 1) ) {
driveThrottle += RAMPING;
} else {
driveThrottle = rightStick;
}
} else if (driveThrottle > rightStick) {
if (driveThrottle - rightStick < (RAMPING + 1) ) {
driveThrottle -= RAMPING;
} else {
driveThrottle = rightStick;
}
}
}
turnThrottle = map(Xbox.getAnalogHat(RightHatX, 0), -32768, 32767, -TURNSPEED, TURNSPEED);
if (isDriveEnabled) {
if (turnThrottle > -DRIVEDEADZONERANGE && turnThrottle < DRIVEDEADZONERANGE) {
turnThrottle = 0;
}
Sabertooth2x.turn(-turnThrottle);
Sabertooth2x.drive(driveThrottle);
}
domeThrottle = (map(Xbox.getAnalogHat(LeftHatX, 0), -32768, 32767, DOMESPEED, -DOMESPEED));
if (domeThrottle > -DOMEDEADZONERANGE && domeThrottle < DOMEDEADZONERANGE) {
domeThrottle = 0; //stick in dead zone - don't spin dome
}
Syren10.motor(1, domeThrottle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment