Helo
I have a problem with my code, only half of the outputs work, i mean pins: 14, 15, 16, 18, 0.
Any idea where is the problem?
int ledPins[] = {14, 15, 16, 18, 0, 1, 2, 3, 4, };
int elementCount = sizeof(ledPins) / sizeof(int);
int delayTime = 25;
void setup()
{
digitalWrite(ledPins[14], HIGH);
digitalWrite(ledPins[15], HIGH);
digitalWrite(ledPins[16], HIGH);
digitalWrite(ledPins[18], HIGH);
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
digitalWrite(ledPins[4], HIGH);
for (int index = 0; index <= elementCount; index++)
{
pinMode(ledPins[index], OUTPUT);
}
}
void loop()
{
oneAfterAnotherNoLoop();
}
void oneAfterAnotherNoLoop()
{
delay(delayTime);
for (int index = 0; index <= elementCount; index++)
{
digitalWrite(ledPins[index], LOW);
delay(delayTime);
}
}
This addresses a non-existent element of the array ledPins[].
Use ledPins[0] for digital pin 14.
Also, take out the last comma, here:
int ledPins[] = {14, 15, 16, 18, 0, 1, 2, 3, 4, };
1 Like
Things like this:
digitalWrite(ledPins[16], HIGH);
Are writing off the end of your array, it doesn't have a 16th element.
Do that stuff in the loop where you set the pinMode.
Also
will reference an element outside the array.
Instead, use
for (int index = 0; index < elementCount; index++)
Once you call
oneAfterAnotherNoLoop();
for the first time, all outputs will be LOW and stay like LOW. I guess you will eventually add code to bring them back to HIGH.
And, pinMode goes before digitalWrite
I forgot to mention what is the idea of the project. The idea is to turn all outputs on so the leds turn on and then one after another go off. In current configuration only pins 18 16, 15, 14, 0 turn on and works as should. The 1, 2, 3, 4 are off whole time.
All runs on atmega8a
I can change pins configuration but it always turn only first 5 pins declared in code and that is my problem, it stick to 5 pins.
Ok, now works perfectly but i have no idea why
int ledPins[] = {14, 15, 16, 18, 0, 1, 2, 3, 4, 5, 6, 7, 8};
int elementCount = sizeof(ledPins) / sizeof(int);
int delayTime = 25;
void setup()
{
for (int index = 0; index < elementCount; index++)
{
pinMode(ledPins[index], OUTPUT);
}
}
void loop()
{
digitalWrite(ledPins[14], HIGH);
digitalWrite(ledPins[15], HIGH);
digitalWrite(ledPins[16], HIGH);
digitalWrite(ledPins[18], HIGH);
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
digitalWrite(ledPins[4], HIGH);
digitalWrite(ledPins[5], HIGH);
digitalWrite(ledPins[6], HIGH);
digitalWrite(ledPins[7], HIGH);
digitalWrite(ledPins[8], HIGH);
oneAfterAnotherNoLoop();
}
void oneAfterAnotherNoLoop()
{
delay(delayTime);
for (int index = 0; index < elementCount; index++)
{
digitalWrite(ledPins[index], LOW);
delay(delayTime);
}
}
nexusprime:
digitalWrite(ledPins[14], HIGH);
digitalWrite(ledPins[15], HIGH);
digitalWrite(ledPins[16], HIGH);
digitalWrite(ledPins[18], HIGH);
Still wrong. Your array has 13 elements, numbered 0 through 12. It looks like you mean:
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
digitalWrite(ledPins[4], HIGH);
digitalWrite(ledPins[5], HIGH);
digitalWrite(ledPins[6], HIGH);
digitalWrite(ledPins[7], HIGH);
digitalWrite(ledPins[8], HIGH);
digitalWrite(ledPins[9], HIGH);
digitalWrite(ledPins[10], HIGH);
digitalWrite(ledPins[11], HIGH);
digitalWrite(ledPins[12], HIGH);
Of course, a loop would be a better way to do that.
dlloyd
March 19, 2022, 2:24am
8
Shouldn't use pins 0 and 1 as these are used for serial communication (programming) the atmega8a. Perhaps replace them with pins 17 and 19 to get this ...
int ledPins[] = {14, 15, 16, 17, 18, 19, 2, 3, 4, 5, 6, 7, 8};
this all is pretty strange because i use only 9 pins, those 5, 6, 7, 8 are ghost pins which i do not use but it need to be write in the program so the 0, 1, 2, 3 do the job.
Hello
Delete all "goast pins" from the array and correct the part of the sketch who work with the array.
system
Closed
September 15, 2022, 2:40pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.