Go Down

Topic: How to switch between “presets/scenes”?  (Read 357 times) previous topic - next topic

venicenerd

So I'm a complete beginner with programmable RGBW and I'm trying to wrap my head around it.

I basically want to instal two strips in my ceiling. I don't want them to do anything fancy. Just a nice fade from one color to another. Maybe some slight "brightness pulsing".

But I do want to be able to quickly switch between different "presets". For example I would like to create three presets:

Preset 1: blue to white with slight pulsing
Preset 2: saturated orange to light orange at very dim level
Preset 3: pure white all around

Now I would like an easy way to be able to switch between these simple presets. Kind of like I am able to set scenes with HomeKit.

All the tutorials I have watched so far always show how to do some crazy rainbow thing and how to change the code and send that change to the board. But I really don't want to log into my Arduino every time and change code to change the mood of my room.

Is there a way to create multiple presets and then switch between them? Like an iPhone app? Even better is there a way to map these presets to HomeKit?

Anyways, I'm completely lost as I do not currently understand how to take this from "oh wow what a cool experiment" to "yeah I can use that on a daily basis to change the mood of my room."

Would love to hear your insights!

PaulRB

Use a WiFi enabled board like Wemos Mini to host a little web page that allows you to switch between your presets or pick colour combinations etc. This is what I did with my Christmas tree lights.

venicenerd

Do you have a guide to a tutorial that explains how to create presets and how to host that website?

I want to wrap my head around this and anything I can find are the tutorials that show you how to "live program" rainbow colors and stuff. I haven't seen anything yet that explains how to set up a couple of preset color scenes and then easily switch between them.

I would like to use this as room lighting so I need an easy way to switch between different presets or no one will ever use the lights.

PaulRB

I don't have any tutorials, sorry. I don't have time to make those and build projects as well. Maybe when I retire.

I can share my Christmas tree lights code with you if you like.

surveyranger

Do you have a guide to a tutorial that explains how to create presets and how to host that website?

I want to wrap my head around this and anything I can find are the tutorials that show you how to "live program" rainbow colors and stuff. I haven't seen anything yet that explains how to set up a couple of preset color scenes and then easily switch between them.

I would like to use this as room lighting so I need an easy way to switch between different presets or no one will ever use the lights.
Have you downloaded the Adafruit NeoPixel library and checked out the "buttoncycler" sketch?  It uses a button to cycle through different lighting modes.  The example sketch uses delays which means you'd have to wait for the mode (or preset) to complete before moving on to another.

The presets are created as separate functions (ie. void preset1) and cycled through using switch/case.

Which Arduino are you using?

Grumpy_Mike

#5
Jun 16, 2019, 06:04 am Last Edit: Jun 16, 2019, 06:05 am by Grumpy_Mike
Quote
The example sketch uses delays which means you'd have to wait for the mode (or preset) to complete before moving on to another.
So in order to avoid this you have to rewrite those demos as a state machine.

Here is one that cycles through different presets at the push of a button.

Code: [Select]

// StrandTest from AdaFruit implemented as a state machine
// pattern change by push button
// By Mike Cook Jan 2016
// Fade function added Sept 2017

#define PINforControl   4 // pin connected to the small NeoPixels strip
#define NUMPIXELS1      64 // number of LEDs on strip

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS1, PINforControl, NEO_GRB + NEO_KHZ800);

unsigned long patternInterval = 20 ; // time between steps in the pattern
unsigned long lastUpdate = 0 ; // for millis() when last update occurred
unsigned long intervals [] = { 20, 20, 50, 100, 30 } ; // speed for each pattern - add here when adding more cases
int fadeStep = 0; // state variable for fade function
int numberOfCases = 4; // how many case statements or patterns you have
const byte button = 3; // pin to connect button switch to between pin and ground

void setup() {
  strip.begin(); // This initializes the NeoPixel library.
  wipe(); // wipes the LED buffers
  pinMode(button, INPUT_PULLUP); // change pattern button
}

void loop() {
  static int pattern = 4, lastReading; // start with the fade function
  int reading = digitalRead(button);
  if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    fadeStep = 0; // reset the fade state variable
    if(pattern > numberOfCases) pattern = 0; // wrap round if too big
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer
    delay(50); // debounce delay
  }
  lastReading = reading; // save for next time

if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
}

void  updatePattern(int pat){ // call the pattern currently being created
  switch(pat) {
    case 0:
        rainbow();
        break;
    case 1:
        rainbowCycle();
        break;
    case 2:
        theaterChaseRainbow();
        break;
    case 3:
         colorWipe(strip.Color(255, 0, 0)); // red
         break;
        
    case 4:
         fade(0,255, 0,64, 0,0, 400); // fade from black to oraange and back
         break;                  
  }
}

void fade(int redStartValue, int redEndValue, int greenStartValue, int greenEndValue, int blueStartValue, int blueEndValue, int totalSteps) {
static float redIncrement, greenIncrement, blueIncrement;
static float red, green, blue;
static boolean fadeUp = false;

if (fadeStep == 0){ // first step is to initialise the initial colour and increments
  red = redStartValue;
  green = greenStartValue;
  blue = blueStartValue;
  fadeUp = false;

  redIncrement = (float)(redEndValue - redStartValue) / (float)totalSteps;
  greenIncrement = (float)(greenEndValue - greenStartValue) / (float)totalSteps;
  blueIncrement = (float)(blueEndValue - blueStartValue) / (float)totalSteps;
  fadeStep = 1; // next time the function is called start the fade
}
else { // all other steps make a new colour and display it
  // make new colour
  red += redIncrement;
  green +=  greenIncrement;
  blue += blueIncrement;
 
  // set up the pixel buffer
  for (int i = 0; i < strip.numPixels(); i++) {
  strip.setPixelColor(i, strip.Color((int)red,(int)green,(int)blue));
  }
 // now display it
  strip.show();
fadeStep += 1; // go on to next step
if(fadeStep >= totalSteps) { // finished fade
  if(fadeUp){ // finished fade up and back
     fadeStep = 0;
     return; // so next call recalabrates the increments
  }
  // now fade back
  fadeUp = true;
  redIncrement = -redIncrement;
  greenIncrement = -greenIncrement;
  blueIncrement = -blueIncrement;
  fadeStep = 1; // don't calculate the increments again but start at first change
}
 }
}

void rainbow() { // modified from Adafruit example to make it a state machine
  static uint16_t j=0;
    for(int i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
     j++;
  if(j >= 256) j=0;
  lastUpdate = millis(); // time for next change to the display
 
}
void rainbowCycle() { // modified from Adafruit example to make it a state machine
  static uint16_t j=0;
    for(int i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
  j++;
  if(j >= 256*5) j=0;
  lastUpdate = millis(); // time for next change to the display
}

void theaterChaseRainbow() { // modified from Adafruit example to make it a state machine
  static int j=0, q = 0;
  static boolean on = true;
     if(on){
            for (int i=0; i < strip.numPixels(); i=i+3) {
                strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
             }
     }
      else {
           for (int i=0; i < strip.numPixels(); i=i+3) {
               strip.setPixelColor(i+q, 0);        //turn every third pixel off
                 }
      }
     on = !on; // toggel pixelse on or off for next time
      strip.show(); // display
      q++; // update the q variable
      if(q >=3 ){ // if it overflows reset it and update the J variable
        q=0;
        j++;
        if(j >= 256) j = 0;
      }
  lastUpdate = millis(); // time for next change to the display  
}

void colorWipe(uint32_t c) { // modified from Adafruit example to make it a state machine
  static int i =0;
    strip.setPixelColor(i, c);
    strip.show();
  i++;
  if(i >= strip.numPixels()){
    i = 0;
    wipe(); // blank out strip
  }
  lastUpdate = millis(); // time for next change to the display
}


void wipe(){ // clear all LEDs
     for(int i=0;i<strip.numPixels();i++){
       strip.setPixelColor(i, strip.Color(0,0,0));
       }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Go Up