Author Topic: Digispark Smart PWM for Effect?  (Read 5829 times)

SPyKER

  • Newbie
  • *
  • Posts: 3
Digispark Smart PWM for Effect?
« on: September 07, 2012, 10:47:51 pm »
You guys are onto something really big here.

I'm not an Arduino programmer, yet, so I need to know if my idea is feasible, or even possible.

I make motorcycle accessories, mostly an intake mod, which can be lit up from the inside. A few years ago, a friend used to hardwire 2-3 flashing circuits into tiny LED boxes (red/yellow/orange, Blue/yellow, etc) to make cool flame effects.

The systems run off a 12VDC battery system.

What I would like to know is this: how hard would it be to have the Digispark control 2-3 banks of lights, and be smart enough to know if it was 2 or 3?

The power to the lights would have to go through some sort of MOSFET I believe. Realistically, there would be three pair of wire connectors. The main idea is that the PWM cycles would vary to be "random" to simulate the flames. I supose these could all be set to "random" length cycles and the unit wouldn't need to know if there were 2-3, but the real goal is for no LEDs to ever be all the way off.

I work with Custom Dynamics as a reseller, and their Stingers come in 12 different colors, so the combination possibilities are pretty cool!

Oh, and one final issue: It would have to go into a water-tight unit.

Anyone up for the challenge? Or even able to help me learn how to do this myself? I'm no electronics engineer, but I have written web and app code for years, so I'm teachable.

Thanks for you time!
« Last Edit: September 07, 2012, 10:52:07 pm by SPyKER »

phiamo

  • Newbie
  • *
  • Posts: 2
Digispark Smart PWM for Effect?
« Reply #1 on: October 09, 2012, 07:39:47 am »
Hi,
would be a nice challange, what kind of interface uses the Stingers? afair from they homepage its just a led strip without any interface ...

SPyKER

  • Newbie
  • *
  • Posts: 3
Digispark Smart PWM for Effect?
« Reply #2 on: October 11, 2012, 10:27:53 am »
phiamo,

You are right. This would be for use with wired singles or strips of lights, attached to screw mount sets. These would be used on things like models and panoramas, vehicles, computer cases, holiday decorations, and more. My biggest challenge is to create it so that it can handle both small DC and alternated DC. And I\'d trying to use mosfets instead of relays for the noise factor.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Digispark Smart PWM for Effect?
« Reply #3 on: October 11, 2012, 01:27:04 pm »
3 MOSFETs (for three channels) would do this no problem, detecting how many are attached is the only challenge but could easily be done with an extra pin on the connector - so if the LED connector was 12V,GND,SENSE and when connected sense was tied to ground then the Digispark could read when that pin was at ground and know something was connected. If you interested in a volume you could email me and I could make a custom shield for it with 3 MOSFETs, connectors, and all the other required parts.

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Digispark Smart PWM for Effect?
« Reply #4 on: November 03, 2012, 06:01:13 pm »
Maybe easier to just use transistors? The common 2n2222 can switch up to about an amp, and they\'re only slightly more expensive than air. Detecting how many strips are attached would be no problem - two of the PWM channels are also analog inputs - so just set those pins to inputs, digitalWrite them high to turn on the pullup, and do an analogRead - if the value is over say 800, nothing is connected. If a transistor is connected, it will get rid of that power and keep the value closer to about 120. It would be more difficult to do this sort of detection on a MOSFET I think.

Do you need to detect them? I use attiny85\'s for lighting control in art installations and we just program all their pins to be outputting the effects we want and wire on to as many as we need during the installation.

For a flame effect, you\'ll want a very specific kind of randomness - flames are not purely random (white noise) as they have physics of air working on them, so they have motion, momentum - they are affected by randomness though. I\'ve found Perlin Noise works pretty well for candle sorts of effects - included below is some code for a really simple 8-bit perlin-like digital noise generator which I use for little candle-like effects:

/* Space Plumber\'s random */
unsigned int sp_random(unsigned int seed, unsigned int range) {
  seed = (seed * 58321) + 11113;
  return (seed >> 8) % range;
}

byte perlin(unsigned int num) {
  byte output = 0;
  byte idx;
  for (idx = 0; idx < 8; idx++) output |= sp_random(num / (1 << idx), 2) << idx;
  return output;
}


What this little piece of code does is create random 8-bit numbers where the each bit gets a new random value half as often as a less significant bit - effectively big changes are slower than littler ones. To get your colour values just give perlin() a number, and increment that number between each call. It is a true function in that it always returns the same value when given the same argument. Something like this:

// random starting values so the lights all begin at different positions
unsigned int position_1 = 1298;
unsigned int position_2 = 21832;
unsigned int position_3 = 48726;

void loop() {
   position_1 += 1;
   position_2 += 1;
   position_3 += 1;
   
   byte value_1 = perlin(position_1);
   byte value_2 = perlin(position_2);
   byte value_3 = perlin(position_3);
   
   // now you have three perlinish random values between 0-255 inclusive
   // update your PWM outputs in the usual ways - to get the Never Totally Off
   // effect, do something like value_1 = (value_1 / 2) + 127;
   // so your range becomes 127-255 inclusive
   
   delay(1); // change this number to be higher for a slower effect, or lower for faster
   // more frantic effect
}


For motorcycles because of the speed of movement you\'ll also need to deal with persistence of vision - there\'s a bit of a trade off - if you run the PWM too slowly the lights will look like they\'re strobing to observers - too fast and the colour of the LED light can change subtly and the MOSFETs or transistors might not be able to keep up with it. Still, I\'d try to run the timer\'s as fast as possible - which on the digispark means updating 62,500 times per second - fast enough to avoid persistence of vision strobing and only move down from that speed if it\'s causing problems with your switches or colour reproduction.

To set timer 0 to run at the highest speed, add this line to your setup: TCCR0B = (1 << CS00) | (TCCR0B & 0b11111000); and for timer 1: TCCR1 = (1 << CS10) | (TCCR1 & 0b11110000); - these lines use some bitwise logic to remove whatever existing speed settings the two digispark timers are configured to, and change it to the highest speed. Note after this change has been made, timer related functionality like millis() and micros() will probably report inaccurate numbers. You\'ll probably also want to run noInterrupts() in your setup function to disable those systems entirely, which will improve the performance of the lighting - removing hiccups where every now and then an animation update takes longer than usual to complete.

Hope this helps!

P.S. hot-melt glue works pretty well for waterproofing. I often dip electronics in a regular wax, but that probably wouldn\'t work out so well on a bike, where it needs to withstand regular shocks which might crack wax. Another option is casting in resin. If you use lead free solder, you should be able to subject your electronics to temperatures of up to 150°C without problems - most resin sets at around 60°C if I recall, so it\'s alright for casting. I sometimes bake electronics in to Sculpey - a kind of thermosetting polymer originally invented for creating transformers, but later marketed for arts and crafts. I\'ve never had any issues, but you do need to be a little careful that the plastic insulation on any wires are going to withstand whatever temperatures. Digistump are required to use lead free solder for health reasons so I wouldn\'t worry about the components on those PCBs coming off at temperatures under about 200°C. There are some pretty sturdy forms of wax which might be worth checking out too. You can do stuff like put the electronics in a small box, then pour in a wax or other kind of solid oil.

If some water does get in there, it isn\'t catastrophic - generally water\'s resistance is high enough that the short circuits it creates wont harm the digispark or other bits and pieces, but it can cause odd behaviour or dimming or crosstalk between the LEDs. Water near the MOSFETs or transistors will be most noticeable as they require minimal current to activate (especially MOSFETs).

But in summary the digispark will work great for this application, especially as you can hook them straight on to the 12v or 6v power supplies in the bike - adding fun stuff like that to my motorbike is one of the things I\'m looking forward to doing when mine arrive ^_^

Be a bit careful if hooking this stuff on to electronic bikes running at over about 24 volts, as the digispark may not handle it okay, and as always make sure you\'re using the right resistors on your LEDs for whichever voltage - premade automotive strips of lights have them built in so no worries there.

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Digispark Smart PWM for Effect?
« Reply #5 on: November 03, 2012, 06:06:00 pm »
Oh and on the blue-yellow combination - I can\'t speak for the laws of your state/nation, but where I live it\'s very very illegal to have red-blue flashing lights on street vehicles when you aren\'t a government emergency worker, so be a bit careful about combining reddish hues with blues if there are similar laws where you are.