Neopixel 16 Ring Arduino Sketch Help

Hello,
I want to create a function that I can call when a sensor is triggered.

It needs to fade to blue like I currently have but it's looping over and over.

How can I pause it until "released", in which case I would call another function.

#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();
}




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);
  }
}

Change to
If( trigger) {
brighten();
trigger = false;
}

Where trigger is a Boolean variable you set when you see your sensor trigger.

1 Like

Thx. I’ve done that but the problem is that brighten() just goes on and on fades in and the fades in again. I need it to fade in once and stop on full blue until “else” happens

No you haven’t, at least not correctly.

Post the code where you did that and it still doesn’t work.

a7

// 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





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
  }
}

That’s quite different to the code you posted.

Did you try @Grumpy_Mike’s suggestion on the problem you were talking about applied to the code you showed us before?

Please describe the actual problem your are actually having with the actual code you are having the problem with.

Your attempt to simplify and learn about your problem by posting something you felt was analogous was a failure.

a7

redundant code

Thanks for both of your antagonistic replies alto.

If anyone can help me figure out how to have that simple fade stop on full blue once it fades up, I'll appreciate it.

I explained well what I wanted but again here it is for those who had the same problem of alto: I need the fade to blue to stop on full blue once it fades, instead of continuing a loop on fade up.

That stop at blue from the fade is the only thing I need help on right now. The additional code is not necessary. I just need to figure out how to get that fade to stop on blue- period.

Thanks killzone_kid: it isn't damaging the code. Do you have a recommendation on how to have the fade stop on full blue and not loop back again?

yes, @Grumpy_Mike already shown you how to do this. you need boolean variable that will stop colorWipe from executing every loop iteration

No, it doesn't work. What Grumpy_Mike is suggesting is an alternative of an if/else statement.

This boolean suggestion is still calling brighten() which is the function that I need to figure out how to STOP its continuous fade in looping once it reaches full blue.

If anyone has a constructive/positive suggestion to this matter please reply.

you need to use this boolean in combination with distance checking not instead

May be use the goto function?

#include <Adafruit_NeoPixel.h>

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

void setup() 
{
  Serial.begin(19200);
  Serial.println("ok");
  
  strip.begin();
  strip.show(); // initialize all pixels to "off"
  flag = false;
}

void loop() 
{
  if (flag == false)
  {
    flag = true;
    brighten();
  }
}

void brighten() 
{
  // flag = true;   ** post edit **
  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);
    }
  }
  Serial.println ("Done");
}

So what you do when a suggestion doesn't work is that you post ALL your latest code and say what it does and what you wanted to do. Who knows you might have misunderstood what I said. If so we can show you where where you went wrong.

Thanks much runaway_pancake!
Your code certainly clarifies the boolean approach and does keep the blue static - however, the strip doesn't fade in unison, it illuminates pixel by pixel across the ring. The full code, which I've already shared, has this same function but differently, taken from the Examples of the strandtest. This is it:

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);
    }
  }
  Serial.println ("Done");
}

Again, thanks much.

Because that is how it is written. After setting the value of each pixel you show it and then delay. If you want to fade all the LEDs at once only do the show call when the for loop has finished.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.