Hello everyone!
I am new around here and new to Arduino. I thought it would be fun to learn by designing a project and working to completion. I didn’t realize how much trouble my little project would give me.
I am making a small marquee sign in the shape of an arrow. I have been working to program in different light patterns that I can change with the press of a button. Like all beginners, I started using delay(). It took me a while to realize that the delay() was what was causing intermittent and unpredictable input from my button. I then discovered millis() which should allow me to replace delay(). I sketched out some code and got the button working on a single LED, which I can then implement into my larger sketch. I am now trying to re-sketch my light patterns (avoiding delay() ) and I am having problems.
I don’t think I fully understand how millis() works. I have read multiple threads here, as well as several other pages that explain it and watched handfuls of YouTube videos. I have spent hours playing with the “blink without delay” and “several things at the same time” sketches, but it does not seem to be sinking in. For the life of me, I cannot figure out how to code the same patterns I had while using delay(). I don’t understand how millis() can program in a “pause” between actions.
For instance, I have a sequential lighting of the LEDs and then they sequentially turn off. This was easy using delay(). Here is a section of that code:
/*
This pattern will:
Sequentially light 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 one at a time
Sequentially light 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Stay on for 1.5 seconds
Sequentially turn off 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Repeat
*/
//Declarations
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //Sets up output pin array
int pinCount = 10; //Declares number of pins in array
int timer = 500;
void setup(){
//Sets pins 2 through 11 as output pins
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{pinMode(ledPins[thisPin], OUTPUT);}
delay(timer);}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
delay(timer / 10);}
delay(timer);
// Sequential light
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer / 5);}
delay(timer * 3);
// Sequential off
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], LOW);
delay(timer / 5);}
delay(timer);
}
I tried to recreate this pattern using millis() and failed miserably. Tried again, failed again. Tried again... you get the picture. Frustrated, I searched for ways to do it not using millis(). I found a library called elapsedmillis (Arduino Playground - elapsedMillis Library) that I thought would help me solve the problem, but I can’t seem to get it to work either. It gives me random lighting of the LEDs, not in the order that I have them listed in the sketch and I do not understand why. On top of that, if I change the interval, the LEDs light in a different random pattern. This piece of code has been simplified to just 5 LEDs to see if I could get it to function on a smaller scale, but I am currently having issues with it (here I am just trying to turn the LEDs on sequentially and leave them on):
#include <elapsedMillis.h>
void setup() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
elapsedMillis elapsedTime;
unsigned int interval = 250;
void loop() {
if (elapsedTime >= interval) {
digitalWrite(13, HIGH);
Serial.println(elapsedTime);
elapsedTime = 0;
}
if (elapsedTime >= interval) {
digitalWrite(12, HIGH);
Serial.println(elapsedTime);
elapsedTime = 0;
}
if (elapsedTime >= interval) {
digitalWrite(11, HIGH);
Serial.println(elapsedTime);
elapsedTime = 0;
}
if (elapsedTime >= interval) {
digitalWrite(10, HIGH);
Serial.println(elapsedTime);
elapsedTime = 0;
}
if (elapsedTime >= interval) {
digitalWrite(9, HIGH);
Serial.println(elapsedTime);
elapsedTime = 0;
}
}
This code makes the LEDs light in a random order, but the serial.println(elapsedTime); will almost always print out the 250 milliseconds that the timer makes. Occasionally it will throw out a 251.
I have gotten frustrated and my brain seems to have stopped making any sense of this at all. I would like a fresh set of eyes to look at it and maybe help me understand why I cannot get it to work like I want it to.
I am fairly sure that my problem is with the code because my breadboard works perfectly with delay(). But, just in case, here is a render of how my breadboard and Arduino are connected.
I think I am on the verge of completing my first real Arduino project if I can just rebuild my light patterns, I can then implement them with my other state change code with the working button and assemble the final product.
In all honesty, I have worked on this for over a year on and off. After several months of not touching it, I have broken it all out again in the hopes that I can finally complete the project.
Any guidance that you can give will be greatly appreciated. And, I would love to show off the final project when done. Thank you for taking the time to read this, and for any help you can throw my way. It is all very much appreciated.