ProtonPack

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 39e0fbebcebb75a93533c7dd8efcb1444fca7edf
parent f3242387bb076a08de51e4e6754a4b370d716d68
Author: Samdal <samdal@protonmail.com>
Date:   Sun, 21 Nov 2021 14:49:40 +0100

cleanup and improvements

Diffstat:
Msrc/main.cpp | 751+++++++++++++++++++++++++++++++++----------------------------------------------
1 file changed, 315 insertions(+), 436 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp @@ -3,79 +3,45 @@ #include <FastLED.h> #include <DFPlayerMini_Fast.h> -/* tweaking variables */ - // time it takes to reload when overheated -// if it's reloading without overheating -// this value is halved -int overheat_time = 5000; // in ms +// if not overheatign reloading takes half the time +#define OVERHEAT_TIME 5000 // default "hp" // this value represents how long it is until // the pack overheats and is slowly depleted // over time or via shooting -#define proton_default_hp 100 - -// volume of the mp3 player -// max is 30 -#define pack_volume 30 - -// these three values are used as offsets for -// the depletion of theproton hp -// see the function reduce_proton_hp() -// and run_cyclotron() for a better understanding - -unsigned long default_cyclotron_time = 600; // in ms -unsigned long shooting_time = 20000; // in ms -unsigned long idle_time = 550000; // in ms +#define DEFAULT_PROTON_HP 100 -// turns on/off debugging mode -#define debugging true +#define DEFAULT_PROTON_PACK_TIME 600 +#define SHOOTING_TIME 20000 +#define IDLE_TIME 550000 -/* pins on the arduino that are in use */ +#define DEBUGGING 1 // front potentiometer is the potentiometer on the gun // it is used to change the proton hp directly above // a cretan threshold -#define front_potentiometer A8 - -// main power button on the proton pack -#define pack_power A9 - -// gun buttons - -// power button on the gun +#define FRONT_POTENTIOMETER A8 +#define PACK_POWER A9 // lower button on the right side -#define gun_power A10 - -// proton indicator switch +#define GUN_POWER A10 // upper button on the right side -#define proton_indicator A11 - -// activate button +#define PROTON_INDICATOR A11 // upper button on the left side -#define activate A12 - +#define ACTIVATE A12 // button on the front of the gun -// currently set to start reload animation -#define intensify_front A13 - -// intensify button -// lower button on the left side -// starts the shootign animation -#define intensify A14 - -// output for the high power LED -// AKA the proton beam - -#define hp_led_r 9 -#define hp_led_g 10 -#define hp_led_b 11 - -// PWM output for NEOPIXEL leds - -#define PACK_LEDS 13 -#define GUN_LEDS 12 +#define INTENSIFY_FRONT A13 +// lower button on the left side, starts the shootign animation +#define INTENSIFY A14 +// output for the high power led, AKA the proton beam +#define HP_LED_R 9 +#define HP_LED_G 10 +#define HP_LED_B 11 + +// cyclotron fade time +#define CYCLOTRON_FADE_TIME 10 +#define CYCLOTRON_FADE_AMOUNT 5 enum { CYCLOTRON1, @@ -85,6 +51,7 @@ enum { N_FILTER, PACK_NUM_LEDS, } pack_leds_names; +#define PACK_LEDS_PIN 13 CRGB pack_leds[PACK_NUM_LEDS]; enum { @@ -93,26 +60,9 @@ enum { FRONT_LED, GUN_NUM_LEDS, } gun_leds_names; +#define GUN_LEDS_PIN 12 CRGB gun_leds[GUN_NUM_LEDS]; -// proton_graph pins -#define PROTON_GRAPH_COUNT 10 -const uint8_t proton_graph[PROTON_GRAPH_COUNT] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; -int proton_graph_stage = 0; - -// button variables -bool gun_power_on; -bool pack_power_on; -bool proton_indicator_on; -bool activate_on; -bool intensify_on; -bool intensify_reload; -bool vent_on; -int pot_value = 5; - -/* tracks */ - -DFPlayerMini_Fast myMP3; enum { power_down_sound = 1, pack_hum_sound, @@ -122,264 +72,191 @@ enum { beep_sound, beep_shoot_sound, gun_overheat_sound, -} sounds; - -/* modes */ +} tracks; +#define PACK_VOLUME 30 +DFPlayerMini_Fast myMP3; enum { - proton_accelerator = 0, - dark_matter_generator, - plasm_distribution_system, - composite_particle_system, + PROTON_ACCELERATOR, + DARK_MATTER_GENERATOR, + PLASM_DISTRIBUTION_SYSTEM, + COMPOSITE_PARTICLE_SYSTEM, + PROTON_MODES_COUNT, } proton_modes; -int currentMode = proton_accelerator; +int current_mode = PROTON_ACCELERATOR; -/* overheating and shooting */ +#define PROTON_GRAPH_COUNT 10 +const uint8_t proton_graph[PROTON_GRAPH_COUNT] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; +uint8_t proton_graph_stage = 0; -unsigned long previous_overheat = 0; +#define POWERCELL_COUNT 20 +const int powercell[POWERCELL_COUNT] {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51}; +uint8_t powercell_stage = 0; + +// button variables (stored here so that debug mode can change them) +bool gun_power_on = true; +bool pack_power_on = true; +bool proton_indicator_on; +bool activate_on; +bool intensify_on; +bool intensify_reload; -// current proton hp -int proton_hp = proton_default_hp; +bool front_led_on = true; +bool shooting; +bool system_on; // current proton reduction caused by the proton indicator knob int proton_reduction; +uint8_t proton_indicator_pot_value = 5; -// previous reduction to stop the proton indicator knob from -// constantly changing -unsigned long previous_hp_reduction = 0; - -// has played overheating sound? -bool beeped = false; - -bool shooting = false; +uint8_t cyclotron_stage; -// last time since shooting has reduced proton hp -unsigned long last_shooting = 0; - -bool system_on = false; - -#define max_power 100 +uint8_t proton_hp = DEFAULT_PROTON_HP; // state of the high power led // for smooth transition - int red_state = 0; int green_state = 0; int blue_state = 0; -// delay for changing high power LED -// in ms -#define high_power_LED_delay 2 - -// time since last led update -unsigned long previous_LED_update = 0; -// time since last color change -unsigned long previous_color_change = 0; - -bool color_change; - -// random delay between color changes -long rng_delay = 100; +#define MAX_POWER 100 +#define HIGH_POWER_LED_DELAY 2 // colors of the firing beam -int high_power_LED_color[4][4][3] = { - { // proton_accelerator +uint8_t high_power_led_colors[PROTON_MODES_COUNT][4][3] = { + { // PROTON_ACCELERATOR // red, yellow, white, blue {90, 0, 0}, {70 , 60, 0}, {90 , 80, 80}, {7 , 20, 70} }, - { // dark_matter_generator + { // DARK_MATTER_GENERATOR // blue, white, light blue, purple {0 , 0, 90}, {80 , 80, 90}, {0 , 30, 70}, {60 , 0, 80} }, - { // plasm_distribution_system + { // PLASM_DISTRIBUTION_SYSTEM // green, light green, green, white {0, 90, 0}, {20, 90, 10}, {0, 90, 0}, {80 , 90, 80} }, - { // composite_particle_system + { // COMPOSITE_PARTICLE_SYSTEM // yellow, orange, red, white {70 , 60, 0}, {80 , 60, 5}, {90, 0, 0}, {90 , 80, 80} } }; -/* cyclotron */ - // time in between cyclotron changes // incereeses depending on the proton hp -unsigned long cyclotron_time = default_cyclotron_time / 100 * proton_hp; - -// previous cyclotron change -unsigned long cyclotron_previous_time = 0; - -// previous cyclotron fade, for a smooth transition -unsigned long cyclotron_previous_fade = 0; - -// fade time steps -unsigned long cyclotron_fade_time = 15; // in ms - -// current cyclotron stage (1-4) -int cyclotron_stage = 0; - -// is a cyclotron cell currently lighted? -bool cyclotron_on = false; - -// gun lights -bool gun_lights_on; - -/* powercell */ - -// time since last powercell update -unsigned long powercell_previous_time = 0; // in ms - -// powercell graph pins -#define POWERCELL_COUNT 20 -const int powercell[POWERCELL_COUNT] {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51}; - -// current powercell stage -int powercell_stage = 0; - -/* LED pulse */ - -// alternate between front LED and white LED - -// last pulse, for timing -unsigned long last_pulse = 0; - -// which of the LEDS should be lit -bool front_LED_on = true; +unsigned long proton_pack_time = DEFAULT_PROTON_PACK_TIME / 100 * proton_hp; // global time value to run the different timings unsigned long t = 0; -/* function declarations */ - -// cycle the powercell -void run_powercell(); - -// animation and sound when the proton pack starts void start_up(); - -// turns of the gun lights -void gun_lights_off(); - -// reloads (sound and light animations), if overheated it lasts longer -void reload(bool overheat); - -// turn on / keep running the proton pack lights void run_proton_pack(); - -// shoot and depleet amunition -void shoot(); - -// changes brightness in the LED at the end of the gun accordingly -void high_power_LED(int LEDcolor[], unsigned long spacing_delay); - -// reads the potentiometer at the front and writes a value to proton_hp -void read_potentiometer(); - -// reduces the proton_hp (ammonution) accordingly void reduce_proton_hp(); +void run_proton_gun(); +void gun_lights_off(); +void shoot(); +void reload(bool overheat); +void reset_pack(); -// check the gun switches and act accordingly -void gun_switches(); +void run_powercell(); +void powercell_off(); -// pulses the WHITE_LED inversely with the FRONT_LED -void pulse_LED(); +// changes brightness in the led at the end of the gun accordingly +bool high_power_led(const uint8_t* ledcolor, const unsigned long spacing_delay); +void high_power_led_off(); -// runs the proton indicator graph on the proton gun +void pulse_led(); // pulses the WHITE_LED inversely with the FRONT_LED void run_proton_indicator(); +void read_potentiometer(); +void proton_indicator_off(); -// cycles to cyclotron +CRGB vent_color(); +CRGB cyclotron_color(); void run_cyclotron(); - -// resets variables and resets functions -void reset_pack(); - -// turns off cyclotron -void cyclotron_off(); - -// fades non-lit cyclotron cells void fade_cyclotron(); +void cyclotron_off(); -// changes the color of the vent -void vent_color(); - -// changes the cyclotron color to the right one -void cyclotron_color(int currentled); - -// printing debugging messages void debugging_message(); - -// turning on and off switches from Serial monitor void debugging_switches(); void setup() { +#if DEBUGGING + Serial.begin(115200); + Serial.println("Debug mode entered"); + delay(1000); +#endif + // input pins (buttons and switches) - pinMode(pack_power, INPUT); - pinMode(gun_power, INPUT); - pinMode(proton_indicator, INPUT); - pinMode(activate, INPUT); - pinMode(intensify, INPUT); - pinMode(intensify_front, INPUT); + pinMode(PACK_POWER, INPUT); + pinMode(GUN_POWER, INPUT); + pinMode(PROTON_INDICATOR, INPUT); + pinMode(ACTIVATE, INPUT); + pinMode(INTENSIFY, INPUT); + pinMode(INTENSIFY_FRONT, INPUT); // output pins - for (int i = proton_graph[0]; i < PROTON_GRAPH_COUNT; i++) - pinMode(i, OUTPUT); - for (int i = powercell[0]; i < POWERCELL_COUNT; i++) - pinMode(i, OUTPUT); + for (int i = 0; i < PROTON_GRAPH_COUNT; i++) + pinMode(proton_graph[i], OUTPUT); + for (int i = 0; i < POWERCELL_COUNT; i++) + pinMode(powercell[i], OUTPUT); - pinMode(hp_led_r, OUTPUT); - pinMode(hp_led_g, OUTPUT); - pinMode(hp_led_b, OUTPUT); + pinMode(HP_LED_R, OUTPUT); + pinMode(HP_LED_G, OUTPUT); + pinMode(HP_LED_B, OUTPUT); // neopixel leds - FastLED.addLeds<NEOPIXEL, PACK_LEDS>(pack_leds, PACK_NUM_LEDS); - FastLED.addLeds<NEOPIXEL, GUN_LEDS>(gun_leds, GUN_NUM_LEDS); - -#if debug - Serial.begin(115200); -#endif + FastLED.addLeds<NEOPIXEL, PACK_LEDS_PIN>(pack_leds, PACK_NUM_LEDS); + FastLED.addLeds<NEOPIXEL, GUN_LEDS_PIN>(gun_leds, GUN_NUM_LEDS); + FastLED.clear(true); + cyclotron_off(); + gun_lights_off(); // sound Serial1.begin(9600); myMP3.begin(Serial1); delay(500); - - myMP3.volume(pack_volume); - FastLED.clear(true); + myMP3.volume(PACK_VOLUME); + delay(500); } // loop while the arduino has power void loop() { -#if !debugging - gun_power_on = digitalRead(gun_power); - pack_power_on = digitalRead(pack_power); -#else +#if DEBUGGING debugging_switches(); - debugging_message(); + //debugging_message(); +#else + gun_power_on = digitalRead(GUN_POWER); + pack_power_on = digitalRead(PACK_POWER); #endif t = millis(); if (pack_power_on || gun_power_on) { + if (!system_on) { + start_up(); + system_on = true; + } + + if (gun_power_on) run_proton_gun(); + else gun_lights_off(); + run_proton_pack(); - system_on = true; } - if (pack_power_on && gun_lights_on) { - gun_lights_off(); - } - if (!pack_power_on && !gun_power_on) { + if (!pack_power_on && !gun_power_on && system_on) { myMP3.play(power_down_sound); reset_pack(); + system_on = false; + delay(100); } } // turn on / keep running the proton pack lights void run_proton_pack() { - if (!system_on) start_up(); + proton_pack_time = DEFAULT_PROTON_PACK_TIME / DEFAULT_PROTON_HP * ( proton_hp - proton_reduction ) + 40; run_cyclotron(); run_powercell(); @@ -389,11 +266,13 @@ void run_proton_pack() // reads the potentiometer at the front and writes a value to proton_hp void read_potentiometer() { -#if !debugging - pot_value = map(analogRead(front_potentiometer), 0, 1023, 0, 5); +#if !DEBUGGING + proton_indicator_pot_value = map(analogRead(FRONT_POTENTIOMETER), 0, 1023, 0, 5); #endif - if (pot_value < 1) proton_reduction = proton_default_hp - 20; - else proton_reduction = proton_default_hp - pot_value * 20; + if (proton_indicator_pot_value == 5) proton_indicator_pot_value = 4; + + if (proton_indicator_pot_value == 0) proton_reduction = DEFAULT_PROTON_HP - 20; + else proton_reduction = DEFAULT_PROTON_HP - (proton_indicator_pot_value + 1) * 20; } // animation and sound when the proton pack starts @@ -413,39 +292,21 @@ void start_up() delay(17); } - cyclotron_off(); - - for (int i = 0; i < POWERCELL_COUNT; i++) - digitalWrite(powercell[i], LOW); - powercell_stage = 0; - - - for (int i = 0; i < PROTON_GRAPH_COUNT; i++) - digitalWrite(proton_graph[i], LOW); - proton_graph_stage = 0; - - analogWrite(hp_led_r, 0); - analogWrite(hp_led_g, 0); - analogWrite(hp_led_b, 0); - red_state = 0; - green_state = 0; - blue_state = 0; + reset_pack(); } // check the gun switches and act accordingly -void gun_switches() +void run_proton_gun() { - gun_lights_on = true; - // if debugging mode is on, dont read switches - // just use the commands from the Serial monitor - // for more see debugging_switches() and loop() -#if !debugging + // the buttons are set from the serial monitor + // see debugging_switches() +#if !DEBUGGING // check the gun switches - proton_indicator_on = digitalRead(proton_indicator); - activate_on = digitalRead(activate); - intensify_on = digitalRead(intensify); - intensify_reload = digitalRead(intensify_front); + proton_indicator_on = digitalRead(PROTON_INDICATOR); + activate_on = digitalRead(ACTIVATE); + intensify_on = digitalRead(INTENSIFY); + intensify_reload = digitalRead(INTENSIFY_FRONT); #endif if (proton_indicator_on) { @@ -453,27 +314,21 @@ void gun_switches() run_proton_indicator(); if (activate_on) { - pulse_LED(); - vent_color(); + pulse_led(); + gun_leds[VENT_LED] = vent_color(); FastLED.show(); if (intensify_on) { - shoot(); // play shooting sounds and animation whilst depleeting ammunition + shoot(); } else if (shooting) { myMP3.play(gun_trail_sound); - analogWrite(hp_led_r, 0); - analogWrite(hp_led_g, 0); - analogWrite(hp_led_b, 0); - shooting = false; - red_state = 0; - green_state = 0; - blue_state = 0; + shooting = false; + high_power_led_off(); } } else { // do theese if the proton indicator is on but not the generator switch - vent_on = false; - front_LED_on = true; + front_led_on = true; gun_leds[VENT_LED] = CRGB::Black; gun_leds[FRONT_LED] = CRGB::Black; gun_leds[WHITE_LED] = CRGB::White; @@ -486,43 +341,48 @@ void gun_switches() return; } } else { - vent_on = false; gun_leds[VENT_LED] = CRGB::Black; gun_leds[FRONT_LED] = CRGB::Black; gun_leds[WHITE_LED] = CRGB::Black; FastLED.show(); proton_reduction = 0; - for (int i = 0; i < PROTON_GRAPH_COUNT; i++) - digitalWrite(proton_graph[i], LOW); - proton_graph_stage = 0; + proton_indicator_off(); } } -// cycle the powercell +void powercell_off() +{ + for (int i = 0; i < POWERCELL_COUNT; i++) + digitalWrite(powercell[i], LOW); + powercell_stage = 0; +} + void run_powercell() { - if (t - powercell_previous_time >= cyclotron_time / 9 + 30) { - digitalWrite(powercell[powercell_stage++], HIGH); + static unsigned long powercell_previous_time; - if (powercell_stage > POWERCELL_COUNT) { + if (t - powercell_previous_time >= proton_pack_time / 6) { + if (powercell_stage >= POWERCELL_COUNT) { for (int i = 0; i < POWERCELL_COUNT; i++) digitalWrite(powercell[i], LOW); powercell_stage = 0; + } else { + digitalWrite(powercell[powercell_stage++], HIGH); } powercell_previous_time = t; } } -// cycles to cyclotron void run_cyclotron() { + static unsigned long cyclotron_previous_time; + static bool cyclotron_on; + fade_cyclotron(); - // the cyclotron time is directly relative to the proton_hp - cyclotron_time = default_cyclotron_time / 100 * ( proton_hp - proton_reduction ) + 40; // while the cyclotron is on, it should stay on for a set amount of time - if ((cyclotron_on) && (t - cyclotron_previous_time >= cyclotron_time /3*2)) { + if (cyclotron_on && t - cyclotron_previous_time >= proton_pack_time / 3 * 2) { cyclotron_stage++; cyclotron_previous_time = t; cyclotron_on = false; @@ -531,10 +391,9 @@ void run_cyclotron() // loop around if (cyclotron_stage > 3) cyclotron_stage = 0; - // if the cyclotron is off, turn it on after a time has exceeded. - if ((!cyclotron_on) && (t - cyclotron_previous_time >= cyclotron_time)) { - cyclotron_color(cyclotron_stage); + if (!cyclotron_on && t - cyclotron_previous_time >= proton_pack_time) { + pack_leds[cyclotron_stage] = cyclotron_color(); FastLED.show(); cyclotron_previous_time = t; @@ -545,41 +404,31 @@ void run_cyclotron() // reloads (sound and light animations), if overheated it lasts longer void reload(bool overheat) { - -#if debugging +#if DEBUGGING Serial.println("---------!RELOADING!---------"); - if (overheat) - Serial.println("---------!OVERHEATED!---------"); + if (overheat) Serial.println("---------!OVERHEATED!---------"); #endif - myMP3.play(gun_overheat_sound); - cyclotron_off(); - for (int i = 0; i < PROTON_GRAPH_COUNT; i++) - digitalWrite(proton_graph[i], LOW); - proton_graph_stage = 0; + pack_leds[CYCLOTRON1] = cyclotron_color(); + pack_leds[CYCLOTRON2] = cyclotron_color(); + pack_leds[CYCLOTRON3] = cyclotron_color(); + pack_leds[CYCLOTRON4] = cyclotron_color(); + proton_indicator_off(); + for (int i = 0; i < POWERCELL_COUNT; i++) + digitalWrite(powercell[i], HIGH); + high_power_led_off(); pack_leds[N_FILTER] = CRGB::Red; + gun_leds[WHITE_LED] = CRGB::White; gun_leds[FRONT_LED] = CRGB::Red; - vent_color(); + gun_leds[VENT_LED] = vent_color(); FastLED.show(); - analogWrite(hp_led_r, 0); - analogWrite(hp_led_g, 0); - analogWrite(hp_led_b, 0); - - for (int i = 0; i < POWERCELL_COUNT; i++) - digitalWrite(powercell[i], HIGH); - powercell_stage = 10; - - red_state = 0; - green_state = 0; - blue_state = 0; - if (overheat) { - delay(overheat_time); + delay(OVERHEAT_TIME); } else { - delay(overheat_time / 2); + delay(OVERHEAT_TIME / 2); intensify_reload = false; } reset_pack(); @@ -589,60 +438,86 @@ void reload(bool overheat) // resets variables and resets functions void reset_pack() { - t = millis(); gun_lights_off(); FastLED.clear(true); FastLED.show(); - for (int i = 0; i < POWERCELL_COUNT; i++) - digitalWrite(powercell[i], LOW); - powercell_stage = 0; - - cyclotron_stage = 0; - proton_hp = proton_default_hp; - cyclotron_on = false; - front_LED_on = true; - system_on = false; - shooting = false; - beeped = false; - cyclotron_previous_time = t; - powercell_previous_time = t; + proton_hp = DEFAULT_PROTON_HP; + cyclotron_off(); + powercell_off(); } // reduces the proton_hp (ammonution) accordingly void reduce_proton_hp() { + static unsigned long previous_hp_reduction; + static bool beeped; + // automaticaly reload if the proton hp is a zero if (proton_hp - proton_reduction <= 0) { reload(true); return; } - // reduce the proton_hp slowly while the pack is ideling - if (t - previous_hp_reduction >= idle_time / proton_default_hp) { + // reduce the proton_hp slowly while the pack is ideling (does not apply if there isn't any proton reduction) + if (proton_reduction != 0 && t - previous_hp_reduction >= IDLE_TIME / DEFAULT_PROTON_HP) { proton_hp -= 1; previous_hp_reduction = t; } // if there is less than x hp left, play the overheat warning sound - if ((proton_hp <= 12) && (!beeped)) { + if (proton_hp - proton_reduction <= 12 && !beeped) { + beeped = true; if (shooting) myMP3.play(beep_shoot_sound); else myMP3.play(beep_sound); - beeped = true; // make sure that it only beeps once + + for (uint8_t blinkamount = 0; blinkamount <= 5; blinkamount++) { + for (int i = 0; i < PROTON_GRAPH_COUNT; i++) + digitalWrite(proton_graph[i], HIGH); + for (int i = 0; i < POWERCELL_COUNT; i++) + digitalWrite(powercell[i], HIGH); + + cyclotron_off(); + pack_leds[N_FILTER] = CRGB::Red; + gun_leds[FRONT_LED] = CRGB::Black; + gun_leds[WHITE_LED] = CRGB::Black; + gun_leds[VENT_LED] = CRGB::Black; + FastLED.show(); + delay(200); + + proton_indicator_off(); + powercell_off(); + pack_leds[CYCLOTRON1] = cyclotron_color(); + pack_leds[CYCLOTRON2] = cyclotron_color(); + pack_leds[CYCLOTRON3] = cyclotron_color(); + pack_leds[CYCLOTRON4] = cyclotron_color(); + pack_leds[N_FILTER] = CRGB::Black; + gun_leds[FRONT_LED] = CRGB::Red; + gun_leds[WHITE_LED] = CRGB::White; + gun_leds[VENT_LED] = CRGB::White; + FastLED.show(); + delay(200); + } + } else if (proton_hp - proton_reduction > 12 && beeped){ + beeped = false; } } // shoot and depleet amunition void shoot() { + static unsigned long previous_led_update = 0; // letting the program know it has shot so that when it turns of it can play the trail effect if (!shooting) { shooting = true; myMP3.play(shoot_sound); } - if (t - previous_LED_update >= high_power_LED_delay) { - static int rng = 0; + if (t - previous_led_update >= HIGH_POWER_LED_DELAY) { + static bool color_change; + static unsigned long rng_delay = 100; + static int rng; + if (color_change) { rng = random(10); rng_delay = random(100, 300); @@ -656,145 +531,157 @@ void shoot() case 1: case 2: case 3: - high_power_LED(high_power_LED_color[currentMode][0], rng_delay); + color_change = high_power_led(high_power_led_colors[current_mode][0], rng_delay); break; // 3/10 chance of being color 2 case 4: case 5: case 6: - high_power_LED(high_power_LED_color[currentMode][1], rng_delay); + color_change = high_power_led(high_power_led_colors[current_mode][1], rng_delay); break; // 2/10 chance of being color 3 case 7: case 8: - high_power_LED(high_power_LED_color[currentMode][2], rng_delay); + color_change = high_power_led(high_power_led_colors[current_mode][2], rng_delay); break; // 1/10 chance of being color 4 case 9: - high_power_LED(high_power_LED_color[currentMode][3], rng_delay); + color_change = high_power_led(high_power_led_colors[current_mode][3], rng_delay); break; default: color_change = true; break; } - previous_LED_update = t; + previous_led_update = t; } // reduce the proton_hp (ammonution) - if (t - last_shooting >= shooting_time / proton_default_hp) { + static unsigned long last_shooting; + if (t - last_shooting >= SHOOTING_TIME / DEFAULT_PROTON_HP) { proton_hp -= 1; last_shooting = t; } } -// changes brightness in the LED at the end of the gun accordingly -void high_power_LED(int LEDcolor[], unsigned long spacing_delay) +// changes brightness in the led at the end of the gun accordingly +bool high_power_led(const uint8_t* ledcolor, const unsigned long spacing_delay) { - int R = constrain(LEDcolor[0], 0, max_power); + static unsigned long previous_color_change; + int R = constrain(ledcolor[0], 0, MAX_POWER); if (red_state < R) red_state++; else if (red_state > R) red_state--; - int G = constrain(LEDcolor[1], 0, max_power); + int G = constrain(ledcolor[1], 0, MAX_POWER); if (green_state < G) green_state++; else if (green_state > G) green_state--; - int B = constrain(LEDcolor[2], 0, max_power); + int B = constrain(ledcolor[2], 0, MAX_POWER); if (blue_state < B) blue_state++; else if (blue_state > B) blue_state--; - analogWrite(hp_led_r, red_state); - analogWrite(hp_led_g, green_state); - analogWrite(hp_led_b, blue_state); - if ((red_state == R) && (green_state == G) && (blue_state == B) && (t - previous_color_change >= spacing_delay)) { + analogWrite(HP_LED_R, red_state); + analogWrite(HP_LED_G, green_state); + analogWrite(HP_LED_B, blue_state); + if (red_state == R && green_state == G && blue_state == B + && t - previous_color_change >= spacing_delay) { previous_color_change = t; - color_change = true; + return true; } + return false; +} + +void high_power_led_off() +{ + analogWrite(HP_LED_R, 0); + analogWrite(HP_LED_G, 0); + analogWrite(HP_LED_B, 0); + red_state = 0; + green_state = 0; + blue_state = 0; } void gun_lights_off() { - gun_lights_on = false; + proton_reduction = 0; + front_led_on = true; + gun_leds[WHITE_LED] = CRGB::Black; gun_leds[FRONT_LED] = CRGB::Black; gun_leds[VENT_LED] = CRGB::Black; FastLED.show(); + + high_power_led_off(); + proton_indicator_off(); + if (shooting) { delay(100); myMP3.play(gun_trail_sound); shooting = false; } - - analogWrite(hp_led_r, 0); - analogWrite(hp_led_g, 0); - analogWrite(hp_led_b, 0); - red_state = 0; - green_state = 0; - blue_state = 0; - proton_graph_stage = 0; - for (int i = 0; i < PROTON_GRAPH_COUNT; i++) - digitalWrite(proton_graph[i], LOW); } // pulses the WHITE_LED inversely with the FRONT_LED -void pulse_LED() +void pulse_led() { - if ((t - last_pulse >= cyclotron_time / 2) && (front_LED_on)) { - gun_leds[FRONT_LED] = CRGB::Black; - front_LED_on = false; - gun_leds[WHITE_LED] = CRGB::White; - last_pulse = t; - FastLED.show(); - } else if ((t - last_pulse >= cyclotron_time / 2) && (!front_LED_on)) { - gun_leds[WHITE_LED] = CRGB::Black; - front_LED_on = true; - gun_leds[FRONT_LED] = CRGB::Red; - last_pulse = t; + static unsigned long previous_pulse; + if (t - previous_pulse >= proton_pack_time / 1.5) { + if (front_led_on) { + gun_leds[WHITE_LED] = CRGB::White; + gun_leds[FRONT_LED] = CRGB::Black; + } else { + gun_leds[WHITE_LED] = CRGB::Black; + gun_leds[FRONT_LED] = CRGB::Red; + } FastLED.show(); + front_led_on = !front_led_on; + previous_pulse = t; } } -// runs the proton indicator graph on the proton gun void run_proton_indicator() { - proton_graph_stage = constrain((proton_hp - proton_reduction) / 10, 0, PROTON_GRAPH_COUNT); + proton_graph_stage = (proton_hp - proton_reduction) / (DEFAULT_PROTON_HP / 10); for (int i = 0; i < PROTON_GRAPH_COUNT; i++) { if (i <= proton_graph_stage) digitalWrite(proton_graph[i], HIGH); - else digitalWrite(proton_graph[i], LOW); + else digitalWrite(proton_graph[i], LOW); } } -// changes the cyclotron color to the right one -void cyclotron_color(int currentled) +void proton_indicator_off() { - switch (currentMode) { - case proton_accelerator: pack_leds[currentled] = CRGB::Red; break; - case dark_matter_generator: pack_leds[currentled] = CRGB::Blue; break; - case plasm_distribution_system: pack_leds[currentled] = CRGB::Green; break; - case composite_particle_system: pack_leds[currentled] = CRGB::Orange; break; - default: pack_leds[currentled] = CRGB::White; break; + for (int i = 0; i < PROTON_GRAPH_COUNT; i++) + digitalWrite(proton_graph[i], LOW); +} + +CRGB cyclotron_color() +{ + switch (current_mode) { + case PROTON_ACCELERATOR: return CRGB::Red; + case DARK_MATTER_GENERATOR: return CRGB::Blue; + case PLASM_DISTRIBUTION_SYSTEM: return CRGB::Green; + case COMPOSITE_PARTICLE_SYSTEM: return CRGB::Orange; + default: return CRGB::White; } } // changes the color of the vent -void vent_color() +CRGB vent_color() { - vent_on = true; - switch (currentMode) { - case proton_accelerator: gun_leds[VENT_LED] = CRGB::White; break; - case dark_matter_generator: gun_leds[VENT_LED] = CRGB::Blue; break; - case plasm_distribution_system: gun_leds[VENT_LED] = CRGB::Green; break; - case composite_particle_system: gun_leds[VENT_LED] = CRGB::Yellow; break; - default: gun_leds[VENT_LED] = CRGB::Red; break; + switch (current_mode) { + case PROTON_ACCELERATOR: return CRGB::White; + case DARK_MATTER_GENERATOR: return CRGB::Blue; + case PLASM_DISTRIBUTION_SYSTEM: return CRGB::Green; + case COMPOSITE_PARTICLE_SYSTEM: return CRGB::Yellow; + default: return CRGB::Red; } } -// turns off cyclotron void cyclotron_off() { pack_leds[CYCLOTRON1] = CRGB::Black; @@ -802,28 +689,25 @@ void cyclotron_off() pack_leds[CYCLOTRON3] = CRGB::Black; pack_leds[CYCLOTRON4] = CRGB::Black; FastLED.show(); + cyclotron_stage = 3; } -// fades non-lit cyclotron cells void fade_cyclotron() { - if(t - cyclotron_previous_fade < cyclotron_fade_time) return; + static unsigned long cyclotron_previous_fade; + if(t - cyclotron_previous_fade < CYCLOTRON_FADE_TIME) return; - int cyclotron_decrease = 3; for(int i = 0; i < 4; i++) { - if (i == cyclotron_stage) continue; // cycle down r, g and b values - for(int c = 0; c < 3; c++) { - if (pack_leds[i].raw[c] - cyclotron_decrease > 0) - pack_leds[i].raw[c] -= cyclotron_decrease; + for(int c = 0; c < 3; c++) + if (pack_leds[i].raw[c] >= CYCLOTRON_FADE_AMOUNT) + pack_leds[i].raw[c] -= CYCLOTRON_FADE_AMOUNT; else pack_leds[i].raw[c] = 0; - } } - cyclotron_previous_fade = t; FastLED.show(); } @@ -831,12 +715,6 @@ void fade_cyclotron() // printing debugging messages void debugging_message() { - - // if debugging mode is off, don't do anything -#if !debugging - return; -#endif - delay(200); for (int i = 0 ; i < 30 ; i++) { Serial.println(""); @@ -847,10 +725,10 @@ void debugging_message() return; } Serial.println("Switches:"); - Serial.println("gun_power_on: " + String(gun_power_on)); + Serial.println("gun_power_on: " + String(gun_power_on)); Serial.println("pack_power_on: " + String(pack_power_on)); Serial.println("proton_indicator_on: " + String(proton_indicator_on)); - Serial.println("activate_on: " + String(activate_on)); + Serial.println("activate_on: " + String(activate_on)); Serial.println("intensify_on: " + String(intensify_on)); Serial.println("intensify_reload: " + String(intensify_reload)); Serial.println(""); @@ -858,20 +736,20 @@ void debugging_message() Serial.println("Proton HP: " + String(proton_hp)); Serial.println("Effective proton HP: " + String(proton_hp - proton_reduction)); Serial.println("t: " + String(t)); - Serial.println("Proton pack mode: " + String(currentMode)); + Serial.println("Proton pack mode: " + String(current_mode)); Serial.println(); - Serial.println("Cyclotron stage: " + String(cyclotron_stage)); - Serial.println("Powercell stage: " + String(powercell_stage)); + Serial.println("Cyclotron stage: " + String(cyclotron_stage)); + Serial.println("Powercell stage: " + String(powercell_stage)); if (!gun_power_on) return; Serial.println(""); Serial.println("Gun lights:"); - Serial.println("White led: " + String(!front_LED_on)); - Serial.println("front led: " + String(front_LED_on)); - Serial.println("Vent led: " + String(vent_on)); + Serial.println("White led: " + String(!front_led_on)); + Serial.println("front led: " + String(front_led_on)); + Serial.println("Vent led: " + String(activate_on)); Serial.println("Proton indicator stage: " + String(proton_graph_stage)); - Serial.println("Proton position: " + String(pot_value)); + Serial.println("Proton position: " + String(proton_indicator_pot_value)); Serial.println("Proton reduction: " + String(proton_reduction)); Serial.println(""); Serial.println("Shooting: " + String(shooting)); @@ -883,18 +761,19 @@ void debugging_switches() { if (Serial.available()) { switch(Serial.read()) { - case 'g': gun_power_on = !gun_power_on; break; + case 'g': gun_power_on = !gun_power_on; break; case 'p': pack_power_on = !pack_power_on; break; case 'q': proton_indicator_on = !proton_indicator_on; break; case 'a': activate_on = !activate_on; break; case 'i': intensify_on = !intensify_on; break; case 'r': intensify_reload = !intensify_reload; break; - case '0': pot_value = 0; break; - case '1': pot_value = 1; break; - case '2': pot_value = 2; break; - case '3': pot_value = 3; break; - case '4': pot_value = 4; break; - case '5': pot_value = 5; break; + + case '0': proton_indicator_pot_value = 0; break; + case '1': proton_indicator_pot_value = 1; break; + case '2': proton_indicator_pot_value = 2; break; + case '3': proton_indicator_pot_value = 3; break; + case '4': proton_indicator_pot_value = 4; break; + case '5': proton_indicator_pot_value = 5; break; } } }