Including my code. and link to Tinkercad so you can physically see what’s going on. Never used it much before, but it’s helped me debug a few things. I’ve been working on this code now for a few days and can’t figure out why it keeps stopping and won’t keep looping. The Tinkercad helps with visually seeing what’s wrong.
Code has notes at beginning as to what’s going on. I’d appreciate if you leave detailed comments as to what you would change and why so I can better understand. Thank you in advance. I’ll check back in a few days (it should rain this week, which means no work)
//UFO Green
/*This is for a project of mine, and I will have to add to it
eventually. It's sort of a knight rider sketch.
I have a second sketch that controls other pins (LEDS) and they need
to blink independently of this sketch. However, what I am encountering is
the LEDS will blink, in the array (using the on board LED to add some time
between the 12 pin LED) correctly once. And then it has a huge delay, and stops
all together. I need some help. Trying to avoid using delays. See note for while command
line 49 */
byte ledPin[] = {9, 10, 11, 12, 13, 12, 11, 10, 9}; // Create array for LED pins
int ledDelay = 200; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
for (int x = 0; x < 13; x++)
{
// set all pins to output
pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();
}
void loop() {
if ((millis() - changeTime) > ledDelay)
{
// if it has been ledDelay ms since last change
changeLED();
changeTime = millis();
}
}
void changeLED()
{
for (int x = 0; x < 12; x++)
{
// turn off all LED's
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
// change direction if we reach the end
if (currentLED > 13) {
direction = -1;
}
for (int x = 0; x > 13; x--)
{
if (currentLED < 9) {
direction = 1;
while (15); //I added this hoping that it would recycle my loop iteration
// but it doesn't seem to want to do it more than once
}
}
}