Hi there!
I've been playing around with Arduino for the first time, working on a project that makes three groups of LEDs do three different things.
Group 1 is 7 LEDs that chase left to right. (in practice it's actually a ring of LEDs that chase in a circle)
Group 2 is 1 LED that fades in and out.
Group 3 is 2 LEDs that flicker like candles.
I have the code.... let's say "working", but there is room for improvement.
-
I'd like group 1's chase to speed up and slow down. I really have no idea how to do this, and it's my main problem. I'd also love it if I could make this group of LEDs quickly fade out, but again, I have no idea how to implement this.
-
The chase itself seems glitchy, and not smooth. Like it stutters, almost.
Here's my current code, which has a lot of stuff I've cannibalised from various example code, and tried to edit:
// -----Definitions
#define GLOW 0
#define DIM 1
#define UP true
#define DOWN false
// ----CONSTANTS (won't change)
const int minPWM = 0;
const int maxPWM = 255;
const byte pwmLED = 9;
//------- VARIABLES (will change)
byte fadeDirection = GLOW;
int fadeValue = 0;
int flickerPin1 = 10;
int flickerPin3 = 11;
byte fadeIncrement = 5;
unsigned long previousFadeMillis;
unsigned long previouschaseLEDMillis=0;
unsigned long otherinterval = 30;
int fadeInterval = 20;
byte chaseLEDpins[] = {2,3,4,5,6,7,8};
int chaseLEDstate=0x01;
boolean direction=UP;
//========
void setup() {
pinMode(12, INPUT_PULLUP);
for (int x=0; x < 7; x++)
pinMode(chaseLEDpins[x], OUTPUT);
analogWrite(pwmLED, fadeValue);
pinMode(flickerPin1, OUTPUT);
pinMode(flickerPin3, OUTPUT);
}
void doTheFade(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
if (fadeDirection == GLOW) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
fadeValue = maxPWM;
fadeDirection = DIM;
}
} else {
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
fadeValue = minPWM;
fadeDirection = GLOW;
}
}
analogWrite(pwmLED, fadeValue);
previousFadeMillis = thisMillis;
}
}
//=======
void loop() {
for (int x=0; x < 7; x++)
digitalWrite(chaseLEDpins[x], bitRead(chaseLEDstate,x));
if (digitalRead(12) == LOW)
turnOnAll();
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previouschaseLEDMillis) >= otherinterval) {
previouschaseLEDMillis = currentMillis;
if (direction==UP) {
chaseLEDstate = chaseLEDstate << 1;
if (chaseLEDstate == 0x80) {
chaseLEDstate = 0x00;
direction = DOWN;
}
}
else {
chaseLEDstate = chaseLEDstate >> 1;
if (chaseLEDstate == 0x00) {
chaseLEDstate = 0x01;
direction = UP;
}
}
{
unsigned long currentMillis = millis();
doTheFade(currentMillis);
} {
analogWrite(flickerPin1, random(120)+135);
analogWrite(flickerPin3, random(120)+135);
delay(random(100));
}}
}
void turnOnAll() {
while (digitalRead(12)==LOW) {
for (int x=0; x< 6; x++)
digitalWrite(chaseLEDpins[x], HIGH);
}
}
//=====END
Any help would be great!
Thanks!!!