[SOLVED] Placement of variable types

Can someone please explain (in stupid man talk) why some people place their variable types in the setup, and some later in the actually bulk of the code? Ie currentvalue and previousvalue below...


Liquid Switch
Ryan Detzel - 33watts.com


#define SENSORPIN 3

byte previousValue;

void setup() {
  //Setup the input pin
  //Default to the up/on position
  previousValue = 1;

void loop() {
  byte currentValue = digitalRead(SENSORPIN);
  if (currentValue != previousValue){
    previousValue = currentValue;

Check this tutorial. It's a little more than I would want to try to rewrite ni a forum post.

Basically, it is all about variable scope. It controls where a variable is usable and what functions have access to it. For the beginner, they can all be global, but with larger programs you want to try to restrict variable scope as much as possible.

I as a hardware engineer put all my variable declarations before setup() so that any part of my program can access them. The software engineers don't like it, but I write for me and not for them 8)

Also look at post #6 at:

and see if that helps at all.

Arrrrrr. Thank you all. I definitely don't think I'm going to need anything other than globals but its good to learn just in case I come across a time when it comes in handy.

I have stacks of global scope definitions at the top of my code, going to have to try and clean it up at some point. :-/

Thanks for your responses though and great link Delta.