Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // =======================================================================================
- // /////////////////////////Padawan Body Code v1.9X //////////////////////////////////////
- // =======================================================================================
- // danf
- // Revised Date: 10/21/13 (unofficial revision by PaulM)
- //
- // Designed to be used with a second Arduino running the Padawan Dome code
- // EasyTransfer and PS2X_lib libraries by Bill Porter
- //
- //
- // Set Sabertooth 2x25 Dip Switches 1 and 2 Down, All Others Up
- // For SyRen Packetized Serial Set Switches 1 and 2 Down, All Others Up
- // For SyRen Simple Serial Set Switchs 2 & 4 Down, All Others Up
- // Placed a 10K ohm resistor between S1 & GND on the SyRen 10 itself
- //
- // (if using Lithium batteries set switch 3 Down on SyRen & Sabertooth 2x25)
- //
- // Arduino Pins: BotBoarduino Pins:
- // D01 = MP3 Trigger RX* D01 = MP3 Trigger RX*
- // D05 = Syren10 S1** D06 = PS2 DAT
- // D06 = Sabertooth S2*** D07 = PS2 COM
- // D07 = Sabertooth S1 D08 = PS2 ATT
- // D08 = Dome RXI D09 = PS2 CLK
- // D10 = PS2 ATT D10 = Dome RXI
- // D11 = PS2 COM D11 = Syren10 S1**
- // D12 = PS2 DAT D12 = Sabertooth S2***
- // D13 = PS2 CLK D13 = Sabertooth S1
- // A4 = LCD DAT A4 = LCD DAT
- // A5 = LCD CLK A5 = LCD CLK
- // D05 = used by internal speaker
- // *If using MP3 Trigger in mode 1, connect MP3 Trigger RX to D03 instead
- // **If using SyRen in mode 3, connect Syren S1 to D01 instead
- // ***S2 is not needed for Sabertooth's serial mode
- //
- //************************** Set speed and turn here************************************//
- byte drivespeed = 75; // 0-stop, 127-full speed.
- // Recommend beginner: 50 to 75, experienced: 100 to 127, I like 100
- byte turnspeed = 45; // the higher this number the faster it will spin in place, lower - easier to controll.
- // Recommend beginner: 40 to 50, experienced: 50 $ up, I like 75
- byte ramping = 5; // Ramping- the lower this number the longer R2 will take to speedup or slow down,
- // change this by incriments of 1
- #define MP3TRIGGERMODE 1 // 0 for regular mode (MP3 Tirgger RX connects to Pin 1)
- // 1 for Software Serial mode (MP3 Trigger connects to Pin 3)
- #define SYRENMODE 3 // 1=Simple Serial , 2=Packetized Software Serial (recommended), 3=Packetized Hardware Serial (S1 connects to Pin 1)
- // Do not use Simple Serial mode in close contact with people.
- // NOTE : you can't use MP3TRIGGERMODE 0 with SYRENMODE 3, since they both use Arduino Pin 1
- int domeBaudeRate = 9600; // Set the Serial baude rate for the Syren motor controller
- // for packetized options are: 2400, 9600 (default), 19200 or 38400
- // for simple use 9600 .
- byte domecompensation = 2; // For controllers that centering problems, causing slight dome drift in one direction
- // use the lowest number with no drift
- byte domespeed = 110; // If using a speed controller for the dome, sets the top speed
- // Use a number up to 127
- #define VOLJUMP 2 // Speed at which volume is adjusted (1=slowest 5=fastest)
- #define VOLLIMIT 100 // Quietest volume can be set to (0-255, higher=quieter, over 64 is pretty quiet)
- #define LCD 0 // Connect to LCD via i2c (pins A4 & A5), default address #0 (A0-A2 not jumpered)
- // Backlight can be toggled on/off using R1 + Pad Left
- // (comment out if no LCD is connected)
- #define AUTOMIN 5 // Minimum seconds between sounds in auto mode
- #define AUTOMAX 20 // Maximum seconds between sounds in auto mode
- #define BOARDTYPE 1 // 0 for regular Arduino, 1 for BotBoarduino (uses different PS2, ST & dome pins)
- ///////include libs and declare variables////////////////////////////
- #include <Sabertooth.h>
- #if (SYRENMODE==3)
- Sabertooth SyR(128);
- #endif
- #include <SoftwareSerial.h>
- #if (SYRENMODE==1)
- #include <SyRenSimplified.h>
- #endif
- #include <PS2X_lib.h> //for v1.7
- #include <Servo.h>
- #include <MP3Trigger.h>
- #include <SoftEasyTransfer.h>
- #if defined(LCD)
- #include <Wire.h>
- #include <LiquidTWI.h> //this is the speedy i2C LCD library - get it here: http://forums.adafruit.com/viewtopic.php?f=19&t=21586
- LiquidTWI lcd(LCD);
- #endif
- //////////////////////////////////////////////////////////////////
- #if (MP3TRIGGERMODE==1)
- SoftwareSerial trigSerial = SoftwareSerial(2, 3);
- #endif
- MP3Trigger trigger;
- #if (BOARDTYPE==0)
- #if (SYRENMODE<3)
- SoftwareSerial SyRSerial(2, 5);
- #endif
- SoftwareSerial domeSerial(2, 8);
- SoftwareSerial STSerial(2, 7);
- #else
- #if (SYRENMODE<3)
- SoftwareSerial SyRSerial(2, 11);
- #endif
- SoftwareSerial domeSerial(2, 10);
- SoftwareSerial STSerial(2, 13);
- #endif
- /////////////////////////////////////////////////////////////////
- Sabertooth ST(128, STSerial);
- #if (SYRENMODE==1)
- SyRenSimplified SyR(SyRSerial); // Use SWSerial as the serial port.
- #elif (SYRENMODE==2)
- Sabertooth SyR(128, SyRSerial);
- #endif
- SoftEasyTransfer ET;//create object
- #include <Servo.h>
- struct SEND_DATA_STRUCTURE{
- //put your variable definitions here for the data you want to send
- //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
- int hpx; // hp movement
- int hpy; // hp movement
- int hpl; // hp light
- int hpa; // hp automation
- int dsp; // 0 = random, 1 = alarm, 5 = leia, 11 = alarm2, 100 = no change
- };
- SEND_DATA_STRUCTURE domeData;//give a name to the group of data
- PS2X ps2x; // create PS2 Controller Class
- int error = 0; // part of the ps2x lib
- byte type = 0; // part of the ps2x lib
- byte vibrate = 0; // part of the ps2x lib
- byte vol = 40; // 0 = full volume, 64+ = inaudible, 255 = off
- byte drive = 0; // 0 = drive motors off ( right stick disabled )
- byte automate = 0;
- unsigned long automateMillis = 0;
- byte automateDelay = random(5,20);// set this to min and max seconds between sounds
- int turnDirection = 20;
- byte action = 0;
- unsigned long DriveMillis = 0;
- int drivenum = 0;
- int sticknum = 0;
- int domenum = 0;
- int domenumset = 0; //used for ramping in servo mode
- int domeramping=(ramping*4);
- int turnnum = 0;
- #if defined(LCD)
- byte lcdlight = 0; //used to toggle the LCD backlight on and off
- #endif
- //////////////set up run once//////////////////////////////////////////
- void setup(){
- #if defined(LCD)
- lcd.begin(16, 2);// sets up the LCD's number of rows and columns
- lcd.clear();
- lcd.setBacklight(LOW);
- delay(200);
- lcd.setBacklight(HIGH);
- delay(200);
- lcd.print("Starting up."); // Print a message to the LCD.
- lcd.setBacklight(LOW);
- delay(100);
- lcd.setBacklight(HIGH);
- #endif
- #if(SYRENMODE<3)
- SyRSerial.begin(domeBaudeRate);
- #endif
- #if(SYRENMODE==1)
- SyR.motor(0);
- #elif(SYRENMODE==2)
- SyR.autobaud();
- #elif(SYRENMODE==3)
- SabertoothTXPinSerial.begin(domeBaudeRate);
- SyR.autobaud();
- #endif
- STSerial.begin(9600); // 9600 is the default baud rate for Sabertooth packet serial.
- ST.autobaud(); // Send the autobaud command to the Sabertooth controller(s).
- // NOTE: *Not all* Sabertooth controllers need this command.
- // It doesn't hurt anything, but V2 controllers use an
- // EEPROM setting (changeable with the function setBaudRate) to set
- // the baud rate instead of detecting with autobaud.
- //
- // If you have a 2x12, 2x25 V2, 2x60 or SyRen 50, you can remove
- // the autobaud line and save yourself two seconds of startup delay.
- ST.drive(0); // The Sabertooth won't act on mixed mode packet serial commands until
- ST.turn(0); // it has received power levels for BOTH throttle and turning, since it
- // mixes the two together to get diff-drive power levels for both motors.
- #if (MP3TRIGGERMODE==1)
- trigger.setup(&trigSerial);
- trigSerial.begin( MP3Trigger::serialRate() );
- #else
- trigger.setup(&Serial);
- #endif
- #if (BOARDTYPE==0)
- error = ps2x.config_gamepad(13,11,10,12, true, false); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
- #else
- error = ps2x.config_gamepad(9,7,8,6, true, false); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
- #endif
- trigger.setVolume(vol);//anything with trigger. is for the mp3trigger
- type = ps2x.readType();
- domeSerial.begin(57600);//start the library, pass in the data details and the name of the serial port.
- ET.begin(details(domeData), &domeSerial);
- #if defined(LCD)
- lcd.clear();
- lcd.print("PADAWAN"); // Print a message to the LCD.
- delay(250);
- lcd.setBacklight(LOW);
- #endif
- }
- ///////function to play sound (also outputs to LCD if connected)////////////////
- void playSound(int soundNumber) {
- #if defined(LCD)
- lcd.setCursor(0, 0);
- lcd.print("Sound "+String(soundNumber)+" ");
- #endif
- trigger.play(soundNumber);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- ///////Loop run over and over//////////////////////////////////////
- //////////////////////////////////////////////////////////////////
- void loop(){
- if(error == 1) //skip loop if no controller found
- return;
- ps2x.read_gamepad(); //read controller and set large motor to spin at 'vibrate' speed
- //// enable / disable right stick & play sound
- if(ps2x.ButtonPressed(PSB_START))
- {
- if (drive<1)
- {
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print("-Drive Enabled-");
- #endif
- drive = 1;
- playSound(52);
- }
- else {
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print("-Drive Disabled-");
- #endif
- drive = 0;
- playSound(53);
- }
- }
- ////turn hp automation or automate on & off
- if(ps2x.ButtonPressed(PSB_SELECT)) {
- if(ps2x.Button(PSB_R2)) {
- if(domeData.hpa == 1) {
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print(" -Auto HP OFF- ");
- #endif
- domeData.hpa = 0;
- domeData.dsp = 100;
- ET.sendData();
- }
- else {//if hp automation is off, turn it on
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print(" -Auto HP ON- ");
- #endif
- domeData.hpa = 1;
- domeData.dsp = 100;
- ET.sendData();
- }//turn hp automation off
- }
- /////////////automate
- else {
- if (automate<1) {
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print(" -Automate ON- ");
- #endif
- automate = 1;
- playSound(52);
- }
- else {
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print(" -Automate OFF- ");
- #endif
- automate = 0;
- action = 0;
- playSound(53);
- }
- }
- }
- if (automate == 1)
- {
- if (ps2x.Analog(PSS_LX)!=128)
- {
- automateMillis = millis();
- }
- unsigned long currentMillis = millis();
- if (currentMillis - automateMillis > (automateDelay*1000))
- {
- automateMillis = millis();
- action = random(1,5);
- if (action>1)
- {
- (playSound(random(32,52)));
- }
- if (action<4)
- {
- #if (SYRENMODE==1)
- SyR.motor(turnDirection);
- #elif(SYRENMODE>=2)
- SyR.motor(1,turnDirection);
- #endif
- delay(750);
- #if(SYRENMODE==1)
- SyR.motor(0);
- #elif(SYRENMODE>=2)
- SyR.motor(1,0);
- #endif
- if (turnDirection>0)
- {
- turnDirection = -49;
- }
- else
- {
- turnDirection = 45;
- }
- }
- automateDelay = random(AUTOMIN,AUTOMAX);// set this to min and max seconds between sounds
- }
- }
- ////volume control
- if(ps2x.Button(PSB_PAD_UP))
- {
- if(ps2x.Button(PSB_R1))
- {
- if (vol>0)
- {
- vol=vol-VOLJUMP;
- if(vol<0) vol=0;
- #if defined(LCD)
- lcd.setCursor(9, 0);
- lcd.print("Vol "+String(vol)+" ");
- #endif
- trigger.setVolume(vol);
- }// volume up
- }
- }
- if(ps2x.Button(PSB_PAD_DOWN))
- {
- if(ps2x.Button(PSB_R1))
- {
- if (vol<VOLLIMIT)
- {
- vol=vol+VOLJUMP;
- if(vol>VOLLIMIT) vol=VOLLIMIT;
- #if defined(LCD)
- lcd.setCursor(9, 0);
- lcd.print("Vol "+String(vol)+" ");
- #endif
- trigger.setVolume(vol);
- }//volume down
- }
- }
- ////LCD backlight toggle on/off
- #if defined(LCD)
- if(ps2x.Button(PSB_PAD_LEFT))
- {
- if(ps2x.Button(PSB_R1))
- {
- if (lcdlight==0)
- { //turn on backlight
- lcd.setBacklight(HIGH);
- lcdlight=1;
- }
- else
- { //turn off backlight
- lcd.setBacklight(LOW);
- lcdlight=0;
- }
- }
- }
- #endif
- /////hp movement
- if(ps2x.ButtonPressed(PSB_PAD_UP))
- {
- if (!(ps2x.Button(PSB_R1)))
- {
- domeData.hpy = 30;
- domeData.dsp = 100;
- ET.sendData();
- }
- }
- if(ps2x.ButtonPressed(PSB_PAD_DOWN))
- {
- if (!(ps2x.Button(PSB_R1)))
- {
- domeData.hpy = 150;
- domeData.dsp = 100;
- ET.sendData();
- }
- }
- if(ps2x.ButtonReleased(PSB_PAD_UP)||ps2x.ButtonReleased(PSB_PAD_DOWN))
- {
- domeData.hpy = 0;
- domeData.dsp = 100;
- ET.sendData();
- }
- if(ps2x.ButtonPressed(PSB_PAD_RIGHT))
- {
- domeData.hpx = 150;
- domeData.dsp = 100;
- ET.sendData();
- }
- if(ps2x.ButtonPressed(PSB_PAD_LEFT))
- {
- domeData.hpx = 30;
- domeData.dsp = 100;
- ET.sendData();
- }
- if(ps2x.ButtonReleased(PSB_PAD_RIGHT)||ps2x.ButtonReleased(PSB_PAD_LEFT))
- {
- domeData.hpx = 0;
- domeData.dsp = 100;
- ET.sendData();
- }
- /////play sounds and change display
- if(ps2x.ButtonPressed(PSB_GREEN))//triangle top
- {
- if(ps2x.Button(PSB_L1))
- {
- (playSound(8));
- domeData.dsp = 0;
- ET.sendData();
- }
- else if(ps2x.Button(PSB_L2))
- {
- (playSound(2));
- domeData.dsp = 0;
- ET.sendData();
- }
- else if(ps2x.Button(PSB_R1))
- {
- (playSound(9));
- domeData.dsp = 0;
- ET.sendData();
- }
- else
- {
- (playSound(random(13,17)));
- domeData.dsp = 0;
- ET.sendData();
- }
- }
- if(ps2x.ButtonPressed(PSB_BLUE))//x bottom
- {
- if(ps2x.Button(PSB_L1))
- {
- (playSound(6));
- domeData.dsp = 0;
- ET.sendData();
- }
- else if(ps2x.Button(PSB_L2))
- {
- (playSound(1));
- domeData.dsp = 1;
- ET.sendData();
- domeData.dsp = 0;
- }
- else if(ps2x.Button(PSB_R1))
- {
- (playSound(11));
- domeData.dsp = 11;
- ET.sendData();
- domeData.dsp = 0;
- }
- else
- {
- (playSound(random(17,25)));
- domeData.dsp = 0;
- ET.sendData();
- }
- }
- if(ps2x.ButtonPressed(PSB_RED))//circle right
- {
- if(ps2x.Button(PSB_L1))
- {
- (playSound(7));
- domeData.dsp = 0;
- ET.sendData();
- }
- else if(ps2x.Button(PSB_L2))
- {
- (playSound(3));
- domeData.dsp = 0;
- ET.sendData();
- }
- else if(ps2x.Button(PSB_R1))
- {
- (playSound(10));
- domeData.dsp = 0;
- ET.sendData();
- }
- else
- {
- (playSound(random(32,52)));
- domeData.dsp = 0;
- ET.sendData();
- }
- }
- if(ps2x.ButtonPressed(PSB_PINK))//square left
- {
- if(ps2x.Button(PSB_L1))
- {
- (playSound(5));
- domeData.dsp = 5;
- ET.sendData();
- domeData.dsp = 0;
- }
- else if(ps2x.Button(PSB_L2))
- {
- (playSound(4));
- domeData.dsp = 0;
- ET.sendData();
- }
- else if(ps2x.Button(PSB_R1))
- {
- (playSound(12));
- domeData.dsp = 0;
- ET.sendData();
- }
- else
- {
- (playSound(random(25,32)));
- domeData.dsp = 0;
- ET.sendData();
- }
- }
- ////turn hp light on & off
- if(ps2x.ButtonPressed(PSB_L3)) //left joystick
- {
- if(domeData.hpl == 1)
- {
- domeData.hpl = 0;
- domeData.dsp = 100;
- ET.sendData();
- }//if hp light is on, turn it off
- else
- {
- domeData.hpl = 1;
- domeData.dsp = 100;
- ET.sendData();
- }//turn hp light on
- }
- /////foot drives
- /////////////////new stuff//////////////////
- sticknum = (map(ps2x.Analog(PSS_RY), 0, 255, -drivespeed, drivespeed));
- if (drivenum < sticknum)
- {
- if (sticknum-drivenum<(ramping+1))
- drivenum+=ramping;
- else
- drivenum = sticknum;
- }
- else if (drivenum > sticknum)
- {
- if (drivenum-sticknum<(ramping+1))
- drivenum-=ramping;
- else
- drivenum = sticknum;
- }
- turnnum = (ps2x.Analog(PSS_RX));
- if (turnnum <= 200 && turnnum >= 54)
- turnnum = (map(ps2x.Analog(PSS_RX), 54, 200, -(turnspeed/3), (turnspeed/3)));
- else if (turnnum > 200)
- turnnum = (map(ps2x.Analog(PSS_RX), 201, 255, turnspeed/3, turnspeed));
- else if (turnnum < 54)
- turnnum = (map(ps2x.Analog(PSS_RX), 0, 53, -turnspeed, -(turnspeed/3)));
- //////////////////////////////////
- if (drive == 1)// right stick (drive)
- {
- #if defined(LCD)
- /*lcd.setCursor(6, 1);
- lcd.print("Tu"+String(turnnum)+" Dr"+String(drivenum)+" ");*/
- #endif
- ST.turn(turnnum);
- ST.drive(drivenum);
- }
- /////dome drive
- domenum = (map(ps2x.Analog(PSS_LX), 0, 255, -domespeed, domespeed));
- if (domenum > domecompensation && domenum < domecompensation) domenum = 0;
- #if defined(LCD)
- lcd.setCursor(0, 1);
- lcd.print("Sy"+String(domenum)+" ");
- #endif
- #if(SYRENMODE==1)
- SyR.motor(domenum);
- #elif(SYRENMODE>=2)
- SyR.motor(1,domenum);
- #endif
- ////////////
- delay(50);
- }
- /////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement