Trying to get certain neopixels to activate on a timer

Im trying to get certain neopixels to activate with a timer.

Like every 1.5 minutes 1 pixel turns on.

I have been trying a few days but my code never compiles :frowning:

I only got the leds working, and the d5 is for a water pump

Here’s some code i have right now, althought it doesnt have the if else statements yet

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif


const byte ledPin=D3;

#define NUMPIXELS      11

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);

void setup() {
 pinMode(D5, OUTPUT);
 pinMode(D3, OUTPUT);
}
 
void loop() {
 digitalWrite(D5, LOW); // Relais UIT
 delay(1000); // wacht een seconde
 digitalWrite(D5, HIGH); // Relais AAN
 delay(1000); // wacht een seconde
 
    SetPixels();
}

void SetPixels() 
{

  for (int i = 0; i < NUMPIXELS; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(0, 255, 0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
}

did you get any code to light up your neopixels at the moment? how are things wired and powered?

Alright so i got further which i great, i used the for loop to trigger the amount off leds. Now im trying to use the Simpletimer.H library to trigger a function after 9 seconds, but my code doesn’t compile because timerid was not declared.

#include <SimpleTimer.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

const byte ledPin=D3;

#define NUMPIXELS      11

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);



// the timer object
SimpleTimer timer;

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 9000);
    
}

void setup() {
    Serial.begin(9600);
    timer.setInterval(1000, repeatMe);
}

void loop() {
    timer.run();
}

void callMeLater() {
  
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
}

timerId = timer.setTimeout(1000, callMeLater);

which part in the code does this line belong to?

timerId = timer.setTimeout(1000, callMeLater);

seems pretty isolated... and indeed what is timerId ??

You don't need to use timers to wait 9s... see how to use millis() in the post at the top of the forum

Thanks, that looks better for the job, i'll get to work and report back.

Alright, so i kind of get it but now im trying to figure out where to put the amount om millis(9000) on which the function activates. I’ve tried some code but it doesn’t compile.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif


#define NUMPIXELS      11

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);


unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin=D3; //neopixel led strip
void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }

  else (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    for (int i = 0; i < 2; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

you forgot the if there (after the else)

  else (currentMillis - startMillis >= period)  //test whether the period has elapsed

(but I'm not sure why you need the test twice)

I’m using a ledstrip with 11 leds and every 9 seconds i want 1 led to turn on. and on 54 seconds the color also changes, but that’s done in the setpixelcolor.

so that would be

{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= 9000)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }

And the second one should look like this?

{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  else if (currentMillis - startMillis >= 18000)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }

And it wont compile because ledpin was not declared

pannekoek141:
And it wont compile because ledpin was not declared

in your first post you hadconst byte ledPin=D3;is this where your strip is connected?

You said

every 9 seconds i want 1 led to turn on.

so this is the correct testif (currentMillis - startMillis >= 9000)but you need to remember (or calculate) which LED to turn on a that point

im working on a project to make my roommates shower less and i built a small prototype, here’s a pic for
the interested:

But i’m having some issues with the code. im trying to get the timer to work to activate 1 led every 9 seconds with if and else if statements, but my code won’t compile and i’m not sure if i put the millis(9000) in the right place. Here’s my code

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif


#define NUMPIXELS      11
#define ledPin

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);


unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin=D3; //neopixel led strip



void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
  pinMode(D5, OUTPUT);
}

void loop()
{

  
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= 9000)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }


{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  else if (currentMillis - startMillis >= 18000)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }

I've got that error out of the way by adding #define ledPin

But now im getting a bunch of other compiling errors, and D3 is where the ledpin is connected. I've got this working if you're interested:

what’s this contraption for?

so does it compile or not? which arduino are you using?

Haha it’s a small prototype, it’s just a plastic tub with a pump connected to it. The pump reused the water. And the ledstrip works as a counter. I’m using the node mcu. These are the errors i’m getting right now

Arduino: 1.8.6 (Mac OS X), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 921600"

Stopwatch:10:45: error: expected primary-expression before '(' token
 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
                                             ^
Stopwatch:10:63: error: expected primary-expression before ',' token
 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
                                                               ^
Stopwatch:16:18: error: expected unqualified-id before '=' token
 const byte ledPin=D3; //neopixel led strip
                  ^
/Users/Owner/Documents/Arduino/Stopwatch/Stopwatch.ino: In function 'void setup()':
Stopwatch:22:17: error: expected primary-expression before ',' token
   pinMode(ledPin, OUTPUT);
                 ^
/Users/Owner/Documents/Arduino/Stopwatch/Stopwatch.ino: In function 'void loop()':
Stopwatch:48:3: error: 'else' without a previous 'if'
   else if (currentMillis - startMillis >= 18000)  //test whether the period has elapsed
   ^
Stopwatch:60:3: error: expected '}' at end of input
   }
   ^
Stopwatch:60:3: error: expected '}' at end of input
exit status 1
expected primary-expression before '(' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Here’s a piece code that i used to get the leds and pump working, and it just uploads fine…

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif


const byte ledPin=D3;

#define NUMPIXELS      11

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);

void setup() {
 pinMode(D5, OUTPUT);
 pinMode(D3, OUTPUT);
}
 
void loop() {
/* digitalWrite(D5, LOW); // Relais UIT
 delay(1000); // wacht een seconde
 digitalWrite(D5, HIGH); // Relais AAN
 delay(1000); // wacht een seconde
*/
    SetPixels();
}

void SetPixels() 



{

  for (int i = 0; i < 4; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(0,255,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
}

I would need to see the whole code… seems your {} brackets do not balance

here is a small piece of code for you to study (console @115200 bauds).

const unsigned long period = 90ul; // should be 9000ul for 9 seconds, here 100x shorter for testing without waiting :)
const unsigned long timeColorChange = 540ul; // should be 54000ul for 54 seconds, here 100x shorter for testing without waiting :)

unsigned long lastTick;

uint16_t nextPixel;
const uint16_t NUMPIXELS = 11;

uint32_t currentColor;
const uint32_t REDCOLOR = 0xF0FF0000;
const uint32_t WHITECOLOR = 0xFFFFFFF;

void lightingUpTo(int16_t n, uint32_t LRGB)
{
  Serial.print("Lighting LED");
  for (uint16_t i = 0; i <= n; i++) {
    Serial.print(" #");  Serial.print(i);
  }
  Serial.print(" in color 0x");  Serial.println(LRGB, HEX);
}


void setup() {
  Serial.begin(115200);
  nextPixel = 0;
  lastTick = millis();
  currentColor = REDCOLOR;
}

void loop() {

  if ((currentColor != WHITECOLOR) && (millis() >= timeColorChange)) {
    currentColor = WHITECOLOR;
  }


  if (nextPixel < NUMPIXELS) {
    if (millis() - lastTick >= period) {
      lightingUpTo(nextPixel, currentColor);
      nextPixel++;
      lastTick += period;
    }
  }
}

The serial console will show:

[color=red]Lighting LED #0 in color 0xF0FF0000
Lighting LED #0 #1 in color 0xF0FF0000
Lighting LED #0 #1 #2 in color 0xF0FF0000
Lighting LED #0 #1 #2 #3 in color 0xF0FF0000
Lighting LED #0 #1 #2 #3 #4 in color 0xF0FF0000[/color]
[color=blue]Lighting LED #0 #1 #2 #3 #4 #5 in color 0xFFFFFFF
Lighting LED #0 #1 #2 #3 #4 #5 #6 in color 0xFFFFFFF
Lighting LED #0 #1 #2 #3 #4 #5 #6 #7 in color 0xFFFFFFF
Lighting LED #0 #1 #2 #3 #4 #5 #6 #7 #8 in color 0xFFFFFFF
Lighting LED #0 #1 #2 #3 #4 #5 #6 #7 #8 #9 in color 0xFFFFFFF
Lighting LED #0 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 in color 0xFFFFFFF[/color]

of course I added the color for you to see that at 54s the color would change (I accelerated the timing x100 to not wait 99 seconds for this to print :slight_smile: )

is this what you are trying to do in terms of code structure?

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif


#define NUMPIXELS      11
#define ledPin

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);


unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin=D3; //neopixel led strip



void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
  pinMode(D5, OUTPUT);
}

void loop()
{

  
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= 9000)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }


{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  else if (currentMillis - startMillis >= 18000)  //test whether the period has elapsed
  {
    for (int i = 0; i < 1; i++) 
  {

    // pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }

This is the entire piece of code i was trying to get working, i think i need to include the Adafruit_Neopixel line and library to get it working. And thanks for the code that you posted! i kind of get it but it looks quite different from mine, although if i get the leds to work on this code it would work! :smiley:

pannekoek141:
i kind of get it but it looks quite different from mine, although if i get the leds to work on this code it would work! :smiley:

usually it's easier to start with the structure of the code that grabs the essence of what you are trying to do. then instead of printing texte, just perform the action...

you know how to turn a LED on.. you know how to represent colors... should be within reach..

Unfortunately it doesn’t feel like that for me, I have been trying from 13.00 and i only got the leds working, kind off :frowning: I also keep getting errors on this line

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);

I do understand that i need to call “pixels” which it does in the for loop but it doesn’t seem to work because it’s inside the for loop.

I understand that the best thing would be to start from the ground up but i’m really not good enough in coding to do that :slightly_frowning_face:

Aaah i just saw the code that you posted, im going to try to get it to work on the leds!

what about something like this:

#include <Adafruit_NeoPixel.h>
const byte ledPin = D3;
const uint16_t NUMPIXELS = 11;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
uint16_t nextPixel;

uint32_t currentColor = 0;
uint32_t STARTCOLOR;
uint32_t ENDCOLOR;


const unsigned long period = 90ul; // should be 9000ul for 9 seconds, here shorter for testing without waiting :)
const unsigned long timeColorChange = 540ul; // should be 54000ul for 54 seconds, here shorter for testing without waiting :)

unsigned long lastTick;



void lightingUpTo(uint16_t n, uint32_t stripColor)
{
  Serial.print("Lighting LED");
  for (uint16_t i = 0; i <= n; i++) {
    Serial.print(" #");  Serial.print(i);
    pixels.setPixelColor(i, stripColor);
  }
  Serial.print(" in color 0x");  Serial.println(stripColor, HEX);
  pixels.show();
}


void setup() {
  Serial.begin(115200);

  pixels.begin(); // This initializes the NeoPixel library.
  pixels.setBrightness(127); // half max brightness
  pixels.clear(); // all black

  nextPixel = 0;
  lastTick = millis();
  STARTCOLOR = pixels.Color(255, 0, 0); // RED
  ENDCOLOR = pixels.Color(0, 0, 255); // BLUE
  currentColor = STARTCOLOR;
}

void loop() {

  if ((currentColor == STARTCOLOR) && (millis() >= timeColorChange)) {
    currentColor = ENDCOLOR;
  }


  if (nextPixel < NUMPIXELS) {
    if (millis() - lastTick >= period) {
      lightingUpTo(nextPixel, currentColor);
      nextPixel++;
      lastTick += period;
    }
  }
}

untested - don’t know if that will work

Sweet! its working, only need to figure out why the color isnt changing after 540ul. working on that right now