Megaman Buster Code Help

Good day,

I’ve been working on this code for a couple hours now, Trying to recall my high school days a decade or 2 ago. However I can’t quite seem to get it. I have searched high and low, and despite my efforts can’t find exactly what I need.(maybe because its 3 am and I can hardly see the screen) anyhow, I would greatly appreciate some guidance, suggestions or a point in the right direction.

What I’m trying to do here, is detect a button press, if its held, charge up(spin LED’s around faster and faster to a maximum defined speed) once button is released, it will flash a couple times, and all LED’s will turn off. Now I got the code to work to a degree, however when I press the button again, the LED’s will either lock up, or continue to spin at maximum speed.

The rest of the code(not yet implemented) that I have no doubt I can solve after this speed bump, Will be a quick pulse of the LED’s if the button is pressed for a second or less(or a very short period of time yet to be determined) and a not quite full charge time frame will have yet another effect.

I’m just about to start the 8 day 3D print of my Megaman X buster I designed, So just trying to sort out electronics (Eventually ill add code to include the charge up sounds)

Thanks for any help at all, I’m going to keep plugging away, research, read beginners guides and see if I can’t still figure it out too.

#include <Adafruit_NeoPixel.h>

#define ledPin 6
#define ledCount 16
#define fireButton 3

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(ledCount, ledPin, NEO_GRB + NEO_KHZ800);

int buttonState;                // Current state of the button
unsigned long secs_delay;       // Charge ring speed
unsigned long secs_held;        // How long the button was held (seconds)
void setup(){
  pinMode(fireButton,INPUT_PULLUP);    // Configure fire button
  secs_delay = 100;
  secs_held = 0;

void loop(){
  buttonState = digitalRead(fireButton);                            // Check fire button state ( feel like there is a better way to do this)
  if (buttonState == LOW){                                          // If low, send to charge phase
  secs_held=0;                                                      //trying to reset things
  secs_delay=100;                                                   //trying to reset things not to self, dont program at 3am

void Charge(){
  while(digitalRead(fireButton) == LOW && (millis() >= 1000)){   // fire button pressed for more then 1 second
    ChargeUP(0x00FFFF, true);                                    // Call Charge up
    secs_held = (millis() / 1000);
    if (secs_delay >= 10){
      secs_delay = secs_delay - secs_held;                        // Decrese delay for charge ring (minimum delay of 5)

    else {
     secs_delay = 10;
                                                                 // Delay for charge ring speed
  }                                                              // Back to while to see if button still pressed

  // Change code around to check if its full charge, part charge, or just a quick shot
  fireCharge(0x00FFFF);                                          // Fire button pressed for under 1 second change


void ChargeUP(uint32_t pixColour, bool onOff){
  static byte currentPos = 0;
  pixels.setPixelColor((currentPos + 0) % ledCount, 0);          // Clear previous last pixel
  if(++currentPos >= ledCount){
    currentPos = 0;

  if(onOff == true) pixels.setPixelColor((currentPos + 3) % ledCount, pixColour);;  // Show pixels
//void fireCharge()
// future code for firing a charged shot if secs_held < 10 seconds, if secs_held is < 1 second, but > 10 fire half charge(  alternate every second led on and off purple and blue, play buster full charge shot sound  

void fireCharge(uint32_t)
  for (int x=0; x<=3; x++){
  fire(0x00FFFF, 10);              //Call fire function
  fire(pixels.Color(0, 0, 0), 0);  //Turn off all pixels


void fire(uint32_t pixColourf, uint8_t wait)  //passed color (c) and delay (wait) values
  int i = 0;                                  //start at pixel 0
  while (i < ledCount)                      // Pixel count loop 
    pixels.setPixelColor(i,pixColourf);       //sets the pixel and color
  };                              //turns on the pixels
   fireoff();                                 // Turn off blinking after 4 seconds//wait designated time (wait)
void fireoff()                                // Kill all led lights when no action
  int o = 0;
  while (o < ledCount)


zerorob: Good day,

Back at ya.

What I'm trying to do here, is detect a button press, if its held, charge up(spin LED's around faster and faster to a maximum defined speed) once button is pressed...


[quote author=Coding Badly link=msg=3902401 date=1539240867] "Released"? [/quote]

I blame it on 3 am, That is what I meant, I'll go fix that. My mistake.

I should also add that that I'm using an Arduino UNO, and LED ring 2812B-16

State machine / get rid of all while-loops and delays. Some of the state machine will be obvious (e.g. "ChargingUp" and "FullyCharged" seem like a rather obvious states.)

The animations may need their own state machines depending on how you want the gadget to react to a button press / release while an animation is playing.

Record the button down time.

The difference between the button-down-time and the current time triggers state transitions (e.g. the transition for "ChargingUp" to "FullyCharged" occurs after N milliseconds).

Trigger the action on button up. The action is determined by the current state when the button is released.