Neopixel I need help

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
homejk
 
Posts: 1
Joined: Thu Aug 12, 2021 7:04 pm

Neopixel I need help

Post by homejk »

Hi sorry for my English I am not a native speaker
I have a problem
I don't understand how to do the simplest thing with it.
I want to know is how to code the NeoPixel after a button has been pushed or sensor
I want to avoid using the delay function
it should work after pressing the button for 5 seconds but run no more than once every 60 seconds

Code: Select all

#include <Adafruit_NeoPixel.h>

int sensor = 2; // PiR sensor

// Pattern types supported:
enum  pattern { SCANNER, };
// Patern directions supported:
enum  direction { FORWARD, REVERSE };

// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
    public:

    // Member Variables:  
    pattern  ActivePattern;  // which pattern is running
    direction Direction;     // direction to run the pattern
    
    unsigned long Interval;   // milliseconds between updates
    unsigned long lastUpdate; // last update of position
    
    uint32_t Color1;  // What colors are in use
    uint16_t TotalSteps;  // total number of steps in the pattern
    uint16_t Index;  // current step within the pattern
    
    void (*OnComplete)();  // Callback on completion of pattern
    
    // Constructor - calls base-class constructor to initialize strip
    NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
    :Adafruit_NeoPixel(pixels, pin, type)
    {
        OnComplete = callback;
    }
    
    // Update the pattern
    void Update()
    {
        if((millis() - lastUpdate) > Interval) // time to update
        {
            lastUpdate = millis();
            switch(ActivePattern)
            {                
                case SCANNER:
                    ScannerUpdate();
                    break;
                    
            }
        }
    }
  
    // Increment the Index and reset at the end
    void Increment()
    {
        if (Direction == FORWARD)
        {
           Index++;
           if (Index >= TotalSteps)
            {
                Index = 0;
                if (OnComplete != NULL)
                {
                    OnComplete(); // call the comlpetion callback
                }
            }
        }
        else // Direction == REVERSE
        {
            --Index;
            if (Index <= 0)
            {
                Index = TotalSteps-1;
                if (OnComplete != NULL)
                {
                    OnComplete(); // call the comlpetion callback
                }
            }
        }
    }
    
    
    // Initialize for a SCANNNER
    void Scanner(uint32_t color1, uint8_t interval)
    {
        ActivePattern = SCANNER;
        Interval = interval;
        TotalSteps = (numPixels() - 1) * 2;
        Color1 = color1;
        Index = 0;
    }

    // Update the Scanner Pattern
    void ScannerUpdate()
    { 
        for (int i = 0; i < numPixels(); i++)
        {
            if (i == Index)  // Scan Pixel to the right
            {
                 setPixelColor(i, Color1);
            }
            else if (i == TotalSteps - Index) // Scan Pixel to the left
            {
                 setPixelColor(i, Color1);
            }
            else // Fading tail
            {
                 setPixelColor(i, DimColor(getPixelColor(i)));
            }
        }
        show();
        Increment();
    }
    
  
   
    // Calculate 50% dimmed version of a color (used by ScannerUpdate)
    uint32_t DimColor(uint32_t color)
    {
        // Shift R, G and B components one bit to the right
        uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
        return dimColor;
    }

     // Returns the Red component of a 32-bit color
    uint8_t Red(uint32_t color)
    {
        return (color >> 16) & 0xFF;
    }

    // Returns the Green component of a 32-bit color
    uint8_t Green(uint32_t color)
    {
        return (color >> 8) & 0xFF;
    }

    // Returns the Blue component of a 32-bit color
    uint8_t Blue(uint32_t color)
    {
        return color & 0xFF;
    }
};


void StickComplete();

// Define some NeoPatterns for the two rings and the stick
//  as well as some completion routines

NeoPatterns Stick(8, 4, NEO_GRB + NEO_KHZ800, &StickComplete);

// Initialize everything and prepare to start
void setup()
{
  Serial.begin(115200);

  pinMode(sensor, INPUT); // PiR sensor

    // Initialize all the pixelStrips
       Stick.begin();

    
   
   
}

// Main loop
void loop()
{
  int sensorval = digitalRead(sensor); // PiR sensor
    // Update the rings.
    if (sensorval == HIGH)
    
    {  
               
    Stick.Update();     
}

else 
{
  }
  
}

// Stick Completion Callback
void StickComplete()
{
    // Random color change for next scan
    Stick.Scanner(Stick.Color(255,16,0), 55);
}

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Neopixel I need help

Post by adafruit_support_mike »

Take a look at the BlinkWithoutDelay sketch:

https://www.arduino.cc/en/pmwiki.php?n= ... thoutDelay

Instead of calling delay(), read the value of millis() when you start, and add the amount tof time you want to wait to that value. The result will be the value of millis() when you want to stop.

Then keep checking millis() until it reaches the value you calculated.

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

Return to “Arduino”