Noob asks WHY????

My friends all tell me I am a smart guy, but damn I feel dumb right now… First let me say that I am just a NOOB and am struggling with understanding programming an Arduino. Forgive my frustration please. I am trying to build an automation system for a diesel generator I am building- I want to start simple and hand a running engine to the Arduino and have it simply watch sensors to control cooling and shut down if overtemp or oil pressure drops. I have declared my pins globally at the beginning of the sketch, but when I verify it, it tells me ‘… not declared in this scope’. WTF? If its declared globally, isn’t it declared for EVERY scope? What am I doing wrong? Yes I checked spelling errors first, I checked against other sample sketches and cannot figure it out… Anyone wanna get paid to mentor me?

It would help a great deal to see your sketch and to see the actual error message you are receiving. Without the details, it will be hard to give any really useful advise on this.

Without any actual code, all we can offer is shots in the dark...

Did you check the case?

myPin

is different than

mypin

How to use this forum

Read this before posting a programming question

Ouch! DangIt Arrch don't do that!

@ Nick- sorry- I read that stuff, but I couldn't find what you are trying to tell me. I guess I screwed up somehow here. I'll just try to figure it out myself then and not bother you all anymore... oh well. Thanks anyway...

Eric_Maki: I have declared my pins globally at the beginning of the sketch, but when I verify it, it tells me '... not declared in this scope'.

What people are telling you is that if you want to know about the cause of an error you need to post the code that causes the error. The most likely cause is a typo somewhere and the compiler tells you which line contains the error, but we can only guess.

Eric_Maki: @ Nick- sorry- I read that stuff, but I couldn't find what you are trying to tell me.

See reply #1.

How can we help you with your code if you don't post it? How can we help with the error message if you don't post that?

Eric_Maki: @ Nick- sorry- I read that stuff, but I couldn't find what you are trying to tell me. I guess I screwed up somehow here. I'll just try to figure it out myself then and not bother you all anymore... oh well. Thanks anyway...

Well with that attitude, I can't imagine why people aren't jumping to giving you the answer. :roll_eyes:

Eric_Maki: @ Nick- sorry- I read that stuff, but I couldn't find what you are trying to tell me.

For one thing it said:

Examples of unhelpful subject lines:

  • Noob here, help needed

Your "Noob asks WHY????" is an example of an unhelpful subject.

Then, did you really, really, read this bit?

11. Tips for getting the most out of your post

Mention which Arduino you have. Is it a Uno? Leonardo? Due? Mini? Mega? The problem might be specific to a certain model. ... Post a complete sketch (program code)! If you don't you waste time while people ask you to do that. However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code. If the problem goes away in the minimal sketch, it wasn't where you thought it was. ... If you get an error, post the error (copy and paste). Not just "I got an error".

You didn't post your code, mention what Arduino you have or post your error message. We aren't trying to attack you, but you aren't trying to help us to help you.

Its actually a shame he gave up, I would like to have seen what the cause of the problem was.....

Don't feed the trolls.

Forget trolls,

If its declared globally, isn’t it declared for EVERY scope?

is this true or not?

Boffin1: Forget trolls,

If its declared globally, isn't it declared for EVERY scope?

is this true or not?

If a variable is defined outside of all code block (context) then yes, you can retrieve and set it from any code block (global).

Thats why I was curious ...

Boffin1: Forget trolls,

If its declared globally, isn't it declared for EVERY scope?

is this true or not?

Mostly, but there are some gotchas:

  • Declarations are processed in the order they occur in the source file. So if you have a function that references a global variable earlier in the source file than the declaration of the global variable, the compiler will complain.
  • If you have split the code into multiple libraries, the libraries will only be able to reference the global variable if they have a declaration in scope. Typically you would put the declaration of a global variable in a common include file (using the extern keyword), and a definition of the global variable in the library source file.
  • Declarations of the same variable in an inner scope hides the reference to the global variable for the scope of the block that the definition is in.
  • Similarly, if you use #define of the name, it hides reference to the name until you #undef the name.

Huha Thanks Michael,

I think I have run foul of a couple of those gotchas along the way, and had to Mgyver my way out.

I will keep a note of these for next time.

Also - If you declare a global, and then inside a function declare another variable with the same name the local gets used and not the global. Have seen a couple times where a noob did that thinking it was the same variable. The local worked, but it didn’t have the global value…
and then when the function exited the global value was unchanged…

Amazed to see "WTF"...

Amazed to see "WTF"...

?

This must be getting on for one of the longest threads with the least OP input.

Conjecture on, dudes.