Jump to content

Talltrooper

Member
  • Posts

    3
  • Joined

  • Last visited

Posts posted by Talltrooper

  1. Hi,

     

    So here is the full layout and code, its still pretty much in beta state, especially everything with the counter needs a rewrite. And not everything planed is supported yet.

    The layout is based on the arduino nano, i used the internal pullups on the arduino board, thats the reason why the whole triggerlogic is inverted, and its missing the trigger resistors.

    I was not able to get a 2GB SD card so i testet a 8GB SD HC and is does the job flawless, even when the soundmodule descriptions told that you are limited to the small ones.

     

    Regards

    Rainer

     

     

     

     

    Schaltplan_zpsqkujb9v7.jpg

    /*
    Project "E-11 Blaster"
    by Evildead
    
    this version is compatible with WT-SD MP3 PLAYER
    
     created  28 Nov 2015
     last modified 12 Dec 2015
    
    This is based on Project "Open Blaster" by TK8177
     
    PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENCE. THE WORK IS PROTECTED BY COPYRIGHT
    AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENCE OR COPYRIGHT
    LAW IS PROHIBITED. BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND
    BY THE TERMS OF THIS LICENCE. THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF
    YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
    
    Attribution-NonCommercial
    
    You are free:
        to copy, distribute, display, and perform the work
        to make derivative works
    
    Under the following conditions:
        Attribution — You must give the original author credit.
        Non-Commercial — You may not use this work for commercial purposes
     
     */
     
    
    
    // this constant won't change:
    const int SelectorSwitch    = 6;    // WEAPON SELECTOR
    const int TriggerSwitch     = 7;    // FIRE
    const int ReloadSwitch      = 8;    // WEAPON RELOAD    
    const int LedFireRed        = 10;   // LED FIRE (RED)
    const int LedFireBlue       = 9;    // LED FIRE (BLUE)
    const int LedFireGreen      = 11;   // LED FIRE (GREEN)
    const int LedCounterG10     = 2;    // Green Counter Green 10
    const int LedCounterG9      = 3;    // Green Counter Green 9
    const int LedCounterG8      = 4;    // Green Counter Green 8
    const int LedCounterG7      = 5;    // Green Counter Green 7
    const int LedCounterG6      = A5;   // Green Counter Green 6
    const int LedCounterG5      = A4;   // Green Counter Green 5
    const int LedCounterG4      = A3;   // Green Counter Green 4
    const int LedCounterR3      = A2;   // Green Counter Red 3
    const int LedCounterR2      = A1;   // Green Counter Red 2
    const int LedCounterR1      = A0;   // Green Counter Red 1
    
                            //Weapon Settings
    
    const int Max_Weapons                    = 4;    // Defines Maximum Weapons
    const int Max_Random_Sound_Arrays        = 2;    // Defines Maximum Weapons
    const int Max_Random_Sounds              = 13;   // Defines Maximum Random Sounds
    
    // Variables will change:
    unsigned long SelectorSwitchHoldT;
    boolean AllowSelect = false;
    int RadioHoldDelay = 3000;                      // Sets the hold delay of Selector Switch for Radio Mode
    int SelectorState = HIGH;
    int SelectorSwitch_LastState = HIGH;
    int TriggerSwitch_LastState = HIGH;
    int ReloadSwitch_LastState = HIGH;
    int CurrentPlayMode = 0;
    int CounterArray[10] = {A0, A1, A2, A3, A4, A5, 5, 4, 3, 2};
    
    //                                           1---2---3---4               Weapon Number
    
    int WeaponMaxAmmoArray[Max_Weapons]      = {10, 10, 10, 10};             // Add maximum Weapon ammo here
    int WeaponTypeArray[Max_Weapons]         = { 1,  1,  3,  3};             // Weapon Type 1 = Weapon with Ammo 2 = Weapon unlimited Ammo 3 = Random Sound Player
    int WeaponFireSoundArray[Max_Weapons]    = { 7,  8,  0,  1};             // Number of Fire Sound File or Number of random sound array
    int WeaponReloadSoundArray[Max_Weapons]  = { 3,  3,  0,  0};             // Number of Reload Sound File
    int WeaponEmptySoundArray[Max_Weapons]   = { 5,  5,  0,  0};             // Number of Empty Sound File
    int WeaponFireLightArray[Max_Weapons]    = { 1,  2,  1,  0};             // Number of Fire Lighting
    int WeaponReloadLightArray[Max_Weapons]  = { 1,  1,  1,  1};             // Number of Reload Lighting
    int WeaponEmptyLightArray[Max_Weapons]   = { 1,  1,  1,  1};             // Number of Empty Lighting
    
    //                                                                    Add sound file numbers for random sound player here, each row must have the maximum number of sound files. Not used places need to be 0
    
    int RandomSoundArray[Max_Random_Sound_Arrays][Max_Random_Sounds] =  {{22,23,24,25,26,27,28,29,30,31,32,33,34},
                                                                        { 35,36,37,38,39,40,41,42,43, 0, 0, 0, 0}};
    int RandomArray;
    int RandomTrack;
    int WeaponAmmoArray[Max_Weapons];
    int WeaponSelected                = 0;
    int CounterState                  = 0;
    int TriggerState                  = HIGH;
    int ReloadState                   = HIGH;
    
    long TrackCounter2 = 0;
    
    int var_loop_ammo        = 0;
    int ledStateAmmo         = LOW;              // ledState used to set the LED
    long previousMillisAmmo  = 0;                // will store last time LED was updated
    long intervalAmmo        = 180;              // interval at which to blink (milliseconds)
    
    void setup()
    {
        delay(600); //pause needed to play the track at startup
    
        // initialize the switch pins as a input:
    
        pinMode(SelectorSwitch, INPUT_PULLUP);
        pinMode(TriggerSwitch, INPUT_PULLUP);
        pinMode(ReloadSwitch, INPUT_PULLUP);
    
        // initialize the LEDs as an output:
    
        pinMode(LedFireRed, OUTPUT);
        pinMode(LedFireBlue, OUTPUT);
        pinMode(LedFireGreen, OUTPUT);
        pinMode(LedCounterG10, OUTPUT);
        pinMode(LedCounterG9, OUTPUT);
        pinMode(LedCounterG8, OUTPUT);
        pinMode(LedCounterG7, OUTPUT);
        pinMode(LedCounterG6, OUTPUT);
        pinMode(LedCounterG5, OUTPUT);
        pinMode(LedCounterG4, OUTPUT);
        pinMode(LedCounterR3, OUTPUT);
        pinMode(LedCounterR2, OUTPUT);
        pinMode(LedCounterR1, OUTPUT);
    
        // Turn fire leds off
    
        digitalWrite(LedFireRed, LOW);
        digitalWrite(LedFireBlue, LOW);
        digitalWrite(LedFireGreen, LOW);
    
        // Fill ammo array
        
        for(int i = 0; i < Max_Weapons; i++)
        { WeaponAmmoArray[i] = WeaponMaxAmmoArray[i]; }
    
            
        // initialize serial communication:
    
        Serial.begin(9600);
        randomSeed(analogRead(6));      
     
        Serial.write(0x7E);     // StartEndCode
        Serial.write(0x03);     // Lenght
        Serial.write(0xA7);     // Volume Command
        Serial.write(0x1d);     // Volume (hexadecimal conversion 1-31 --> 1f = max  / 14 = medium)
        Serial.write(0x7E);     // StartEndCode
    
    
        // start sound
        Serial.write(0x7E);     // StartEndCode
        Serial.write(0x04);     // Lenght
        Serial.write(0xA0);     // Play from SD Card
        Serial.write(0x00);     // Highbyte
        Serial.write(0x01);     // track number 01
        Serial.write(0x7E);     // StartEndCode
    
        delay (300);
        CounterStart ();
        delay (3600);
    
        // random vader welcome
    
        TrackCounter2 = random(0,3);  // play files random vader voice
    
        Serial.write(0x7E);     // StartEndCode
        Serial.write(0x04);     // Length
        Serial.write(0xA0);     // Play from SD Card
        Serial.write(0x00);     // Hightbyte
    
        if ( TrackCounter2 == 0 )
            { Serial.write(0x3c); }
        if ( TrackCounter2 == 1 )
            { Serial.write(0x3d); }
        if ( TrackCounter2 == 2 )
            { Serial.write(0x3e); }
        if ( TrackCounter2 == 3 )
            { Serial.write(0x3f); }
    
        Serial.write(0x7E);     // StartEndCode
    }
    
    void loop()
    {
    
    //---------------------------------------------------------------------------------------------- SELECTOR BEGIN --------------------------------------------------------------------------
    
        SelectorState = digitalRead(SelectorSwitch);                                                // read the SelectorSwitch input pin:
       
        if (SelectorState == LOW && SelectorSwitch_LastState == HIGH)       // Selector is pressed
        {
            SelectorSwitchHoldT = millis();                     // mark the time
            AllowSelect = true;                                 // allow state changes
        }
       
        if (AllowSelect == true && SelectorState == LOW && SelectorSwitch_LastState == LOW)      //Selector remain pressed
        {
            if ((millis() - SelectorSwitchHoldT) >= RadioHoldDelay)                              // for longer than x/1000 sec(s) Enter Radio Mode
            {
                Serial.write(0x7E);     // StartEndCode
                Serial.write(0x04);     // Lenght
                Serial.write(0xA0);     // Play from SD Card
                Serial.write(0x00);     // Highbit
                Serial.write(0x40);     // track number button sound
                Serial.write(0x7E);     // StartEndCode
    
                delay (1500);
    
                Serial.write(0x7E);     // StartEndCode
                Serial.write(0x04);     // Lenght
                Serial.write(0xA0);     // Play from SD Card
                Serial.write(0x00);     // Highbit
                Serial.write(0x41);     // Track number
                Serial.write(0x7E);     // StartEndCode
        
                ChangePlayMode (0);
                AllowSelect = false;                                                             // prevent multiple state changes
            }
        }
     
        if (AllowSelect == true && SelectorState == HIGH && SelectorSwitch_LastState == LOW)
        {
            if ((millis() - SelectorSwitchHoldT) < 700 )  //debounce
            {
                ChangePlayMode (0);
                PlaySelectSound ();
                WeaponSelected ++;
                if (WeaponSelected == Max_Weapons)                                               // Last Weapon reached return to first
                    { WeaponSelected = 0; }
                UpdateCounterLed();
                AllowSelect = false;                                                             // prevent multiple state changes
            }
        }
    
        SelectorSwitch_LastState = SelectorState;
    
                    
    //----------------------------------------------------------------------------- SELECTOR END ---------------------------------------------------------------------------------------
                                                                                 
    
    //------------------------------------------------------------------------------- WEAPONS ------------------------------------------------------------------------------------------
    
    //----------------------------------------------------------------------------- Weapon Fires ---------------------------------------------------------------------------------------
    
        TriggerState = digitalRead(TriggerSwitch);                                                                  // shots count - read the pin value and save it
    
        if (TriggerState == LOW && WeaponAmmoArray[WeaponSelected] == 0)                                            // Trigger is pulled but weapon is empty
        {
            UpdateCounterLed ();
            if (TriggerState != TriggerSwitch_LastState)                                                            // compare the TriggerState to its previous state
            {
                PlayEmptySound ();
            }
        }
        
        if (TriggerState == LOW && WeaponTypeArray[WeaponSelected] == 1 && WeaponAmmoArray[WeaponSelected] > 0)     // Trigger is pulled and weapon not empty and weapon have ammo
        {
            if (TriggerState != TriggerSwitch_LastState)                                                            // compare the TriggerState to its previous state
            {
                if (TriggerState == LOW)                                                                            // if the state has changed, decrement the counter
                {
                    WeaponAmmoArray[WeaponSelected] = (WeaponAmmoArray[WeaponSelected] -1) ;
                    UpdateCounterLed ();
                    PlayFireSound ();
                    PlayFireLight ();
                }
            }
        }
    
        if (TriggerState == LOW && WeaponTypeArray[WeaponSelected] == 3)                                            // Trigger is pulled and weapon is in random soundfile mode
        {
            if (TriggerState != TriggerSwitch_LastState)                                                            // compare the TriggerState to its previous state
            {
                if (TriggerState == LOW)
                {
                    PlayRandomSound ();
                    PlayFireLight ();
                }
            }
        }
    
        TriggerSwitch_LastState = TriggerState;                                                                     // save the current state as the last state, for next time through the loop
    
        if (WeaponAmmoArray[WeaponSelected] == 0)                                                                   // Check if weapon is empty and let red led blink
        { CounterStatusEmpty (); }
    
    //---------------------------------------------------------------------------- Weapon Reloads -----------------------------------------------------------------------------------------
    
        ReloadState = digitalRead(ReloadSwitch);
        if (ReloadState == LOW && WeaponTypeArray[WeaponSelected] == 1)                                             // No need to reload a weapon without ammo
        {
            PlayReloadSound ();
            CounterReload ();
            WeaponAmmoArray[WeaponSelected] = WeaponMaxAmmoArray[WeaponSelected] ;
        }
    }
    
    
    //--------------------------------------------------------------------------------------- SOUND FUNCTIONS -------------------------------------------------------
    
        void ChangePlayMode (int PlayMode)
        {
            if (CurrentPlayMode != PlayMode)
            {
                Serial.write(0x7E);                                          // StartEndCode
                Serial.write(0x03);                                          // Lenght
                Serial.write(0xA9);                                          // Set Play Mode
                Serial.write(0x00+PlayMode);                                 // Set Play Mode to 0 No Cyle or 1 Cycle
                Serial.write(0x7E);                                          // StartEndCode
    
                CurrentPlayMode = PlayMode;
            }
        }
    
        void PlaySelectSound ()
        {
            Serial.write(0x7E);                                               // StartEndCode
            Serial.write(0x04);                                               // Length
            Serial.write(0xA0);                                               // SDCard Playback
            Serial.write(0x00);                                               // Highbyte
            Serial.write(0x04);                                               // Lowbyte Track 4
            Serial.write(0x7E);                                               // StartEndCode
        }
    
        void PlayFireSound ()
        {
            Serial.write(0x7E);                                               // StartEndCode
            Serial.write(0x04);                                               // Length
            Serial.write(0xA0);                                               // SDCard Playback
            Serial.write(highByte(WeaponFireSoundArray[WeaponSelected]));     // Highbyte
            Serial.write(lowByte(WeaponFireSoundArray[WeaponSelected]));      // Lowbyte
            Serial.write(0x7E);                                               // StartEndCode
        }
    
        void PlayEmptySound ()
        {         
            Serial.write(0x7E);                                               // StartEndCode
            Serial.write(0x04);                                               // Length
            Serial.write(0xA0);                                               // SDCard Playback
            Serial.write(highByte(WeaponEmptySoundArray[WeaponSelected]));    // Highbyte
            Serial.write(lowByte(WeaponEmptySoundArray[WeaponSelected]));     // Lowbyte
            Serial.write(0x7E);                                               // StartEndCode
            
            delay(500);
        }
    
        void PlayReloadSound ()
        {                 
            Serial.write(0x7E);                                                // StartEndCode
            Serial.write(0x04);                                                // Length  
            Serial.write(0xA0);                                                // SDCard Playback
            Serial.write(highByte(WeaponReloadSoundArray[WeaponSelected]));    // Highbyte
            Serial.write(lowByte(WeaponReloadSoundArray[WeaponSelected]));     // Lowbyte
            Serial.write(0x7E);                                                // StartEndCode
        }
    
        void PlayRandomSound ()
        {
            RandomArray = WeaponFireSoundArray[WeaponSelected];
            for(int i = 0; i < (Max_Random_Sounds); i++)
            {
                if (RandomSoundArray[RandomArray][i] == 0)
                {
                    RandomTrack = random (0, (i -1));
                    break;
                }
                if (i == (Max_Random_Sounds -1))
                {
                    RandomTrack = random (0, (Max_Random_Sounds -1));
                    break;
                }
            }
            
            Serial.write(0x7E);                                                   // StartEndCode
            Serial.write(0x04);                                                   // Length
            Serial.write(0xA0);                                                   // SDCard Playback
            Serial.write(highByte(RandomSoundArray[RandomArray][RandomTrack]));   // Highbyte
            Serial.write(lowByte(RandomSoundArray[RandomArray][RandomTrack]));    // Lowbyte
            Serial.write(0x7E);                                                   // StartEndCode
        }
    
    
    //----------------------------------------------------------------------------------- LIGHT FUNCTIONS -------------------------------------------------------
    
        void PlayFireLight ()
        {
            switch (WeaponFireLightArray[WeaponSelected])
            {
                case 1:                                         // E-11 Blaster
                    delay(30); // pause to wait mp3 start
            
                    digitalWrite(LedFireRed, HIGH);
                    digitalWrite(LedFireBlue, HIGH);
                    digitalWrite(LedFireGreen, HIGH);
    
                    delay(1);
    
                    digitalWrite(LedFireBlue, LOW);
                    digitalWrite(LedFireGreen, LOW);
    
                    delay(80);  // partial lenght of sound
    
                    digitalWrite(LedFireRed, LOW);
    
                    delay(250); // pause to wait the sound finish
                    break;
                
                case 2:                                       // E-11 Stun
                    delay(30); // pause to wait mp3 start
                    analogWrite(LedFireRed, 255);
                    analogWrite(LedFireBlue, 255);
                    analogWrite(LedFireGreen, 255);
                    delay(30);
                    analogWrite(LedFireRed, 0);
                    analogWrite(LedFireGreen, 0);
                    analogWrite(LedFireBlue, 0);
                    delay(30);
                    analogWrite(LedFireRed, 255);
                    analogWrite(LedFireBlue, 255);
                    analogWrite(LedFireGreen, 255);
                    delay(30);
                    analogWrite(LedFireRed, 0);
                    analogWrite(LedFireGreen, 0);
                    analogWrite(LedFireBlue, 0);
                    delay(30);
                    analogWrite(LedFireBlue, 255);
                    delay(50);
                    analogWrite(LedFireBlue, 0);
                    delay(40);
                    analogWrite(LedFireBlue, 255);
                    delay(30);
     
                    // fade out from max to min in increments of 5 points:
                    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
                    {
                        analogWrite(LedFireBlue, fadeValue);
                        delay(10);
                    }       
                    delay(100); // pause to wait the sound finish
                    break;
                
                default:
                    break;
            }
        }
    
    //------------------------------------------------------------------------ COUNTER FUNCTIONS -------------------------------------------------------
    
        void UpdateCounterLed ()
        {
            CounterState = ((WeaponAmmoArray[WeaponSelected]*10) / WeaponMaxAmmoArray[WeaponSelected]);
    
            for(int i = 0; i < CounterState; i++)
            {
                digitalWrite(CounterArray[i], HIGH);
            }
    
            for(int i = 9; i >= CounterState; i--)
            {
                digitalWrite(CounterArray[i], LOW);
            }
        }
    
        void CounterStatusEmpty ()                       //LED BAR - BLINKING END AMMO
        {
            // blinking led start
            unsigned long currentMillisAmmo = millis();
            if(currentMillisAmmo - previousMillisAmmo > intervalAmmo)
            {
                previousMillisAmmo = currentMillisAmmo;
                if (ledStateAmmo == LOW)
                    ledStateAmmo = HIGH;
                else
                    ledStateAmmo = LOW;
    
                digitalWrite(LedCounterR1, ledStateAmmo);
                var_loop_ammo++;
            }  // blinking led END
        }
    
        void CounterStart ()
        {
            for(int i = 0; i < 9; i++)
            {
                digitalWrite(CounterArray[i], HIGH);
                delay(30);
            }
            digitalWrite(CounterArray[9], HIGH);
        }
    
        void CounterReload ()                         // delay total 2700      RELOAD AMMO LED BAR
        {
            for(int i = 9; i >= 0; i--)
            {
                digitalWrite(CounterArray[i], LOW);
            }
            delay(200);
    
            for(int i = 0; i < 5; i++)
            {
                digitalWrite(CounterArray[0], HIGH);
                delay (300);
                digitalWrite(CounterArray[0], LOW);
                delay (200);
            }
            CounterStart ();        
         }
    
    
    
    • Like 2
  2. Hello,

     

    I have had the same problem, it is caused by sending the single no cycle command all the time, even if it already set.

    I added a function which checks if a change is needed and only sends the command in this case.

    Sadly i can´t post the needed lines as my code differs pretty much from the original one.

     

    But if its ok and does not create confusion with the original project i can post the whole layout/code here.

     

    Regards

×
×
  • Create New...