Why do I need this?

why do I need to use the "int" again on counterLED below void loop, if I had already used it in void setup? It says that counterLED wasnt enabled in this scope, but I thought the int counterLED inside the for loop at void setup would have done the job. I fattened the code that Im trying to understand. The code: int counter; int delay1 = 666; void setup() { * this is the first time is use int* for(int counterLED=2;counterLED<=6;counterLED++) { pinMode(counterLED, OUTPUT); } Serial.begin(9600); } void loop() { * why is the int needed here again for the same variable?* for (int counterLED=2; counterLED<=6;counterLED++) { digitalWrite(counterLED, HIGH); delay(delay1); digitalWrite(counterLED, LOW); } thanks a lot
22 Replies
MaderDash
MaderDash3mo ago
@TheStudier do you have a local grocery store near you?
TheStudier
TheStudierOP3mo ago
yes a ffew
MaderDash
MaderDash3mo ago
I have one near me also.
TheStudier
TheStudierOP3mo ago
im gonna go to one of them in the morning prolly at 6 am
MaderDash
MaderDash3mo ago
If I told you now. Go to the local grocery store and I will meat you there in 5 minutes. And I went to mine.. do you think we would meet?
TheStudier
TheStudierOP3mo ago
no
MaderDash
MaderDash3mo ago
Why
TheStudier
TheStudierOP3mo ago
i dont know what store you talking about
MaderDash
MaderDash3mo ago
The local grocery store
TheStudier
TheStudierOP3mo ago
i dont know where you live
MaderDash
MaderDash3mo ago
The local grocery store
TheStudier
TheStudierOP3mo ago
yes maybe by luck we could meet
MaderDash
MaderDash3mo ago
But there is no garentee right?
TheStudier
TheStudierOP3mo ago
indeed
MaderDash
MaderDash3mo ago
Bingo. When you make a varable INSIDE a function its called a LOCAL varable That is it only exists INSIDE that one varable.
TheStudier
TheStudierOP3mo ago
do you mean it exists inside that one function instead of variable? "That is it only exists INSIDE that one varable"
MaderDash
MaderDash3mo ago
No that VARABLE only exist inside that one FUNCTION
TheStudier
TheStudierOP3mo ago
yes i thougt you said varable exists in varable sorry
MaderDash
MaderDash3mo ago
Just like your physical store only exist in your town and mine in my town. So if you try and access it OUTSIDE of that function say frome loop... it does NOT exist. So you need to make it again.
TheStudier
TheStudierOP3mo ago
so the code in the void setup says "pinMode(counterLED, OUTPUT); wait nothing I think I understand nice
MaderDash
MaderDash3mo ago
Ha ha I no it sounds like im talking crazy.. but if you hang with me. You will get smarter and learn yhings.
TheStudier
TheStudierOP3mo ago
okay thanks a lot this was a huge helper, also, your profile says "everything is IMO" In my opinion it should say "everyting is IMO, IMO"

Did you find this page helpful?