Neopixel 16 Ring Arduino Sketch Help

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
andale
 
Posts: 6
Joined: Fri Aug 23, 2019 2:33 pm

Neopixel 16 Ring Arduino Sketch Help

Post by andale »

Hello,
I want to fade in to blue like I currently have but have it pause and not restart the loop.
How do I do this?

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);


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

void loop() {
  brighten();
  //darken();
}



// 0 to 255
void brighten() {
  uint16_t i, j;
    for (j = 0; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, j);
    }
    strip.show();
    delay(10);
  }
}
Last edited by adafruit_support_bill on Fri Jan 21, 2022 8:19 pm, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums.

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel 16 Ring Arduino Sketch Help

Post by adafruit_support_bill »

I want to fade in to blue like I currently have but have it pause and not restart the loop.
Move the contents of your loop() function to your startup() function. Startup only runs once.

User avatar
andale
 
Posts: 6
Joined: Fri Aug 23, 2019 2:33 pm

Re: Neopixel 16 Ring Arduino Sketch Help

Post by andale »

Thanks for the prompt reply.

Would this be able to be called per sensor behavior?
I need to call the fade to blue when triggered and when "released" it will call another lighting mode.

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel 16 Ring Arduino Sketch Help

Post by adafruit_support_bill »

Your post and your code do not refer to any sensors. What sensors do you have and how are they connected? Please define specifically how you want the system to respond to the sensor input.

User avatar
andale
 
Posts: 6
Joined: Fri Aug 23, 2019 2:33 pm

Re: Neopixel 16 Ring Arduino Sketch Help

Post by andale »

Thanks again for your reply.

Alright, below is the current code I have so far, incomplete as I am still figuring it out.

I am using the sonic HC-SR04 sensor.

ULTIMATE GOAL the project will work that 8 LED diodes and the Adafruit Neopixel Ring 16 will be fully lit, static, when the sensor is "undisturbed". When it senses a minimum distance of 36 inches - it calls the rainbow function from the strandtest PLUS (not currently in my code) randomly blink the 8 LED diodes. Then, when the distance is once again "cleared" all lighting components (8 LEDS and NeoPixel Ring) elegantly fade back to static.

Code: Select all


// Adafruit Pixel Ring
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#endif

#define LED_PIN 4
#define LED_COUNT 16

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);


// Ultrasoninc Sensor HC-SR04
const int trigPin = 12;
const int echoPin = 11;
long duration, inches;


// LEDs info here





void setup() {

//Neopixel dataPin
strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show();            // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

//Serial Port
Serial.begin(9600);

}




void loop(){

// Ultrasoninc Sensor HC-SR04 calculate distance  
  pinMode (trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds (2);
  digitalWrite (trigPin,HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin, LOW);
  pinMode (echoPin, INPUT);
  
  noInterrupts();
  duration = pulseIn (echoPin, HIGH);
  interrupts();

   inches = microsecondsToInches(duration);
   
   Serial.print(inches);
   Serial.print("in, ");
   Serial.println();
 
   delay(20); 

   
  if (inches > 36) {
    colorWipe(strip.Color(  0,   0, 255), 50); // Blue;
  } 
  
  else {
    rainbow (1);
  }
 
 
}



//Variable for Inches Conversion
long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}



//Color wheel
void rainbow (int wait) {
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    strip.rainbow(firstPixelHue);
    strip.show(); // Update strip with new contents
    delay(wait);  // Pause for a moment
  }
}


// Fill strip pixels Blue
void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel 16 Ring Arduino Sketch Help

Post by adafruit_support_bill »

the first thing to do is to verify that the code you have so far works. If everything is connected properly, I'd expect it to show the rainbow if the distance is <36" and show blue if it is more. If hat is working, then we can look into how to achieve the fade.

User avatar
andale
 
Posts: 6
Joined: Fri Aug 23, 2019 2:33 pm

Re: Neopixel 16 Ring Arduino Sketch Help

Post by andale »

IMG_3775.jpg
IMG_3775.jpg (505.34 KiB) Viewed 132 times
Yes, I've confirmed that works before requesting help. I've been stuck since achieving this. I tried uploading a .mov to demonstrate in action but wasn't able to "The extension mov is not allowed".

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel 16 Ring Arduino Sketch Help

Post by adafruit_support_bill »

What you need to add now is some state information to store the current state of your ring. this should be declared at global scope so that it persists between iterations of your loop:

Code: Select all

enum RingState { Startup, Rainbow, Fading, Blue };  // all possible states of the ring
RingState state = Startup; // initial state
int fadeLevel = 0; // initial fade level
Then in your code, you can use the stored state information something like this:

Code: Select all

	if (inches > 36) 
	{
		if (state == Rainbow) // start the fade
		{
			state = Fading;
		}
		if (state == Fading) // increase fade level one notch
		{
			colorWipe(strip.Color(  0,   0, ++fadeLevel), 50); // Blue;
			if (fadeLevel == 255)
			{
				state = Blue; // we are done
			}
		}
	}

	else 
	{
		rainbow (1);
		state = Rainbow;
		fadeLevel = 0;
	}

User avatar
andale
 
Posts: 6
Joined: Fri Aug 23, 2019 2:33 pm

Re: Neopixel 16 Ring Arduino Sketch Help

Post by andale »

Thanks much for the recommendation. It works but:

(1) the ring is off until the sensor is interrupted. When the program loads the initial state of the ring should be full 255 blue, and LEDs full on.

(2) the fade is now extremely slow, first going to a full state of OFF for a long time, turning on in sequential ring with not all leds at the same time, and stepping up to full blue in a jagged fashion.

The fade should behave as in the first example I sent where ALL the leds fade up smoothly. I've attempted the strip.fill from Uberguide and I just can't get it to work. I would also not mind a cross fade from one state to the next but have not been able to figure that out either.

Thanks again.

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel 16 Ring Arduino Sketch Help

Post by adafruit_support_bill »

1) Just add code to do that in your startup. Then set state = Blue.

2) Decrease the time parameter you are using in your colorWipe. You can also move the 'show();' command outside of the 'for' loop.

User avatar
andale
 
Posts: 6
Joined: Fri Aug 23, 2019 2:33 pm

Re: Neopixel 16 Ring Arduino Sketch Help

Post by andale »

Thx so much for your suggestions.
I did that, and set the state to Blue. I think that the enum setup distorts what blue is though. Once I did that it just remains on rainbow static and then official rainbow when sensor triggered.

I'm trying to figure out how is it that this code found in the strandtest does change the colors to blue and stop, and how your suggestion does that as well in order to replicate it:

Code: Select all

/ Fill strip pixels Blue
void colorWipe(uint32_t color, int wait=10) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}
I'll play with this new info for a while and post whatever I can figure out. I very much appreciate your help and patience in my learning process.

User avatar
adafruit_support_bill
 
Posts: 88040
Joined: Sat Feb 07, 2009 10:11 am

Re: Neopixel 16 Ring Arduino Sketch Help

Post by adafruit_support_bill »

I think that the enum setup distorts what blue is though. Once I did that it just remains on rainbow static and then official rainbow when sensor triggered.
Not sure what you mean by 'distorts'. As soon as setup completes, the loop will start running. If the distance is < 36, it will display the rainbow. If you want it to stay blue for a while, then add a delay at the end of your setup.

That colorWipe code sets the pixels to whatever color you pass in - one at a time - and with a delay between each pixel. How long it remains blue depends entirely on what your code does after calling that function.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”