Hi Everyone,
I’m working on a led project with a Mega 2560 where I created 2 different sketches and I need to combine them into one. The first sketch is comprised of 8 leds where each led turns on and stays on, one at a time, every 50ms, until all leds are lit and then they all shut off at once and the whole process repeats itself.
The 2nd sketch is comprised of 4 leds where each leds comes on for 1 second and then shuts off and then the next led lights up and does the same thing, this going left to right down a row of 4 leds. There is also a push button that when pressed reverses the order they light up, making it go right to left.
I used “delay” in both sketches to establish the timing and in order to combine them into one sketch I've been studying up on using “millis” for the last 5 days. After endless YouTube videos, articles and experimenting, I have a basic understanding of millis, but I just can’t get it to work and it feels like time to ask for some help.
When I wrote the sketches, I put all the leds in arrays and created “for() Loop” functions for them which I’m beginning to think is the problem why millis won’t work. The millis interval works fine during experimenting for turning specific leds on & off but I can’t figure out how to use it to run “for()Loop” functions, not to mention the push button in sketch #2.
I'm still pretty new to all this, it's kind of like trying to learn Chinese all by yourself. I’ll include both sketches and any help would be appreciated.
//Sketch #1
int timer = 50;
int blueLeds[8] = {2,3,4,5,6,7,8,9};
int pinCount = 8;
void setup() {
for (int thisPin = 0; thisPin < pinCount; thisPin++){
pinMode(blueLeds[thisPin], OUTPUT);
}
}
void loop() {
for (int thisPin = 0; thisPin < pinCount; thisPin++){
digitalWrite(blueLeds[thisPin], HIGH);
delay(timer);
}
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--){
digitalWrite(blueLeds[thisPin], LOW);
}
}
Sketch #2
int timer1 = 750;
int redLeds[4] = {32, 34, 36, 38};
int redLeds1[4] = {38, 36, 34, 32};
int pinCount1 = 4;
int button = 40;
int buttonState = 0;
void setup() {
for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
pinMode(redLeds[thisPin], OUTPUT);
pinMode(redLeds1[thisPin], OUTPUT);
pinMode(button, INPUT);
}
}
void loop() {
buttonState = digitalRead(button);
if (buttonState == LOW)
for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
digitalWrite(redLeds[thisPin], HIGH);
delay(timer1);
digitalWrite(redLeds[thisPin], LOW);
}
else
{
for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
digitalWrite(redLeds1[thisPin], HIGH);
delay(timer1);
digitalWrite(redLeds1[thisPin], LOW);
}
}
}