Welcome, %1$s. Please login or register. - Did you miss your activation email?

Login with username, password and session length
 

Author Topic: Flickering LEDs  (Read 1071 times)

Offline Mstowell

  • Admiral
  • *****
  • Posts: 719
  • Gort! Barenga!
Flickering LEDs
« on: September 06, 2016, 11:12:43 am »
Hello all,
I am wanting to try setting up some flickering LEDs to simulate the impulse engines on TOS 1701.
I found some examples of code.  The code references a True Random library that I need to load, but I have not been able to find where I can get that library.  I have searched for it on yahoo and google, but to no avail.
Does anyone have functioning code for doing this?
I am wanting to integrate it into a sketch that also does the strobe and navigation lights, so that I can use just 1 board.

Thanks,
Matt
Matt

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1339
    • MW ModelWerks
Re: Flickering LEDs
« Reply #1 on: September 07, 2016, 05:52:01 pm »
Ok...

Here's some code that randomly turns on LEDs across 5 pins.  Right now, it is set to randomly turn on the LED from between 75 and 150ms, then keep it off for the same intervals.  By playing with the low number and the max number in the random() function, you should be able to get the level of flicker you want.

// Blink 1 LED at a random interval (0.075 second to 0.150 seconds) without using delay()
//Curcuit:  LEDs connected to pin 10, 12, 13, 1  & 2, all with 220 ohm resistors to ground.

#define numberOfLEDs 5
long nextFlash[5];
int ledPin[] = { 2, 1, 10, 12, 13}; // LED pins to use.
int ledState[5];

void setup(){
 for(int i = 0; i<numberOfLEDs; i++){
   pinMode(ledPin,OUTPUT);
   ledState = LOW;
   digitalWrite(ledPin, LOW); // all LEDs off
     nextFlash = millis() +random(75, 150);
 }
}

void loop(){
for(int i = 0; i<numberOfLEDs; i++){
if(millis() > nextFlash){
  if(ledState == LOW) ledState = HIGH; else ledState = LOW;
  digitalWrite(ledPin,ledState);
  nextFlash = millis()+random(75, 150) ; // next toggle random time
} }
}
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline Mstowell

  • Admiral
  • *****
  • Posts: 719
  • Gort! Barenga!
Re: Flickering LEDs
« Reply #2 on: September 08, 2016, 09:11:06 am »
I copied sketch to IDE.
Ran sketch.
Got the following error.

"exit status 1
incompatible types in assignment of 'int' to 'int [5]'"

I am using a Uno R3
Matt

Offline NCC1966

  • Commander
  • ***
  • Posts: 146
Re: Flickering LEDs
« Reply #3 on: September 08, 2016, 11:08:14 am »
It's broken and won't work. It has several mistakes, among them ledState, ledPin and nextFlash are defined as arrays and along the code are tried to be accessed as single vars. I just stopped to check the code after that.

 ::)

These generic non tested scripts picked from Internet can be really awful.

If you want my advice, copy and paste someone else's code is not the ideal scenario specially when you are trying to control hardware. You should try to learn some coding and write your own otherwise you will be always bumping with such frustrating situations.

 ;)
« Last Edit: September 08, 2016, 11:10:09 am by NCC1966 »

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1339
    • MW ModelWerks
Re: Flickering LEDs
« Reply #4 on: September 08, 2016, 07:48:50 pm »
Okay, NCC, before you go too far, I wrote that code.  It's not some script I pulled off the internet.  Yes it's broke--you spotted the arrays were an issue, but missed the index issue.

What isn't obvious, and has bitten me before when posting code in this forum is:
[ i ]

It turned my array position indicators and loop counter into italics for the post, which is pretty obvious when you look at the bottom half of the code.
So to get around HTML script in the post, try this version (replace [ i ] with [j]):

// Blink 1 LED at a random interval (0.075 second to 0.150 seconds) without using delay()
//Circuit:  LEDs connected to pin 2, 10 & 12 all with 220 ohm resistors to ground.

#define numberOfLEDs 1
long nextFlash[1];
int ledPin[] = { 2, 10, 12}; // LED pins to use.
int ledState[1];

void setup(){
 for(int j = 0; j<numberOfLEDs; j++){
   pinMode(ledPin[j],OUTPUT);
   ledState[j] = LOW;
   digitalWrite(ledPin[j], LOW); // all LEDs off
     nextFlash[j] = millis() +random(75, 150);
 }
}

void loop(){
for(int j = 0; j<numberOfLEDs; j++){
if(millis() > nextFlash[j]){
  if(ledState[j] == LOW) ledState[j] = HIGH; else ledState[j] = LOW;
  digitalWrite(ledPin[j],ledState[j]);
  nextFlash[j] = millis()+random(75, 150) ; // next toggle random time
} }
}

This compiles, and you can see it run here:
https://circuits.io/circuits/2675901-the-unnamed-circuit/edit#breadboard

Note the run time is not real time, so you'll need to play with the 75 and 150ms to get the look you want.  You can increase the number of LEDs by changing #define numberOfLEDs 1 to whatever number you want, and making the array ledPin[] have a matching number of pins.

Mstowell:  NCC is right about learning for yourself.  It took me several weeks of experimentation and learning to develop the above.  I originally needed code to randomly flash 5 LEDs; this was not something I could readily find on the internet for a cut and paste.  As i figured out what i wanted, I began to learn what different things did and was able to put them together.  I also had to teach myself what the different functions meant to make sense of it.  Like what the heck == means versus = ...
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1339
    • MW ModelWerks
Re: Flickering LEDs
« Reply #5 on: September 09, 2016, 09:36:53 am »
One other obvious answer on this is to go to the dollar store and get a cheap flickering LED candle, and tear it apart.  Using an Arduino just for this may be a bit overkill.
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline madmonk

  • Commander
  • ***
  • Posts: 231
Re: Flickering LEDs
« Reply #6 on: September 09, 2016, 10:22:24 am »
I use the following code on some of my lighting kits to flicker a couple of LEDs at different rates.

int ledPin1 = 1;
int ledPin2 = 0;

void setup() {
 
pinMode(ledPin2, OUTPUT);
pinMode(ledPin1, OUTPUT);

}
void loop() {

analogWrite(ledPin2, random(150, 255));
delay(random(100));
analogWrite(ledPin1, random(150, 255));
delay(random(100));
 }

The delay function makes it difficult to use with other code and I tend to use it on an ATtiny85 chip, will work on a Nano or pro but probably over kill.  Just make sure the pins you use are PWM ones.

The (150,255) function is a min/max setting for the output so the LED never fully goes out when flickering.

Offline NCC1966

  • Commander
  • ***
  • Posts: 146
Re: Flickering LEDs
« Reply #7 on: September 09, 2016, 04:48:26 pm »
Okay, NCC, before you go too far, I wrote that code.  It's not some script I pulled off the internet.

I didn't mean to offend you. If I did, I apologize. Actually my message was for Mstowell in the sense that he shouldn't just pick code around and hope that it work. You wouldn't believe the amount of crazy codes you can find for download on the web that doesn't work. Beside the cracked codes, in the case of Arduino there are still scripts that will work for one board and not for other. Yup, the cards aren't all same. They supposed to be but unfortunately when it comes to the real world things can be different.

Anyway when a non-coder try to copy-and-paste codes harvested around hoping that things will work he/she can really suffer with the frustration. Trust me, even being a coder myself for 30+ years sometimes I feel this frustration!

  :(

But again, I APOLOGIZE for any inconvenience that my acid comment may be caused to you!

 :-[

Now Madmonk has published an interesting post...

I use the following code on some of my lighting kits to flicker a couple of LEDs at different rates.

int ledPin1 = 1;
int ledPin2 = 0;

void setup() {
 
pinMode(ledPin2, OUTPUT);
pinMode(ledPin1, OUTPUT);

}
void loop() {

analogWrite(ledPin2, random(150, 255));
delay(random(100));
analogWrite(ledPin1, random(150, 255));
delay(random(100));
 }

The delay function makes it difficult to use with other code and I tend to use it on an ATtiny85 chip, will work on a Nano or pro but probably over kill.  Just make sure the pins you use are PWM ones.

The (150,255) function is a min/max setting for the output so the LED never fully goes out when flickering.


I tested this code and it is very good for its simplicity. However I had a couple small problems with it and that was actually easy to fix. First the first value of random function as 150 didn't work for me (the LED kept continuously lighted) and changing it to a smaller value fixed the problem. Actually I changed it to 0 (zero).

The other problem was with the pins. Not ALL Arduino pins accept the AnalogWrite command (see it here: https://www.arduino.cc/en/Reference/AnalogWrite) and pins 1 and 2 aren't among the ones that it will work with the Uno R3. I changed it for pins 12 and 13 and it worked.

Here the complete code fixed for Uno R3 board:

int ledPin1 = 12;
int ledPin2 = 13;

void setup() {
 
pinMode(ledPin2, OUTPUT);
pinMode(ledPin1, OUTPUT);

}
void loop() {

analogWrite(ledPin2, random(0, 255));
delay(random(100));
analogWrite(ledPin1, random(0, 255));
delay(random(100));
 }


 :)

Offline Mstowell

  • Admiral
  • *****
  • Posts: 719
  • Gort! Barenga!
Re: Flickering LEDs
« Reply #8 on: September 09, 2016, 05:16:29 pm »
It's broken and won't work. It has several mistakes, among them ledState, ledPin and nextFlash are defined as arrays and along the code are tried to be accessed as single vars. I just stopped to check the code after that.

 ::)

These generic non tested scripts picked from Internet can be really awful.

If you want my advice, copy and paste someone else's code is not the ideal scenario specially when you are trying to control hardware. You should try to learn some coding and write your own otherwise you will be always bumping with such frustrating situations.

 ;)

I discovered some years ago that I will never be a coder.  I don't have the patience, time or inclination to fully write my own code.  But I can read enough to try taking existing code. 
Matt

Offline simi

  • Admiral
  • *****
  • Posts: 588
  • JUST enough knowledge to do some serious damage
Re: Flickering LEDs
« Reply #9 on: September 09, 2016, 06:17:58 pm »
It's broken and won't work. It has several mistakes, among them ledState, ledPin and nextFlash are defined as arrays and along the code are tried to be accessed as single vars. I just stopped to check the code after that.

 ::)

These generic non tested scripts picked from Internet can be really awful.

If you want my advice, copy and paste someone else's code is not the ideal scenario specially when you are trying to control hardware. You should try to learn some coding and write your own otherwise you will be always bumping with such frustrating situations.

 ;)

I discovered some years ago that I will never be a coder.  I don't have the patience, time or inclination to fully write my own code.  But I can read enough to try taking existing code.

I'm a coder - but totally identify with this.  Find code online (google-fu) that does what or something like you want and modify to your tastes.  No need to re-invent the wheel unless that's the kind of thing you like to do (and that's cool too).

Cheers!

Simi
As a software architect, I'm pretty darn good.  As someone with knowledge of building things in the real world, well, I'm a software architect.

Offline madmonk

  • Commander
  • ***
  • Posts: 231
Re: Flickering LEDs
« Reply #10 on: September 10, 2016, 02:41:49 am »
NCC, thanks for the comments and correction, I use it on an ATtiny 85 chip rather than an Uno as that's easier to hide in a kit, pins 1 and 2 on the 85 are the PWM pins.  I run the chip and LEDs at 4.5V which gives a nice subtle flicker.

Offline NCC1966

  • Commander
  • ***
  • Posts: 146
Re: Flickering LEDs
« Reply #11 on: September 10, 2016, 07:32:07 am »
I'm a coder - but totally identify with this.  Find code online (google-fu) that does what or something like you want and modify to your tastes.  No need to re-invent the wheel unless that's the kind of thing you like to do (and that's cool too).
Agree, I am a pro coder here too (30+ years) and I do that all the time. What I meant is that there is a big difference on doing that when you master coding and when you don't.

 ;)

 




Enterprise © Bloc