I am currently building a proton pack from the new rebooted ghostbusters movie (don't hate me lol).
I have learned a considerable amount regarding coding now and have managed to succesfully crow bar a load of code in based on different actions. The only problem is, I'm only comfortable with the delay function and I'm already quite familiar about how much of an a*se ache this can be when multitasking.
My project involves
1 x 24 Neopixel Ring
1 x Momentary switch (trigger)
2 x Rocker switches
4 x RED LED (for barrel of the gun)
1 x red LED for power blinker
1 x WHITE LED for muzzle strobe (to simulate firing)
Switches are currently different in the video I've linked, but that's the end goal.
Here is my sequence:
Arduino master switch (rocker 1) turns power on and instantly causes the Neopixel ring to do a single colour wipe in red then fade in and out continuously.
Then: Rocker Switch 2 turns the 4 barrel LED's on, Neopixel Ring begins chase sequence, power blinker... blinks.
Once the above has happened...
Momentary switch (trigger) causes NPR to strobe, 4 LED's to do a chase sequence, white muzzle LED strobes and power blinker remains solid red.
Video:
Sadly the delays in the code halt things from running continuously so I'm keen to move away from delays and try to implement milli's. I had a crack at it, but still failed on the overall project. Ideally, when the trigger is pressed, I'd rather the neopixel strobed simultaneously with the LED's on the breadboard chasing, instead of alternating... Which is exactly what you'd expect from the delay function.
I've tidied up my code as much as possible before posting it here. Any help to start phasing out delays in my work would be very much appreciated.
#include <Adafruit_NeoPixel.h>
#define PIN 6 ///NEOPIXELS
#define NUM_PIXELS 24 ///NUMBER OF NEOPIXELS
unsigned long previousMillis=0;
const long interval = 250;
const int ledPin = 13; ///BLINKING LED
uint32_t currentColor;// current Color in case we need it
uint16_t currentPixel = 0;// what pixel are we operating on
int inPin = 7; // PUSHBUTTON
int Pin2 = 5;
int val = 0; // variable for reading the pin status
int ledState = LOW;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, 6, NEO_GRB + NEO_KHZ800);
// Array with Arduino pins containing LEDs in sequence
void setup(){
pinMode(5, INPUT_PULLUP); ///switch
pinMode(7, INPUT_PULLUP); ///switch
pinMode(8, OUTPUT); ///LED 1
pinMode(9, OUTPUT); /// LED 2
pinMode(10, OUTPUT); ///LED 3
pinMode(11, OUTPUT); ///LED 4
pinMode(12, OUTPUT); /// WHITE LED
pinMode(13, OUTPUT); /// BLINKING LED
for (int x=0; x < 6; x++) // Set Pins with LEDs to OUTPUT
currentPixel = 0;
strip.begin();
strip.show(); // Initialize all pixels to 'off'
colorWipe(strip.Color(255, 0, 0), 0); // Red
}
/// Arduino master switch
// Fill the dots one after the other with a color until solid red
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
for (int x=0; x < 50; x++) { // Wait for 1 second
delay(1);}
}}
// Strobe effect during firing (pin 6)
void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
for(int j = 0; j < StrobeCount; j++) {
setAll(red,green,blue);
showStrip();
delay(FlashDelay);
setAll(255,0,0);
showStrip();
delay(FlashDelay);
}}
/// WHEN POWER UP SWITCH IS PUSHED (pin 5)
void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
int Position=0;
for(int i=0; i<NUM_PIXELS*2; i++)
{
Position++; // = 0; //Position + Rate;
for(int i=0; i<NUM_PIXELS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
((sin(i+Position) * 127 + 128)/255)*green,
((sin(i+Position) * 127 + 128)/255)*blue);
}
showStrip();
delay(50);
}
}
void FadeInOut(byte red, byte green, byte blue){
float r, g, b;
for(int k = 255; k >= 0; k=k-2) {
r = (k/256.0)*red;
g = (k/256.0)*green;
b = (k/256.0)*blue;
setAll(r,g,b);
showStrip();
delay(5);
}
for(int k = 0; k < 256; k=k+1) {
r = (k/256.0)*red;
g = (k/256.0)*green;
b = (k/256.0)*blue;
setAll(r,g,b);
showStrip();
delay(5);
}}
void loop() {
val = digitalRead(Pin2); { // read input value
if (val == LOW) { // check if the input is HIGH
val = digitalRead(inPin); { // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
digitalWrite(8,HIGH);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
RunningLights(0xff,0,0, 500);
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}}
else {
Strobe(0xff, 0xff, 0xff, 10, 40, 1);
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
delay(20);
digitalWrite(12,LOW);
delay(20);
digitalWrite(8,HIGH);
delay(100);
digitalWrite(8,LOW);
delay(25);
digitalWrite(12,HIGH);
delay(20);
digitalWrite(12,LOW);
delay(20);
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(25);
digitalWrite(12,HIGH);
delay(20);
digitalWrite(12,LOW);
delay(20);
digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(25);
digitalWrite(12,HIGH);
delay(20);
digitalWrite(12,LOW);
delay(20);
digitalWrite(11, HIGH);
delay(100);
digitalWrite(11, LOW);
delay(25);
}
}}else {
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
FadeInOut(0xff, 0, 0);
}
}
}
// *** DON'T TOUCH BELLOW HERE - BELLEND!!!!!! ***
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_PIXELS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}