abbe33
December 10, 2021, 8:00am
1
I want the code to turn on some leds in order, then blink them and then loop it. I cannot find my misstake but when i turn on the code 4 leds immediately turn on and sometimes blink. What are my misstakes?
int allPins[] = {2,3,4,5,6,7,8,9};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
setPinMode(OUTPUT);
// put your main code here, to run repeatedly:
for(int i = 0; i < 9; i++){
digitalWrite(i, HIGH);
delay;1000;
Serial.print(i);
}
{
delay(10);
setPinStates(LOW);
delay(20);
setPinStates(HIGH);
delay(20);
setPinStates(LOW);
}
}
void setPinStates(byte state)
{
byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
for (int p = 0; p < numberOfPins; p++)
{
digitalWrite(allPins[p], state);
}
}
void setPinMode(byte mode)
{
byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
for (int p = 0; p < numberOfPins; p++)
{
digitalWrite(allPins[p], mode);
}
}
red_car
December 10, 2021, 8:07am
2
delay; 1000;
maybe...
delay(1000);
and
delay(10);
setPinStates(LOW);
delay(20);
setPinStates(HIGH);
delay(20);
setPinStates(LOW);
10ms, 20ms... you won't see that with the naked eye. Maybe try 500ms.
abbe33
December 10, 2021, 8:11am
3
abbe33:
t
so sloppy thanks. However, the 4 leds are still turned on after the first iteration even though the serial monitor shows the correct numbers now.
red_car
December 10, 2021, 8:13am
4
Post your code.. where do you write to the monitor apart from the statement below?
Serial.print(i);
abbe33
December 10, 2021, 8:15am
5
nowhere, I just used it to check if the iteration of the pins went as it should.
int allPins[] = {2,3,4,5,6,7,8,9};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
setPinMode(OUTPUT);
int i =0;
// put your main code here, to run repeatedly:
for(int i = 0; i < 9; i++){
digitalWrite(allPins[i], HIGH);
delay(1000);
Serial.print(i);
}
{
delay(10);
setPinStates(LOW);
delay(100);
setPinStates(HIGH);
delay(100);
setPinStates(LOW);
}
}
void setPinStates(byte state)
{
byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
for (int p = 0; p < numberOfPins; p++)
{
digitalWrite(allPins[p], state);
}
}
void setPinMode(byte mode)
{
byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
for (int p = 0; p < numberOfPins; p++)
{
digitalWrite(allPins[p], mode);
}
}
abbe33
December 10, 2021, 8:16am
6
The pins are still not turning on in a sequence. The blink part works and the i iterates but all leds are lit after the first iteration.
abbe33
December 10, 2021, 8:18am
7
I found it!
i added
setPinStates(LOW);
to the start of the loop thank you for your time friend!
red_car
December 10, 2021, 8:21am
8
btw.. this is not a good idea...
int i = 0;
// put your main code here, to run repeatedly:
for (int i = 0; i < 9; i++) {
declaring i
twice... asking for trouble. get rid of the first one.
and... put this in setup()
setPinMode(OUTPUT);
Why the magic number 9?
Your functions don't use magic numbers.
red_car
December 10, 2021, 9:04am
10
anon73444976:
Why the magic number 9?
and if that doesn't convince you...
Pages for logged out editors learn more
9 (nine) is the natural number following 8 and preceding 10.
In the beginning, various Indians wrote a digit 9 similar in shape to the modern closing question mark without the bottom dot. The Kshatrapa, Andhra and Gupta started curving the bottom vertical line coming up with a 3-look-alike. The Nagari continued the bottom stroke to make a circle and enclose the 3-look-alike, in much the same way that the sign @ encircles a lowercase a. As time ...
you still have undefined behaviour when read array out of bounds
system
Closed
June 8, 2022, 9:14am
12
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.