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!



sketch modification...
 
Share:
Notifications
Clear all

[Solved] sketch modifications

2 Posts
2 Users
0 Reactions
2,052 Views
 ace
(@ace)
New Member
Joined: 6 years ago
Posts: 1
Topic starter  
I would like some help to modify your All Effects sketch.  I'll start with just the Twinkle effect since that's mainly what I want right now.  I'm using Uno.
1.  I have an older version of the sketch where there was a "#define BRIGHTNESS" at the top, but I noticed in the current version, it isn't there.  Where in the current version can you change the brightness?
2. I want to eliminate all instances of delay and use millis, but I am lost
3. I also want to add a potentiometer to adjust the speed.  Here is the sketch I am using:
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 23

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
Twinkle(0xff, 0xff, 0xff, 10, 100, false);
}

void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0,0,0);

for (int i=0; i<Count; i++) {
setPixel(random(NUM_LEDS),red,green,blue);
showStrip();
delay(SpeedDelay);
if(OnlyOne) {
setAll(0,0,0);
}
}
delay(SpeedDelay);
}

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.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
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
I noticed when this twinkle sketch runs, there is a very slight pause after it "fills in", before it clears and starts over.  Is there a way to fix this so this pause isn't there (maybe using millis instead of delay)?  I've tried changing the number of pixels that are on at once (10 in this example), but it doesn't help.  Thank you.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Ace!

Hmm,... I do not recall "#define BRIGHTNESS" in the original twinkle code, but I could be wrong.
I do know that NeoPixel does not have a global brightness function, but FastLED (the library I'd use) does: setBrightness().
So if you switched to FastLED then you could call that function any time. Also note that FastLED is a faster and more mature library for controlling LEDs. 

Delay vs Millis; Millis would allow more accurate time, and is supposed to be non-blocking (if you write the code correctly). I have to admit that I have not really used it as a replacement for Delay though. So I'm not sure how suitable it will be for your purpose.
I found this article which gives a good explanation - I hope it's helpful.

Using a potentiometer for speed is most certainly possible.
What I'd do, and I'm not saying this is the only and most correct solution, is write your own "delay()" function.
In that function you'd read the potentiometer value and use it, or multiply the indicated delay with that and then do the actual delay.

Something like this:

#define PotPin 2
...
void myDelay(unsigned long milliseconds) {  // milliseconds = max delay
  int PotVal;
  PotVal = analogRead(potPin);  // analogRead reads a value 0 ... 1023
  delay( (PotVal * milliseconds)/1023 );
}

I have not tested this code, but it could be a good start. You'd call the function like the delay(X) function: myDelay(X).
This may not respond instantly and smoort, again I have not tested this.

To reduces the "pause" in Twinkle, you'll have to keep in mind that each time the function is called it blanks all LEDs, and after that a count number of times does the effect, and then executes a Delay.

So to speed things up, you could do this;

void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0,0,0);
  
  for (int i=0; i<Count; i++) {
     setPixel(random(NUM_LEDS),red,green,blue);
     showStrip();
     delay(SpeedDelay);
     if(OnlyOne) { 
       setAll(0,0,0); 
     }
   }
  // delay(SpeedDelay); // Remove this delay
}

Hope this is helpful 


   
ReplyQuote
Share: