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);
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.
}
}
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
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.
}
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 )
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!
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!
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 I also keep getting errors on this line