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(){
pixels.begin();
pinMode(fireButton,INPUT_PULLUP); // Configure fire button
secs_delay = 100;
secs_held = 0;
Serial.begin(9600);
}
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
Charge();
}
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
delay(secs_delay);
} // 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);
pixels.show(); // 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
delay(50);
}
}
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
i++;
}
pixels.show(); //turns on the pixels
delay(50);
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)
{
pixels.setPixelColor(o,(0,0,0));
o++;
}
pixels.show();
}