PaulS:
How long will it take to run through this for loop? It depends on previousMillis and wait, doesn't it? Some iterations will take no time. Others will take longer. But, overall, how long will each color be shown?
I think you'll be very hard-pressed to find an example of blink without delay that uses a for loop (or that uses it for timing stuff). The blink without delay philosophy is NOT a direct replacement for delay(). It is a mind set that causes one to approach writing code completely differently.
I thought it would only depend on the wait time, so 10ms in my case, but I suppose I'm mistaken. I'm realizing that this may be a bit over my head. And I think you're probably exactly right with that last statement. It's fascinating, I just need more time to learn the basics I think.
Robin2:
PaulS:
It is a mind set that causes one to approach writing code completely differently.
+1
Instead of doing all of the fade in a single FOR loop each step of that process should take place in subsequent iterations of loop() and there should be a variable somewhere (I would use a global variable) to keep track of what stage of the fade things are at.
As a learning exercise you might try modifying one of the LED flash functions in several things at a time to make it fade the LED.
And apologies for posting the link twice - the grey cells are not what they used to be.
...R
Hmmm I think I understand what you are saying. So increment the brightness each time through the loop so that you can return to the top of the program as fast as possible. I think that's pretty much what happens in softBlink in this code:
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);
const int buttonPin = 2; // momentary push button on pin 0
const int numPixelsInStrip = 30;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
long previousMillis; // will store last time pixel was updated
int neoPixelToChange = 0; //track which neoPixel to change
int neoPixel_j = 0; //stores values for program cycles
int nPatterns = 1; //number of patterns to rotate through
int lightPattern = 1; //rotates between patterns with a button press
int defaultBrightness = 64;
//cylon variables
int fadeDirection = -1;//change sigen to fade up or down
boolean cylonDirection = true; //keeps track of the direction the pixels should swipe
boolean cylonPause = false; //keeps track of the pause inbetween swipes
long delayMillis = 0; // will store the last time the cylon swipe was paused
//----------------------------------------------------------------------------------------------
// the setup routine runs once when you press reset:
void setup() {
strip.begin();
strip.show(); // initialize all pixels to 'off'
strip.setBrightness(defaultBrightness); // initialize brightness
pinMode(buttonPin, INPUT); // initialize the BUTTON pin as an input
}
//=============================================================================================
// the loop routine runs over and over again forever;
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
//change the program
lightPattern = (lightPattern + 1) % (nPatterns + 1); //include numOfPrograms + 1, since there is an off state
}
}
}
// save the reading. Next time through the loop
lastButtonState = reading;
//-----------------------------------------------------------------------------------------------------
switch(lightPattern) {
case 1:
softBlink(strip.Color(255,255,150), 35, 10); //off white
break;
}
}
//=============================================================================================
// Fill all the dots with one color
void allColor(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
//----------------------------------------------------------------------------------------------
//fades in, then shuts off
void softBlink(uint32_t c, uint8_t brightness, uint8_t wait) {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > wait) {
//set the color of all pixels
allColor(c);
// save the last time you changed a NeoPixel
previousMillis = currentMillis;
uint16_t i;
int b = (neoPixel_j * brightness) / brightness;
strip.setBrightness(b);
strip.show();
neoPixel_j = (neoPixel_j + 1) % brightness;
}
}
Isn't that right? I guess I just don't understand it well enough to be able to fade it back down. How do I increment the brightness to a certain level without a "for" statement? should I increment a variable in this part of the code?
switch(lightPattern) {
case 1:
softBlink(strip.Color(255,255,150), 35, 10); //off white
break;
Sorry, I know I have a lot of questions. I'll try some more stuff tonight when I have my set up in front of me. I just feel like I'm so close and it's very frustrating. Thanks for all of your help Paul and Robin