First off, im new to Arduino,but not to electronics. lines of code just dont mix with me!
Im working with some WS2812 leds. what I want to do is run a pattern for ten times and then run another pattern 10 times. and of course repeat. I already have the patterns working individually, but i cant figure out how to have them run a few times each, instead of just once. Ive attached a basic piece of code that works, but each pattern only runs once.
Help please!
Thanks
#include "FastLED.h"
#define NUM_LEDS 4
#define DATA_PIN 3
#define CLOCK_PIN 13
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
}
void loop() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Green;
FastLED.show();
leds[dot] = CRGB::Red;
delay(500);
}
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Red;
FastLED.show();
leds[dot] = CRGB::Green;
delay(500);
}
}/code]
INTP
September 5, 2016, 2:12am
2
for loops
for (byte i = 0; i < 10; i++){
//whatever it is you want to happen 10 times
}
MorganS
September 5, 2016, 2:14am
3
#include "FastLED.h"
#define NUM_LEDS 4
#define DATA_PIN 3
#define CLOCK_PIN 13
#define NUM_PATTERN1 10
#define NUM_PATTERN2 10
CRGB leds[NUM_LEDS];
void pattern1() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Green;
FastLED.show();
leds[dot] = CRGB::Red;
delay(500);
}
}
void pattern2() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Red;
FastLED.show();
leds[dot] = CRGB::Green;
delay(500);
}
}
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
for(int i=0; i<NUM_PATTERN1; i++) {
pattern1();
}
for(int i=0; i<NUM_PATTERN2; i++) {
pattern2();
}
}
void loop() {
//empty - any action you want to repeat 'forever' goes here
}
Any questions?
This is very close to what im looking for. but after the last pattern, it just stops. im looking for it to the pattersn to repeat themselfs again
MorganS:
#include "FastLED.h"
#define NUM_LEDS 4
#define DATA_PIN 3
#define CLOCK_PIN 13
#define NUM_PATTERN1 10
#define NUM_PATTERN2 10
CRGB leds[NUM_LEDS];
void pattern1() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Green;
FastLED.show();
leds[dot] = CRGB::Red;
delay(500);
}
}
void pattern2() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Red;
FastLED.show();
leds[dot] = CRGB::Green;
delay(500);
}
}
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
for(int i=0; i<NUM_PATTERN1; i++) {
pattern1();
}
for(int i=0; i<NUM_PATTERN2; i++) {
pattern2();
}
}
void loop() {
//empty - any action you want to repeat 'forever' goes here
}
Any questions?
MorganS
September 5, 2016, 2:25am
5
Then move those two new for() loops into the loop() function. (I did put a comment there saying you could do that.)
J-M-L
September 5, 2016, 2:25am
6
EDIT - was too slow to type this. Answered correctly just above
MorganS:
Any questions?
Would just suggest to put in the loop() as OP specification was
what I want to do is run a pattern for ten times and then run another pattern 10 times. and of course repeat.
#include "FastLED.h"
#define NUM_LEDS 4
#define DATA_PIN 3
#define CLOCK_PIN 13
#define NUM_PATTERN1 10
#define NUM_PATTERN2 10
CRGB leds[NUM_LEDS];
void pattern1() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Green;
FastLED.show();
leds[dot] = CRGB::Red;
delay(500);
}
}
void pattern2() {
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Red;
FastLED.show();
leds[dot] = CRGB::Green;
delay(500);
}
}
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
}
void loop() {
for(int i=0; i<NUM_PATTERN1; i++) {
pattern1();
}
for(int i=0; i<NUM_PATTERN2; i++) {
pattern2();
}
}
INTP
September 5, 2016, 2:31am
7
Since we're going for excessive, might as well throw in
#define delayTime 500
and
delay(delayTime);
boolean pattern1 = true;
int numTimes = 0;
loop() {
if(pattern1) {
run_pattern_1();
numTimes++;
if(numTimes >= 10) {
pattern1 = false;
numtimes = 0;
}
}
else {
run_pattern_2();
numTimes++;
if(numTimes >= 10) {
pattern1 = true;
numtimes = 0;
}
}
}
Morgan and JML hit it on the head! thanks! I wish I was that good and quick with this. I could build what i wanted with RGB leds, diodes, a 555 timer and a couple decade counters faster than i could code this!