Can a varible contain a number? As in pb1 pb2?

I can't find in the documentation where is says variables can not contain numbers. I'm wondering if variables such as LED1 PIN2 or PB1 PB2 or valid?

Or are variables limited to a-z A-Z?

Thanks

Yes, can use numbers etc. but there are limitations.

"You can name a variable any word that is not already one of the keywords in Arduino.
Avoid beginning variable names with numeral characters."
Leo..

I thought I remember reading you could. So what's wrong with the following code?

If PBA and PBB work fine. But changing the A to a 1 and the B to a 2 and it doesn't work.
On a whim I tried making them an int, didn't work either.

Docs say these are valid

int inputVariable1;
int inputVariable2 = 0; // both are correct

// Clclone Game
byte PB1 = 0;
byte PB2 = 0;
byte DT = 25;
byte WIN = 20;

void setup() {
Serial.begin(9600); //Initalize serail communicaitons

// Iinitialize pins 2 - 43 as an output:
for (byte thisPin = 2; thisPin < 43; thisPin++) {
pinMode(thisPin, OUTPUT);
Serial.println(thisPin);
}
pinMode (44, INPUT);
pinMode (45, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
PB1 = digitalRead (44);
PB2 = digitalRead (45);

digitalWrite (21, PB1);
digitalWrite (22, PB2);

digitalWrite (42, HIGH); // PB LED

for (byte Led = 2; Led == 40; Led++) {
// turn LED on:
digitalWrite(Led, HIGH);
Serial.println(Led);
delay(DT);
// turn led off:
digitalWrite(Led, LOW);
}

}

+++++++++++++++++++++++++++++++++++++++++++++

CycloneGame3:2: error: expected unqualified-id before numeric constant

byte PB1 = 0;

^

CycloneGame3:3: error: expected unqualified-id before numeric constant

byte PB2 = 0;

^

C:\Users\dougs\AppData\Local\Temp\arduino_modified_sketch_75990\CycloneGame3.ino: In function 'void loop()':

CycloneGame3:22: error: lvalue required as left operand of assignment

PB1 = digitalRead (44);

^

CycloneGame3:23: error: lvalue required as left operand of assignment

PB2 = digitalRead (45);

^

exit status 1
expected unqualified-id before numeric constant

PBx are reserved names.

Are you kidding? This is too funny.

Reserved words I thought would be here

and on this page:

But PB is not on either. Is there a list?

Pb1 and Pb2 are valid.
Leo..

Wawa:
Pb1 and Pb2 are valid.
Leo..

So just PB is not?

Are varibles case sensitive? I take it they are.

so PB is not the same as Pb.

Anything begingin with PB is invald.

Easy enough to try for yourself
Leo..

byte Pb1;
byte pB1;
byte pb1;
byte PB1;

void setup() {
}

void loop() {
}

What is PB reserved for?

Is there a list of reserved words? Can you provide a link?

THank you .

It's not reserved in the way that for() and if() are reserved.

It's defined by the framework that sits underneath Arduino. They are abbreviations that appear in the processor datasheet. PB1 is the name of the 2nd pin on port "B". (PB0 is the name of the first pin. It goes up to PB7 on the 8-bit Arduinos. The 32-bit ones can have PB31.)

It's not easy to find as it will be buried deep in the #include files that you don't normally need to see or understand.

You just need to know that this name has already been used for something else and if you don't understand what that something-else is, it's easiest to choose a different name for your own variables.

In general though, any time that you have numbers in your variable names, you should consider using an array instead.

Thanks this makes a lot of sense. I found the link to the framework. Guess I just managed to hit the landmine in choosing PB.

Learning loops right now and sstill fumbling through some basics. Arrays will be next.

I posted the code I'm working on in another post. I'm stuck right now on switching from delay to milli.

Maybe this would be perfet fit for arrays.

When you do get around to posting code again, read up on the forum stickies - using tags - so the listing is correctly displayed, and not ‘interpreted‘ by the forum text renderer.

A good way to avoid reserved words is to start your variable names with lower-case letters.

...R

Doug101:
Learning loops right now and sstill (sic) fumbling through some basics. Arrays will be next.

Once you progress a little in your coding ability, you'll see that loops and arrays go hand-in-hand. Whenever you have an array Data Structure your will almost always have a loop Control Structure suppling its indices.

Robin2:
A good way to avoid reserved words is to start your variable names with lower-case letters.

...R

EXCELLENT SUGGESTION.

What's mising from the Arduino documentation are these helpful suggestions, and more imporatanly examples showing the why.

Another way to avoid reserved words is to use actual words.
Instead of Pb1 why not pushButton1State or startPbState (or whatever PB means)?

Reason for not using a variable name like PushButtonStateforPushButtonNumber1 is that’s a lot of typing every time the Carib Le is referenced. PB1 is short, meaningful and it’s the name used in the schematic and parts list. Seems silly to type PushButtonStateforPushButtonNumber1. Evert time when PB1 involves much less typing.

So type PB1 into the code and use Find and Replace to change it to a more meaningful name throughout the program before compiling it.

UKHeliBob:
So type PB1 into the code and use Find and Replace to change it to a more meaningful name throughout the program before compiling it.

PB1 and PB2 are meaningful. They match the schematic and parts list. Using PB instead of pb or Pb or pB make the code easier to read and by making variables in all CAPs makes it so much easier to read.

Wawa:
Easy enough to try for yourself
Leo..

byte Pb1;

byte pB1;
byte pb1;
byte PB1;

void setup() {
}

void loop() {
}

Yes I could spend time experimenting and maybe I might stumble upon another reserved word or set of characters. Not trying to come off sounding like a jerk, but why would I not want to read the documentation and look at the list of reserved letters and words.

There has to be a list..... right?