Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Lightning Effect
 
Share:
Notifications
Clear all

[Solved] Lightning Effect

14 Posts
5 Users
0 Likes
5,238 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

This topic continues a conversation from the Led Effects article ... (link)

Original post:

Hi, Hans,

You helped me a few years ago with a sketch and now I’m back for more. . I’m making a cloud lamp and I’d like to have a lightning effect inside. I have an Arduino Mega and a short strip of WS2812B NeoPixels. I want the effect to be random, like real lightning. For example, three quick flashes, dark for several seconds, then a slower flash fading up; that kind of thing. I thought I could edit the strobe sketch or the Halloween eyes, but I don’t understand how to use the random function. Would love some help! 

Thanks,

Claire


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

I assumed you are using the FastLED variant, so here the modified Strobe code (untested, since I have no hardware laying around at the moment to test this):

#include "FastLED.h"
#define NUM_LEDS 60 
CRGB leds[NUM_LEDS];
#define PIN 6 
byte red = 0xff;
byte green = 0xff;
byte blue = 0xff;
int numberOfFlashes;
int delayBetweenFlashes;
int pauseAfterEffect; 
void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  randomSeed(analogRead(0));
}
// REPLACE FROM HERE
void loop() { 
  numberOfFlashes=random(10); // random number between 0 and 10
  delayBetweenFlashes=random(100); // random number between 0 and 100
  pauseAfterEffect=random(1000); // random number between 0 and 1000
  Strobe(red, green, blue, numberOfFlashes, delayBetweenFlashes, pauseAfterEffect);
}
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(0,0,0);
    showStrip();
    delay(FlashDelay);
  }
 
 delay(EndPause);
}
// REPLACE TO HERE
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_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

Give it a try and let me know how this worked out so we can continue from there ... 


   
ReplyQuote
(@gbonsack)
New Member
Joined: 7 years ago
Posts: 4
 

What I want to do is press a number, on a 3 X 4 keypad and have the sketch run a 35 second light show loop (I do light painting, so I start the show and start walking, while the photographers take a 30 second exposure.

When I compile this sketch, I get an error message for line 141 and 3 more for line 201:

Error message given for line 141: 'theaterChase' was not declared in this scope

Error messages given for line 201: expected unqualified-id before 'for'

for (int j = 0; j < 15; j++) { //do 10 cycles of chasing, changed to 15 for length

error: 'j' does not name a type & error: 'j' does not name a type

I have re-typed several line before and after said line, without any success, any suggestion other than trash Windows 10 and switch to Linux or get a MAC.


   
ReplyQuote
(@gbonsack)
New Member
Joined: 7 years ago
Posts: 4
 

The above keypad_test .ino is a version of my modified Button_Cycler_Mine.ino, which works great, but if I want loop (case) 5, I do not want to run the previous 4, just to run 5 and then power down and repeat for a second shot.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Since the Arduino fully resets after a power loss, and since the Arduino isn't the greatest when it comes to multi tasking, you might want to consider using a multi-position switch or a rotary selector switch. They can be found in several different shapes ... from rotating to sliding, small, big, multi layer etc.

In your case you'd need only 5 or 6 positions, single layer ... position 6 could be "OFF" for example.
The advantage would be that the last used position is remembered (unless you change the position of the switch of course).

You can choose to use 5 pins or make a more complex setup by using an analog pin and connecting a different resistor per pin. That last solution obviously makes things a little more complicated, but will spare you the use of pins if you would need pins for other purposes.

There are other more advanced methods of course, by using an IC to handling things (see this Arduino Forum topic).


   
ReplyQuote


(@gbonsack)
New Member
Joined: 7 years ago
Posts: 4
 

Hans,

I found the issue with my keypad sketch, when I re-typed one of the 240/302 code lines, I corrected the 240/302 error but accidentally added another letter to the code line - resulting in the "Not Declared" error 50 to 60 lines above that line - "FAT FINGERS".

At the moment, I am 3D Printing parts for the keypad holder, battery pack, Arduino holder and light stick spinner. I should have everything complete and wired together within a couple of day, so I can post a link to a sample video and/or a sample image. Revised code is attached.

Like I said above, the push button (button_cycler_mine.ino) advance sketch worked, but the keypad sketch was full of error message (cut and paste, using our favorite OS).


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Oh wow, nice! I played with 3D printers for a while, but have to admit that it's not ready for use by me yet hahah ...
Too much work and tweaking to get "normal" printing going and no budget for the more expensive ones. 

I'm sorry for not being so much present the past days - work and hobby are seriously beginning to conflict.
One of these days hahah ... I love this stuff more than my job though ... can't wait to see your 3D print!
I'll try to find some time this week to go through your code.


   
ReplyQuote
(@gbonsack)
New Member
Joined: 7 years ago
Posts: 4
 

Here is the working code. I put the YouTube link on the main page.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Awesome! 
But couldn't find the link on the main page?


   
ReplyQuote
(@claire)
New Member
Joined: 7 years ago
Posts: 1
 

Hi, Hans,
I seem to be back on now. Refreshing the page helps. I tried the sketch and got a lot of error messages. 
  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Mac OS X), Board: "Arduino Mega 2560 or Mega ADK"
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h: In member function 'void ClocklessController<DATA_PIN, T1, T2, T3, RGB_ORDER, XTRA0, FLIP, WAIT_TIME>::showPixels(PixelController<RGB_ORDER, 1, -1ul>&) [with unsigned char DATA_PIN = 6u, int T1 = 6, int T2 = 8, int T3 = 6, EOrder RGB_ORDER = GRB, int XTRA0 = 0, bool FLIP = false, int WAIT_TIME = 10]':
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:368: error: can't find a register in class 'POINTER_REGS' while reloading 'asm'
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:368: error: can't find a ...

register in class 'POINTER_REGS' while reloading 'asm'
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:368: error: 'asm' operand has impossible constraints
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:368: error: 'asm' operand has impossible constraints
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:369: error: 'asm' operand has impossible constraints
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:369: error: 'asm' operand has impossible constraints
...
/Users/claire/Documents/Arduino/libraries/FastLED-3.1.3/platforms/avr/clockless_trinket.h:449: error: 'asm' operand has impossible constraints

   
ReplyQuote


 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Hi Claire,

that seems to be a problem with the FastLED library.

Since I have never seen these errors, maybe you'd want to consider reinstalling everything;

1) download the latest Arduino IDE version,
2) Download and install the latest FastLED library.

I think it's the Arduino application that you're using that is very old. 


   
ReplyQuote
(@wison)
New Member
Joined: 6 years ago
Posts: 1
 

Hi Hans, 

I can't find in libraries   <Patterns.h> <Key.h> have any link to download?

Thank you!


   
ReplyQuote
(@1234abcd)
Active Member
Joined: 5 years ago
Posts: 11
 

Great and easily customisable lightning effect from Andrew Tuline, awesome in a tree with 10 or more APA102C strips...

/*
 * 
 * Lightning (Andrew Tuline) https://github.com/atuline/FastLED-Demos/blob/master/lightnings/lightnings.ino
 * 
 */

#include "FastLED.h" // FastLED library. Please use the latest version

// Fixed definitions. Cannot be changed interactively
#define LED_DATA 3 // Data pin
#define LED_CLOCK 4 // Clock pin
#define COLOR_ORDER BGR // GRB for WS2812 and BGR for APA102
#define LED_TYPE APA102 // Using APA102, WS2812, WS2801 - don't forget to change LEDS.addLeds
#define NUM_LEDS 144 // Number of LEDs

// Global variables. Can be changed interactively
uint8_t max_bright = 255; // Overall brightness

struct CRGB leds[NUM_LEDS]; // Initialise LED array

uint8_t frequency = 40; // Controls the interval between strikes
uint8_t flashes = 7; // The upper limit of flashes per strike
unsigned int dimmer = 1;
uint8_t ledstart; // Starting location of flash
uint8_t ledlen; // Length of flash

void setup() {
  
  delay(1000);
  
  LEDS.addLeds<LED_TYPE, LED_DATA, LED_CLOCK, COLOR_ORDER>(leds, NUM_LEDS); // For WS2801 or APA102
  
  FastLED.setBrightness(max_bright);
  
}

void loop() {
  
  ledstart = random8(NUM_LEDS); // Determine starting location of flash
  ledlen = random8(NUM_LEDS-ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
  
  for (int flashCounter = 0; flashCounter < random8(2,flashes); flashCounter++) {
    if(flashCounter == 0) dimmer = 6; // The brightness of the leader is scaled down by a factor of 6
    else dimmer = random8(1,4); // Return strokes are brighter than the leader
    
    fill_solid(leds+ledstart,ledlen,CHSV(255, 0, 255/dimmer));
    FastLED.show(); // Show a flash
    delay(random8(3,11)); // Each flash only lasts 3-11 milliseconds
    
    fill_solid(leds+ledstart,ledlen,CHSV(255,0,0)); // Clear the section of LEDs
    FastLED.show();
    
    if (flashCounter == 0) delay (150); // Longer delay until next flash after the leader
    
    delay(50+random8(100)); // Shorter delay between strokes
    
  }
  
  delay(random8(frequency)*100); // Delay between strikes
  
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Awesome! 


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: