Project guidance

Hello Everyone,

I'm Eddie, mewbie here. I have been working with a friend of mine who programs LED's using Arduino's quite often. I've come to a sort of question. I've seen some people on youtube and other places use their LED's to simulate a "lightning" effect. The downside is that it seems like they are using 12v LED strips and i have 5v LED strips wired. Would it be possible to do the below?

On/off switch to light all 288 LED's one solid color
Push button switch to make the lightning effect

I have attached my wiring scheme and here is a link to the LED's i have wired.

What is the problem with using 5V LEDs as opposed to 12V LEDs ?

OPs image...

No problem doing what you want, just keep track of your LED power supply.

So below is the sketch i found. I wouldnt need the sound. I showed it to my buddy and he said it wouldnt work because of the difference in LED's.

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

int ledPin = 9; // LEDs (via MOSFET) connected to pin 9
int rxPin = 10; // DFplayer RX to Arduino pin 10
int txPin = 11; // DFplayer TX toArduinopin 11
int busyPin = 12; // DFplayer BUSY connected to pin 12

SoftwareSerial mySoftwareSerial(rxPin, txPin);
DFRobotDFPlayerMini myDFPlayer;

void setup()
{

pinMode(ledPin, OUTPUT);
pinMode(busyPin, INPUT);

mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println(F("Initializing DFPlayer..."));

if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin. Check connection and SD card, or reset the Arduino."));
while (true);
}

Serial.println(F("DFPlayer Mini online."));

myDFPlayer.setTimeOut(500); // Set serial communictaion time out 500ms
myDFPlayer.volume(30); // Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_BASS); // Set EQ to BASS (normal/pop/rock/jazz/classic/bass)
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // Set device we use SD as default
myDFPlayer.enableDAC(); // Enable On-chip DAC
}

void loop()
{
int flashCount = random (3, 15); // Min. and max. number of flashes each loop
int flashBrightnessMin = 10; // LED flash min. brightness (0-255)
int flashBrightnessMax = 255; // LED flash max. brightness (0-255)

int flashDurationMin = 1; // Min. duration of each seperate flash
int flashDurationMax = 50; // Max. duration of each seperate flash

int nextFlashDelayMin = 1; // Min, delay between each flash and the next
int nextFlashDelayMax = 150; // Max, delay between each flash and the next

int thunderDelay = random (500, 3000); // Min. and max. delay between flashing and playing sound
int thunderFile = random (1, 17); // There are 17 soundfiles: 0001.mp3 ... 0017.mp3
int loopDelay = random (5000, 30000); // Min. and max. delay between each loop

Serial.println();
Serial.print(F("Flashing, count: "));
Serial.println( flashCount );

for (int flash = 0 ; flash <= flashCount; flash += 1) { // Flashing LED strip in a loop, random count

analogWrite(ledPin, random (flashBrightnessMin, flashBrightnessMax)); // Turn LED strip on, random brightness
delay(random(flashDurationMin, flashDurationMax)); // Keep it tured on, random duration

analogWrite(ledPin, 0); // Turn the LED strip off
delay(random(nextFlashDelayMin, nextFlashDelayMax)); // Random delay before next flash
}

Serial.print(F("Pausing before playing thunder sound, milliseconds: "));
Serial.println(thunderDelay);
delay(thunderDelay);

Serial.print(F("Playing thunder sound, file number: "));
Serial.println(thunderFile);
myDFPlayer.playMp3Folder(thunderFile);
delay(1000); // Give the DFPlayer some time

while (digitalRead(busyPin) == LOW) { // Wait for the DFPlayer to finish playing the MP3 file
}

Serial.print(F("Pausing before next loop, milliseconds: "));
Serial.println(loopDelay);
delay(loopDelay);

}

Exactly what type of LEDs do you have ?

Assuming you're using 2x 144-pixel Neopixel strips, you can try this. It's lightning logic is based on the code you showed but adapted (hopefully!) for the Neopixel. It compiles but I have no idea if it works.

#include <Adafruit_NeoPixel.h>

#define SW_READ_INTERVAL        50ul            //mS    time between button reads (minimum)

const uint16_t  numLEDs = 288;                  //#     number of LEDs in Neopixel strip
const byte pinLEDs = 6;                         //pinNo pin number of dataline to Neopixels
const byte pinSW = 9;                           //pinNo pin number of button/switch

const byte flashNumMin =  3;                    //#     minimum number of flashes per sequence
const byte flashNumMax =  9;                    //#     maximum number of flashes per sequence
//
const byte flashBrightnessMin =  10;            //#     minimum brightness of flash
const byte flashBrightnessMax =  255;           //#     maximum brightness of flash
//
const int flashDurationMin = 1;                 //mS    minimum duration of flash
const int flashDurationMax = 50;                //mS    maximum duration of flash
//
const int flashDelayMin = 1;                    //mS    minimum period between flashes
const int flashDelayMax = 150;                  //mS    maximum period between flashes
//
byte lSw;

Adafruit_NeoPixel neoPix = Adafruit_NeoPixel( numLEDs, pinLEDs, NEO_GRB + NEO_KHZ800);

void setup() 
{
    //init the neoPix object
    neoPix.begin();
    //and set the switch input mode
    pinMode( pinSW, INPUT_PULLUP );    
    //establish current switch status
    lSw = digitalRead( pinSW );
    
}//setup
 
void loop() 
{
    //run the state machine every loop
    LightningStateMachine();
    
}//loop


//state names
#define WAIT_BUTTON     0
#define FLASH_LIGHTNING 1
//
#define TURN_ON         0
#define TIME_ON         1
#define TIME_OFF        2
//
void LightningStateMachine( void )
{
    byte
        nSw;
    static int
        flashCount;
    static unsigned long
        timeLightning,
        timeLightningDelay,
        tSw;
    unsigned long
        tNow;
    static byte
        flashBright,
        stateFlash = TURN_ON,
        stateLightning = WAIT_BUTTON;

    tNow = millis();
    switch( stateLightning )
    {
        case    WAIT_BUTTON:
            //check for the button pin to go from high to low
            if( (tNow - tSw) >= SW_READ_INTERVAL )
            {
                //check for the button pin to go from high to low
                tSw = tNow;
                nSw = digitalRead( pinSW );
                if( nSw != lSw )
                {
                    //not the same as last time...
                    lSw = nSw;
                    //is it low now?
                    if( nSw == LOW )
                    {
                        //yes; button was pushed.
                        //set up the number of flashes and then move to the FLASH state
                        flashCount = random( flashNumMin, flashNumMax );
                        stateLightning = FLASH_LIGHTNING;
                        
                    }//if
                    
                }//if
                
            }//if
            
        break;

        case    FLASH_LIGHTNING:
            //if we're done flashes...
            if( flashCount == 0 )
            {
                //...init the button read timer and then move back to read the button again
                stateLightning = WAIT_BUTTON;
                tSw = tNow;
                
            }//if
            else
            {
                //for flashing the lightning, our states are...
                switch( stateFlash )
                {
                    case    TURN_ON:
                        //turn on the strip at a random brightness and for a random time...
                        timeLightningDelay = (unsigned long)random( flashDurationMin, flashDurationMax );
                        flashBright = random( flashBrightnessMin, flashBrightnessMax );
                        uint32_t fillcol = neoPix.Color( flashBright, flashBright, flashBright ); 
                        neoPix.fill( fillcol, 0, neoPix.numPixels() );                                        
                        //...and show it 
                        neoPix.show();
                        //set up to time the on-time and move that state
                        timeLightning = millis();
                        stateFlash = TIME_ON;
                        
                    break;

                    case    TIME_ON:
                        //we're timing the on-time of the flash...
                        if( (tNow - timeLightning) >= timeLightningDelay )
                        {
                            //once done, turn off the strip...
                            neoPix.clear();                                        
                            neoPix.show();
                            //and set up for a random interflash delay
                            timeLightning = tNow;
                            timeLightningDelay = (unsigned long)random( flashDelayMin, flashDelayMax );
                            //then move to time that
                            stateFlash = TIME_OFF;
                                
                        }//if
                        
                    break;

                    case    TIME_OFF:
                        //when the interflash delay is done...
                        if( (tNow - timeLightning) >= timeLightningDelay )
                        {
                            //we're done one more flash; decrement the flash count...
                            flashCount--;
                            //and go back and light it up again
                            stateFlash = TURN_ON;
                                
                        }//if
                        
                    break;
                    
                }//switch
                
            }//else
            
        break;
        
    }//switch
    
}//LightningStateMachine

thank you for taking the time to put this together. I tried this code. when i push the button with this code, it starts the flash but its so minimal. You have to like squint your eyes to see that it may be flashing.

Loleddie:
thank you for taking the time to put this together. I tried this code. when i push the button with this code, it starts the flash but its so minimal. You have to like squint your eyes to see that it may be flashing.

Try playing with these constants:

const byte flashBrightnessMin =  10;            //#     minimum brightness of flash
const byte flashBrightnessMax =  255;           //#     maximum brightness of flash
//
const int flashDurationMin = 1;                 //mS    minimum duration of flash
const int flashDurationMax = 50;                //mS    maximum duration of flash

As experiments, you might try:

  • making the minimum brightness 200 or something, just as an experiment, and

  • making the min flash duration 100mS and the max 500mS, again as an experiment.

If the results are promising, maybe make the limits a bit lower until you're happy with the result.

Are you using 800kHz-capable Neopixels (2812) or the 400kHz ones (2811)?