Is there a way to debug code in Setup? Trying to use a for

I have a Mega with 40 LEDs attached pins 2-42. Trying to use a for to set the range as output pins. Instead of 40 lines or using an array thought I would try a for.

Since this is declared in setup and not loop I’m thinking if there’s an error it would be harder to debug.
First question, how does one debug code, like a for statement in setup or about setup in global?

Here are two attempt at the code. Comments welcome.

Int LedPin;

void setup() {
Following line was posted incorrectly. See corrected line of code below.
for (LEDPin = 2; LEDPin < 43; LEDpin = LedPin LEDPin +1) {

pinMode(LEDPin, OUTPUT);

}

}


void setup() {

for (int LEDPin = 2; LEDPin < 43; LEDpin ++) {

pinMode(LEDPin, OUTPUT);

}

}

You haven't said what is wrong with either version.
But what is the point of "LEDpin = LEDPin"?

Pete

Debug, like using debug prints?
In setup()?

Sure. Why not?

You've got fewer than 100 posts, so five minutes between posts. You could have tried it in five minutes.

My bad.
I corrected in original post. Should be

for (LEDPin = 2; LEDPin < 43; LEDpin = LEDPin +1)

I corrected in original post.

Naughty !
Now posts that refer to the original don't make sense.

How 'bout:

for(byte q = 2;q <= 42;q++)
 pinMode(q,OUTPUT);

:wink: :slight_smile:

Why byte and not Int? I would think it would work.

But why byte? Is that becaouse it uses less memory? If not why?
Or is thay because one can?

I have a Mega with 40 LEDs attached pins 2-42

Do you have a current limit resistor on each LED?

At 20 mA/LED, 40 LEDs on at once could exceed the total current limit of the Mega, and the Mega voltage regulator will probably overheat.

https://playground.arduino.cc/Main/ArduinoPinCurrentLimitations

Serial.print() works just fine inside setup(). Just make sure it occurs after the Serial.begin().

Many Arduinos will reset when the Serial Monitor is opened, so you see the very first data printed with no delay. Others will not reset so you need some way of delaying the execution so that you can get the Serial Monitor connected. Have you heard of the delay() function?

I always use ints for pin numbers as some day I'm expecting to see an Arduino with more than 255 pins. If you think that day will never come and you think that it's important for this loop to execute as fast as possible with the least memory usage, then you can use byte.

jremington:
Do you have a current limit resistor on each LED?

At 20 mA/LED, 40 LEDs on at once could exceed the total current limit of the Mega, and the Mega voltage regulator will probably overheat.

https://playground.arduino.cc/Main/ArduinoPinCurrentLimitations

Thanks… I’m actually using a MOS FET boards which have a LED and limiting resister.
Sketch will not have more than 5 LEDs firing at one time.

MorganS:
Serial.print() works just fine inside setup(). Just make sure it occurs after the Serial.begin().

Many Arduinos will reset when the Serial Monitor is opened, so you see the very first data printed with no delay. Others will not reset so you need some way of delaying the execution so that you can get the Serial Monitor connected. Have you heard of the delay() function?

I always use ints for pin numbers as some day I'm expecting to see an Arduino with more than 255 pins. If you think that day will never come and you think that it's important for this loop to execute as fast as possible with the least memory usage, then you can use byte.

Thanks, didn't know Serial.print would work outside of loop. Does this mean I can use it in global? Or above loop and setup?

Yes I am familar with delay.

Thanks for sharing I could use Byte or Int.

With byte one is just limited to 256 where int is in the 65,000 range and can be positive or negitive?

But why byte? Is that becaouse it uses less memory? If not why?

Yes, why consume a 16 bit int to hold a 6 bit number? And it's a good habit to make you think about the size needed for a variable instead of blindly using int for small numbers.

I love it. Thank you.

Just getting started and you are mkaing me think and learn. +1