How do I differentiate between Initialization and Declaration?

Hello all,

I have written this above setup.

int life = 3

Does this tell the arduino that pin 3 is now called life, or does it give the integer life an initial value of 3?

If someone could tell me how to do both things, that would be most helpful.

I would also like to know how to set an integer (that has been happily counting) back to zero before the loop starts again.

Thank you for your time!

Joe.

Does this tell the arduino that pin 3 is now called life, or does it give the integer life an initial value of 3?

It tells the compiler you want a 16 bit global variable called "life", and you want its value to be three before setup runs. Nothing at all to do with pins, unless YOU want it to be.

I would also like to know how to set an integer (that has been happily counting) back to zero before the loop starts again.

Happy or sad, set it back to zero when you've finished with it.

Holy pants that was fast AWOL. Thanks for the reply. Could you tell me how I would assign 'life' to a pin?

AWOL: Happy or sad, set it back to zero when you've finished with it.

Like this?

int life == 0;

Could you tell me how I would assign 'life' to a pin?

You can't assign a sixteen bit variable to a one bit pin, but you can use it as the name of the pin, e.g.

int life = 3;
pinMode (life, OUTPUT);

but there's nothing which binds the variable to pin 3.

No, not like that, that's creating a new "life" (and we all know what trouble that can land you in). Lose the "int" and one of the "="

AWOL: Happy or sad, set it back to zero when you've finished with it.

like this:

x = 0;

Getting there!

So if I was to write

digitalWrite(life, HIGH);

How would it know which pin to make high? I have a feeling I have massively misunderstood something fundamental about arduino (or C)

How would it know which pin to make high?

It takes the value in the variable "life" and uses that to address the pin.

Haddonjw: I have a feeling I have massively misunderstood something fundamental about arduino (or C)

Well, about high level languages you have... a initialization assigns a symbol to a value. "life" is the symbol, 3 is the value. But a declaration may not initialize the value by default. You can just declare "life" and assign a value later. Assignment is a mechanism for changing the value associated with a symbol. A reference to the symbol produces the value.

Behind the scenes, a value is stored in a memory location. You don't have to sweat that too much unless you're using pointers. But a declaration with no initializer reserves a memory location for future values to be saved.

It's more a lot more complicated than this, but the short version is that the magic is essentially based on giving names to memory locations.

You assign the pin numbers at the top of your sketch.

byte life = 13; // can be any pin - use 13 as that's the onboard LED

void setup(){
pinMode (life, OUTPUT);
}

void loop(){
// make life pin go High & Low about every 1/2 second
digitalWrite (life, HIGH);
delay (500);
digitalWrite (life, LOW);
delay(500);
}

So if I was to write

int life = 3;
digitalWrite(life, HIGH);
life - 2;
digitalWrite(life, LOW);
life = 0

Would that turn pin 3 on, then pin 1 and then reset life to 0?

I really do appreciate your time, you have all given me spectacularly useful info.

Haddonjw: Getting there!

So if I was to write

digitalWrite(life, HIGH);

How would it know which pin to make high? I have a feeling I have massively misunderstood something fundamental about arduino (or C)

That line of code is equivalent to this:

digitalWrite(3, HIGH);

It sets pin 3 HIGH. But the variabe has no special meaning as a pin, it just means 3. That 3 could be a pin number, a hockey score, or the number of times you've scratched your nose today. It doesn't matter. But digitalWrite expects the first argument to be a pin number, so digitalWrite takes whatever number you give it and operates on the pin with the same number.

Would that turn pin 3 on, then pin 1 and then reset life to 0?

No, because

life - 2;

doesn't do anything useful (though the compiler won't complain).

life = life - 2;

is one version of what you want.

Haddonjw: So if I was to write

int life = 3;
digitalWrite(life, HIGH);
life - 2;
digitalWrite(life, LOW);
life = 0

Would that turn pin 3 on, then pin 1 and then reset life to 0?

I really do appreciate your time, you have all given me spectacularly useful info.

That would set pin 3 HIGH, then low.

This line means nothing.

life -2 ;

That's just the same as a statement saying

1;

It's legal, but does nothing.

life = life -2;

or

life -= 2;

AWOL and Delta_G, I think you may have cracked it! I am writing a code for a simple 2 team IR laser tag game. This is the life counter.

I will quickly go stuff in these changes and let you know.

Thanks so very much.

The program that CrossRoads gave you is similar to the Blink program that comes with the Arduino IDE. You can load that program with the menu sequence:

File --> Examples --> 01.Basics --> Blink

That program looks like:

void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

As CrossRoads pointed out, pin 13 is the pin that has the onboard LED tied to it. Most C programmers, however, don't like "magic numbers" in their programs, so the will define a variable and use that instead. So you could modify the Blink program as:

int ONBOARD_LED = 13;

void setup() {
  // initialize digital pin 13 as an output.
  pinMode(ONBOARD_LED , OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(ONBOARD_LED , HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(ONBOARD_LED , LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

In this case, ONBOARD_LED is a variable that is initialized to a value of 13. Functionally, the two programs are identical, but the second flavor is easier to read because the value 13 is now assigned to a variable with a name that makes more sense in context. While not required, because the onboard LED pin doesn't change for most Arduino boards, it's a constant pin number from board-to-board. Therefore, most C programmers would use uppercase letters for the variable name so the person reading the code knows its a variable that shouldn't be changed. If you want to make sure the variable doesn't change, you could define it as:

const int ONBOARD_LED = 13;

With this definition, any attempt to change its value in the program will draw an error message from the compiler.

Would that turn pin 3 on, then pin 1 and then reset life to 0?

No, one would typically use an array for that.

byte pinsArray[] = {3,1,0,}; // be careful using 1, 0 as those are also the Serial port pins
// pinsArray[0] = 3, pinsArray[1] = 1, pinsArray[2] = 0

byte x;  // variable to use in a loop

void setup(){
for (x = 0; x<3; x=x+1){ // count from 0,1,2
pinMode (pinsArray[x],  OUTPUT); // set pin 3, then 1, then 0 as outputs
}

void loop(){
for (x = 0; x<3; x=x+1){ // count from 0,1,2
digitalWrite (pinsArray[x],  HIGH); // set pin 3, then 1, then 0 HIGH
}
delay(100); // 100mS pause
for (x = 0; x<3; x=x+1){ // count from 0,1,2
digitalWrite (pinsArray[x],  LOW); // set pin 3, then 1, then 0 LOW
}
delay (100);
} // end of loop

Great info thanks econjack. Crossroads, the array thing made me want to run away to my mummy… riding a pink vespa…

I put in my new knowledge about changing variables… it seems to sort of do it…

Here’s the code. I have some kind of craziness going on where it will lose 1 life on the first hit but then two or three on the next successive one’s

#define RCVR 2    //IR RCVR Signal pin
#define RPOS 5    //IR RCVR positive pin
#define EMIT 3    //IR LED pin
#define TS  13    //Team Switch Common
#define TA 12     // Team Switch is A
#define TB 7      // Team Switch is B
#define TRIG 4    //Trigger pin
#define lONE 9    //One life left
#define lTWO 10   //Two lives left
#define lTHREE 11 //Three lives left
int life;     // Life Counter, Get three on reset
int pLen;         // To be used to measure Pulse Length
int lenCount;     //Counts the number of incoming pulses 
int aHit;         //Gives number of A Pulses that equals a hit
int bHit;         //Gives number of B Pulses that equals a hit
int sigGEN;       //Makes the A and B Signals

void setup() 
{
    pinMode (RCVR, INPUT);
    pinMode (RPOS, OUTPUT);
    pinMode (EMIT, OUTPUT);
    pinMode (TS, OUTPUT);
    pinMode (TA, INPUT);
    pinMode (TB, INPUT);
    pinMode (TRIG, INPUT);
    pinMode (lONE, OUTPUT);
    pinMode (lTWO, OUTPUT);
    pinMode (lTHREE, OUTPUT);
    if(life == 0)             //There are 0 lives at start
      {life++;                //add first
      life++;                 //add second
      life++;                 //add third
      }
    Serial.begin(9600); // open a serial connection to your computer
}


void loop() 
{ 
  if(life == 3)
     {digitalWrite(lONE, HIGH);                                        //
      digitalWrite(lTWO, HIGH);
      digitalWrite(lTHREE, HIGH);
     }
   if(life == 2)
     {digitalWrite(lONE, HIGH);                                        //
      digitalWrite(lTWO, HIGH);
      digitalWrite(lTHREE, LOW);
     }
   if(life == 1)
     {digitalWrite(lONE, HIGH);                                        //
      digitalWrite(lTWO, LOW);
      digitalWrite(lTHREE, LOW);
     }
   if(life == 0)
     {digitalWrite(lONE, LOW);                                        //
      digitalWrite(lTWO, LOW);
      digitalWrite(lTHREE, LOW);
     }
     if(life < 0)
     {digitalWrite(lONE, LOW);                                        //
      digitalWrite(lTWO, LOW);
      digitalWrite(lTHREE, LOW);
     }
  digitalWrite(RPOS, HIGH);                                        //Turn on the Receiver
          if (digitalRead(RCVR)== 0)                               // If the RCVR pin has started to receive
              {for (int lenCount=0; lenCount <8; lenCount++)    // Do the following 10x
                    {pLen = pulseIn(RCVR, LOW);                    //Measure the length of the received pulse
                     if (pLen > 8000)                              //If it is more than 8000
                         {if(pLen < 12000)                         //and less than 12000
                             {aHit++;                              //Count one pulse for A
                             }
                         }
                      if (pLen > 27000)                           //If it is more than 8000
                         {if(pLen < 31000)                        //and less than 12000
                             { bHit++;                            //Count one pulse for B
                             }
                         }
                    }                                             //Move on if count gets to <8
                                Serial.print(life);
                                if(aHit > 5)                      //If there are more than 5 A pulses
                                  {digitalWrite(TS,HIGH);         //Make the Team Switch live
                                      if(digitalRead(TA) == HIGH) //If Team Switch is A (Friendly Fire)
                                          {life-=2;                //Lose 2 lives
                                          }
                                      if(digitalRead(TB) == HIGH) //If Team Switch is B (Enemy Fire)
                                          {life--;                //Lose 1 life
                                          }
                                  }
                                if(bHit > 5)                      //If there are more than 5 A pulses
                                  {digitalWrite(TS,HIGH);         //Make the Team Switch live
                                      if(digitalRead(TB) == HIGH) //If Team Switch is B (Friendly Fire)
                                          {life-=2;                //Lose 2 lives
                                          }
                                      if(digitalRead(TA) == HIGH) //If Team Switch is A (Enemy Fire)
                                          {life--;                //Lose 1 life
                                          }
                                          
                                  }
                                    Serial.print(life);
                                   Serial.println();
              }
                                aHit = 0;                        //Reset aHit
                                bHit = 0;                        //Reset bHit
}

Here’s the serial after a few shots… 3 being the lives before, 2 after.
32
20
0-3
-3-6
-6-9
-9-12
-12-14
-14-16
-16-19
-19-21
-21-24

I am hoping the serial to run 32 21 10 0-1 -1-2

etc

most C programmers would use uppercase letters for the variable name so the person reading the code knows its a variable that shouldn't be changed

Course, I never claimed to be a C programmer, I'm just a hardware designer who plays at being a programmer here 8) So my examples may not follow all the software conventions ...