First of all thanks for taking the time to read this and maybe (hopefully) able to help me!
I hope I post this into the right spot if not please let me know.
I'm a creative guy working on a personal costume project. I have no big ambitions in becoming a programmer but did start with several tutorials to get a bit of insight. With the simple things I'm starting to get the hang of it but don't seem to be able to crack this (which is probably peanuts for you peeps..).
The purpose is to have three scenes of the LED's:
Base state is a dimm blue backlit state of the LED's with a random appearing "glow" in near white + fade out
When the switch is pressed a random white sparkle (not too fast) appears on all LED's
When the switch is pressed again 1 random LED will glow up/down until switch is pressed again
Switch pressed again will return to base state
Sounds easy no? Well, I found 2 templates that I like for this and understand enough about to customise speed etc. I don't know how to random pick 1 LED from the pack, keep it glowing before fade out and return to base state after button press.
Being a costume I've decided to go with the latest Adafruit Flora V3 and the Flora RGB Neopixels V2.
I want to use between 80 and 100 pixels on a dress.
My question; can anyone help me get this working the way I've envisioned it?
Please find the code for the two patterns I already like below. No button code has been put in yet.
In case of any questions please let me know!
Cheers, Andras
// For ADAFRUIT FLORA Board
// Flora RGB Neopixels are connected in line on PIN 6 with + on V-BATT and - on GND - strand of 100
// Switch is moment-switch connected to PIN TX1 with + and - on GND
// External power supply for power
// Purpose is to have three scenes of the LED's
// Base state is a dimm blue backlit state of the LED's with a random appearing "glow" in near white + fade out
// When the switch is pressed a random white sparkle (not too fast) appears on all LED's
// When the switch is pressed again 1 random LED will glow up/down until switch is pressed again
// Switch pressed again will return to base state
// Define the pixel-count and PIN setting:
#define NUMBEROFPIXELS 10 // Number of Flora Neopixels V2 (using 10 for testing)
#define PIXELPIN 6 // onbaord PIN with pixels
// Setup the libraries:
#include "Arduino.h"
#include <Adafruit_NeoPixel.h>
#include <NeoPixelPainter.h>
// I do not know exactly whats happening below here:
Adafruit_NeoPixel neopixels = Adafruit_NeoPixel(NUMBEROFPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800);
NeoPixelPainterCanvas pixelcanvas = NeoPixelPainterCanvas(&neopixels); //create canvas, linked to the neopixels (must be created before the brush)
NeoPixelPainterBrush pixelbrush = NeoPixelPainterBrush(&pixelcanvas); //crete brush, linked to the canvas to paint to
void setup() {
 //randomSeed(analogRead(0)); // new random seed (for what?)
 neopixels.begin(); // start the pixels
Â
}
void loop() {
// slow random twinkle in "white" on a dimmed blue base color
Â
  HSV brushcolor;
   pixelbrush.setSpeed(100); // set the speed (10 LED's "200" is fine) "0" = off
   pixelbrush.setFadein(false); // do not fade in ("appear")
   pixelbrush.setFadeout(true); // do fade out ("ftb")
 Â
  if (rand() % 100 == 0) // at a random interval, move the brush to paint a new pixel (brush only paints a new pixel once)
  {
   brushcolor.h = rand();
   brushcolor.s = random(20); //set low saturation, almost white
   brushcolor.v = random(200) + 20; //set random brightness
  Â
   pixelbrush.setColor(brushcolor);
   pixelbrush.moveTo(random(NUMBEROFPIXELS)); //move the brush to a new, random pixel
   pixelbrush.setFadeSpeed(random(50) + 25); //set random fade speed, minimum of 5
  }
  //add a background color by setting all pixels to a color (instead of clearing all pixels):
 Â
  int i;
  for (i = 0; i < NUMBEROFPIXELS; i++)
  {
   neopixels.setPixelColor(i, 0, 0, 10); //color in RGB: dark blue
  }
  pixelbrush.paint(); //paint the brush to the canvas
  pixelcanvas.transfer(); //transfer (add) the canvas to the neopixels
  neopixels.show();
// random fast sparkle over all LED's (loosely based sparkler template)
{
 HSV brushcolor;
{
   pixelbrush.setSpeed(1);
   pixelbrush.setFadeout(true); //sparkles fade out
   pixelbrush.setFadein(false); //and fade in immediately after reaching the set brightness
}
 if (random(5) % 100 == 0) // at a random interval, move the brush to paint a new pixel (brush only paints a new pixel once)
  //set a new brush color in each loop
 Â
  brushcolor.h = (0); //set color
  brushcolor.s = (0); //random but low saturation, giving white-ish sparkles
  brushcolor.v = random(200) + 20; //random (peak) brighness
  pixelbrush.setColor(brushcolor);
  pixelbrush.moveTo(random(NUMBEROFPIXELS));
  pixelbrush.setFadeSpeed(random(250) + 20); //set a new fadespeed with some randomness
 neopixels.clear();
 pixelbrush.paint(); //paint the brush to the canvas (and update the brush, i.e. move it a little)
 pixelcanvas.transfer(); //transfer (add) the canvas to the neopixels
  neopixels.show();
Â
  }
 }
Please use [ code ] tags next time. The forum software will eat some of your code if you don't. It's all explained in the "how to use this forum" post at the top of every forum.
You've used the word "state" several times in your post. In the computer world "state machine" is the phrase used to describe this kind of behaviour. There's many bad tutorials online. The best one I know of is State Machine
I'm not fully understanding your vision. The basic background animation always runs but sometimes you want to flash one LED? Then you need to pick which LED when entering that state and have the background NOT mess with that LED while your extra code is running through that state.
This is a fairly complex state machine, with subsidiary state machines running the individual animations. Don't despair when it looks like your code is getting huge. Make sure to write down the states on a piece of paper. Try and encapsulate the different features into different functions. For example:
void loop() {
 now = millis();
 doBackgroundAnimation();
 switch(state) {
  case STATE_IDLE:
   if(buttonWasPushed()) {
    state = STATE_TWINKLE;
    startStateTwinkle = now;
    twinkleLED = random(NUMBEROFPIXELS);
   } else {
    doThePushThisButtonAnimation();
   }
  break;
  case STATE_TWINKLE:
   if(millis() - startStateTwinkle > MaxTwinkleTime) {
    state = STATE_IDLE
    startStateIdle = now;
   } else {
    doTheTwinkleAnimation();
   }
  break;
 }
}
Of course, each of those do...Animation() functions will only advance the animation by one step, they don't run the entire sequence before exiting.
If you want a special animation to run to completion instead of just twinkling for a pre-set number of seconds, then the do...Animation() function should return true when it finishes and then your main state machine can detect that the animation has finished and change state to the next animation.
if(doTheSpecialAnimation()) {
 //animation has finished if it returns true
 state = STATE_IDLE;
}
Sorry for the "code" thing will use this next time!
I guess my word usage of "state" confuses here.
In the explanation read the word "state" as scenes.
So:
Scene 1) General scene with blue based random glowing LEDs (as in the code)
(button = pushed)
Scene 2) Random white sparkle over all the LEDs
(button = pushed)
Scene 3) One random LED will start pulsing / glowing
(button = pushed) -> back to scene 1
"State" or "Scene" are almost interchangeable. Where they are different is you may have more states that explicitly define actions like "smoothly fade Scene 1 down to black"; maybe there's an "OFF" state too.
You can still go back and edit your original post to wrap [code] and [/code] around your code.
I've not foreseen any difficult transitions between the scenes just switch over and go.
Just startup play scene 1, button -> play 2 etc.
Also tried to squeeze your code into the program but got totally lost....
Missing references and me not knowing how to fix them..
True noob here and I'm feeling rather silly
More or less what your code says makes sense to me as I can follow what you mean.
But how I get the different elements linked correctly is beyond me so far.
Ask the mods to move this post to "Gigs and Collaborations".
I have done similar projects. Actually - I am doing a similar project: lighting up some 3d-printed game pieces with four neopixels and a selectable animation. PixelBrush is new to me though. Perhaps I have been doing it the hard way all this time
In any event, the pattern is something like:
struct Scene {
 virtual void begin() = 0;
 virtual void paint() = 0;
 virtual void end() = 0;
};
struct Scene1 : public Scene {
 void begin() {/* code goes here to set up the scene - the iniial paint */}
 void paint() {/* code goes here to draw the scene with reference to the current time*/}
 void end() {/* code goes here to clean up */}
} scene1;
struct Scene2 : public Scene {
 void begin() {/* code goes here to set up the scene - the iniial paint */}
 void paint() {/* code goes here to draw the scene with reference to the current time*/}
 void end() {/* code goes here to clean up */}
} scene2;
struct Scene3 : public Scene {
 void begin() {/* code goes here to set up the scene - the iniial paint */}
 void paint() {/* code goes here to draw the scene with reference to the current time*/}
 void end() {/* code goes here to clean up */}
} scene3;
// a list of all our scenes
Scene *scene[] = { &scene1, &scene2, &scene3 };
const int SCENES = sizeof(scene)/sizeof(*scene); // number of scenes
int currentScene; // first scene is number zero
const byte buttonPin = 3; // assuming the button is on pin 3
byte buttonState = HIGH;
void setup() {
 // setup of pixels, button pins, etc goes here
 currentScene = 0; // start at position 0
 scene[currentScene]->begin();
}
void loop() {
 byte prevButtonState = buttonState;
 buttonState = digitalRead(buttonPin);
 // has the button been pressed?
 if(buttonState == LOW && prevButtonState == HIGH) {
  scene[currentScene]->end();
  currentScene = (currentScene + 1) % SCENES;
  scene[currentScene]->begin();
 }
 scene[currentScene]->paint();
 pixelcanvas.transfer();
 neopixels.show();
Â
}
@ Paul, super thanks for the code. Will give it a go this afternoon (Amsterdam time).
Could also be a good thing to move this to that section too as I do seek coop!
Will for sure let you know how I venture with your code.
@ Jim Lee, the goal is 100 neopixels in a costume. For testing I set the count on 10.
If you're willing to do most of the coding and testing yourself, then it's best to keep this out of the collaborations. If you want to just pay someone, then report your own post to a moderator and ask to get it moved.
So it looks like your code is doing the animations you want but you just need to add the button code to select scenes? Start a new sketch that just turns on one Neopixel red, green or blue according to the scene. Getting the button code working will take a little bit of effort, so you don't want the large scene code in there while you're working on the button.
The button states might be:
No button pressed
Button is currently pressed but it seems like a short-term glitch less than 100 milliseconds
Button is currently pressed and it has been continuously pressed for more than 100 milliseconds
Button is currently pressed and we have already moved on to the next scene; we must wait for the button to be released before we can recognise another button push
This is basic debouncing. Maybe you can start from a debouncing tutorial sketch.