0

Water Flow Simulation with NeoPixels & Arduino
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Water Flow Simulation with NeoPixels & Arduino

by jordanat2012 on Sun May 20, 2018 9:08 am

Hi there,
I'm pretty new to coding, all self-taught thus far, and am struggling with modifying some NeoPixel code to get it to do what I want. I'm trying to use a strand of NeoPixels to simulate flowing water. So far, I've modified the Adarfruit rainbowCycle routine to have a swatch of white pixels track along the strand of blue pixels. What I'm really trying to do is to get darker blue shades to fade into white and then back to blue along the length of the strip. I want it to be a fluid transition, but right now the white is a hard change over from the blue. Ideally, the fade will move along the length of the strip. The code I have right now is included.

Anyone got some advice? Thanks in advance!

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define PIN 7

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

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

void loop() {
  rainbowCycle(5);
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 5) {
    return strip.Color(0, 0, 255 - WheelPos * 3);
  }
  if(WheelPos < 50) {
    WheelPos -= 5;
    return strip.Color(255, 255, 255);//255 - WheelPos * 3);
  }
//  WheelPos -= 15;
//  return strip.Color(0, 0, 255 - WheelPos * 3);
}

jordanat2012
 
Posts: 12
Joined: Sun May 20, 2018 8:51 am

Re: Water Flow Simulation with NeoPixels & Arduino

by jordanat2012 on Mon Aug 27, 2018 6:58 am

Wanted to ping everyone on this question again. Anyone able to offer any advice?

jordanat2012
 
Posts: 12
Joined: Sun May 20, 2018 8:51 am

Re: Water Flow Simulation with NeoPixels & Arduino

by adafruit_support_bill on Mon Aug 27, 2018 7:28 am

Try something like:

Code: Select all | TOGGLE FULL SIZE
// Input a value 0 to 255 to get a color value.
// The colours are a transition blue-white-blue.
uint32_t Wheel(byte WheelPos)
{
  if(WheelPos < 128)
  {
    return strip.Color(WheelPos * 2, WheelPos * 2, 255);
  }
  else
  {
    return strip.Color(255 - (WheelPos * 2), 255 - (WheelPos * 2), 255);//255 - WheelPos * 3);
  }
}

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Water Flow Simulation with NeoPixels & Arduino

by jordanat2012 on Mon Aug 27, 2018 10:23 am

That looks like it will probably work - thanks so much!

On a semi related note, is there a forum or lesson that explains how the color wheel code works? I've spent some time in the past trying to figure it out, but I'm all self-taught at coding, and it was seeming to get over my head.

jordanat2012
 
Posts: 12
Joined: Sun May 20, 2018 8:51 am

Re: Water Flow Simulation with NeoPixels & Arduino

by adafruit_support_bill on Mon Aug 27, 2018 10:37 am

I don't know of any tutorials that go into details about how it works. The original color wheel basically divides the wheel into 3 sections, where each section does a linear interpolation between two of the primary colors. Red->Green, Green->Blue, Blue->Red.

The version I posted above is just two sections: Blue->White and White->Blue.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Water Flow Simulation with NeoPixels & Arduino

by jordanat2012 on Mon Sep 09, 2019 6:26 am

Hi Bill,
I've been working with this code that simulates water flowing. Just now getting around to implementing in my final project. Need to reverse the direction of the "flow." Right now it looks like it flows TOWARDS the circuit; I need it to flow AWAY from the circuit.

I did some searching in an attempt to learn how to make that change, but never had any luck. Is there an easy way that I've overlooked to change direction? Here's the code I'm using right now.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

const int waterflowPin = 9;   // data output pin for NeoPixel strip
#define waterflowNumPixels (59)
Adafruit_NeoPixel waterflowStrip = Adafruit_NeoPixel(waterflowNumPixels, waterflowPin, NEO_GRB + NEO_KHZ800);

void setup() {
  waterflowStrip.begin();
  waterflowStrip.show();
}

void loop() {
  waterflow(10);
}

void waterflow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<waterflowStrip.numPixels(); i++) {
      waterflowStrip.setPixelColor(i, Wheel((i+j) & 255));
    }
    waterflowStrip.show();
    delay(wait);
  }
}

uint32_t Wheel(byte WheelPos)
{
  if(WheelPos < 128)
  {
    return waterflowStrip.Color(WheelPos * 2, WheelPos * 2, 255);
  }
  else
  {
    return waterflowStrip.Color(255 - (WheelPos * 2), 255 - (WheelPos * 2), 255);
  }
}

jordanat2012
 
Posts: 12
Joined: Sun May 20, 2018 8:51 am

Re: Water Flow Simulation with NeoPixels & Arduino

by adafruit_support_bill on Mon Sep 09, 2019 7:50 am

Try reversing the inner 'for' loop:
Code: Select all | TOGGLE FULL SIZE
   for(i = waterflowStrip.numPixels(); i >=0; --i)

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Water Flow Simulation with NeoPixels & Arduino

by jordanat2012 on Tue Sep 10, 2019 6:29 am

Thanks Bill. I tired that, but it seems to leave the strip completely inactive; the strip is just turned off altogether.

jordanat2012
 
Posts: 12
Joined: Sun May 20, 2018 8:51 am

Re: Water Flow Simulation with NeoPixels & Arduino

by adafruit_support_bill on Tue Sep 10, 2019 8:15 am

Please post your modified code.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: Water Flow Simulation with NeoPixels & Arduino

by kcl1s on Tue Sep 10, 2019 8:30 am

jordanat2012 wrote:Thanks Bill. I tired that, but it seems to leave the strip completely inactive; the strip is just turned off altogether.

That problem stems from using unsigned int's (uint16_t) in your for loop counters. Because they are unsigned they roll over and never get less than zero. That for loop never ends. Declare them as int's and it lights up the pixels but it does not seem to change the perceived flow direction. Changing the outside for loop gets the the results you want.
Code: Select all | TOGGLE FULL SIZE
for(j=255; j >= 0; j--) {


Fellow hobbyist
Keith

kcl1s
 
Posts: 1251
Joined: Tue Aug 30, 2016 12:06 pm

Re: Water Flow Simulation with NeoPixels & Arduino

by jordanat2012 on Wed Sep 11, 2019 7:19 am

Thanks so much Keith! I believe that seems to have done the trick. The "unit" to "int" transition was exactly what was needed. I'll upload the code to my final install and confirm, but the test strip on my workbench looks like it's showing what I want.

One other thing I was hoping you guys might be able to help me out with - the strip is showing white and then the blue stripe runs through it. Is there a simple way to take the white to a pale blue, so the transition between the white and the blue stripe isn't as severe? Does that make sense what I'm asking?

Thanks!

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

const int waterflowPin = 9;   // data output pin for NeoPixel strip
#define waterflowNumPixels (59)
Adafruit_NeoPixel waterflowStrip = Adafruit_NeoPixel(waterflowNumPixels, waterflowPin, NEO_GRB + NEO_KHZ800);

void setup() {
  waterflowStrip.begin();
  waterflowStrip.show();
}

void loop() {
  waterflow(10);
}

void waterflow(int8_t wait) {
  int16_t i, j;

  for(j=255; j>=0; j--) {
     for(i=0; i<waterflowStrip.numPixels(); i++) {
//     for(i = waterflowStrip.numPixels(); i >=0; --i) {
      waterflowStrip.setPixelColor(i, Wheel((i+j) & 255));
    }
    waterflowStrip.show();
    delay(wait);
  }
}

int32_t Wheel(byte WheelPos)
{
  if(WheelPos < 128)
  {
    return waterflowStrip.Color(WheelPos * 2, WheelPos * 2, 255);
  }
  else
  {
    return waterflowStrip.Color(255 - (WheelPos * 2), 255 - (WheelPos * 2), 255);
  }
}

jordanat2012
 
Posts: 12
Joined: Sun May 20, 2018 8:51 am

Please be positive and constructive with your questions and comments.