{"id":489,"date":"2009-05-26T15:39:45","date_gmt":"2009-05-26T20:39:45","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/?page_id=489"},"modified":"2010-06-22T21:52:51","modified_gmt":"2010-06-23T02:52:51","slug":"variables","status":"publish","type":"page","link":"https:\/\/www.tigoe.com\/pcomp\/code\/code\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"<p><strong>Variables<\/strong> are places in computer memory for storing changing information. You use variables to keep records for your program, such as whether the user has pushed a button or not, how many times she&#8217;s pushed the button, how many times the microcontroller has flashed a light, how much time has passed since the last button push, and so forth.<\/p>\n<p>Think of computer memory as a bunch of coffee cups that you can put a label on the outside and store things on the inside. Variables allow you to put your own names on the outside of the coffee cup and put things you want to remember inside of it. You can then use the stuff inside the cups by referring to them by name in your if statements and loops can then find the coffee cups by name and take different actions based on what&#8217;s there. The real power comes not from the fact that you can place things in variables, but that you can replace them or \u201cvary\u201d them easily.<\/p>\n<p>Before you use a variable in most languages (BASIC, C, Wiring, etc.) you need to give it a name, or <strong>declare<\/strong> your variable. Normally, you do this at the very beginning of your program or at the beginning of a routine. Use a name that describes what you\u2019re using the variable to remember, because it will make your code much more readable. Some people like to add &#8220;Var&#8221; to the ends of variable names, to make them easy to identify when reading the code. You can use any name you want for your variable as long as it does not start with a number, has no spaces, and isn\u2019t a keyword. When you try to run your program, the compiler will let you know if your variable name isn\u2019t allowed.<\/p>\n<p>To store a value in a variable you put the name of the variable on the left side of an equation and the value you want to remember on the right, like so:<\/p>\n<pre>fooVar = 12\r\nsensorVar = 250\r\nswitchVar = 125<\/pre>\n<p>Every variable has a <strong>data type<\/strong>. The data type of a variable determines how much memory the microcontroller needs for the variable, and how it will use the data stored in the variable. You declare both the name and the data type of the variable before you use it, like so:<\/p>\n<p><em>Wiring\/Arduino:<\/em><\/p>\n<pre>char fooVar;\r\nint barVar;\r\nlong timeVar;<\/pre>\n<p><em>PicBasic Pro:<\/em><\/p>\n<pre>byteVar var word\r\nswitchVar var bit\r\nbigVar var word<\/pre>\n<p><em>BX-Basic:<\/em><\/p>\n<pre>dim byteVar as byte\r\ndim bigVar as integer\r\ndim fractionVar as single<\/pre>\n<div>\n<p>Here are the data types for the PIC and BX-24, broken down by the amount of memory each needs:<\/p>\n<p><em>Wiring\/Arduino:<\/em><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\" width=\"483\" align=\"center\">\n<tbody>\n<tr>\n<td><strong>Data Type <\/strong><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Memory needed<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Description<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Range<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Example<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">char<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">8 bits <\/span><\/td>\n<td><span style=\"font-size: xx-small;\">An ASCII character value <\/span><\/td>\n<td><span style=\"font-size: xx-small;\">0 &#8211; 255<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">thisVar = &#8216;A&#8217;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">byte<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">8 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">n positive integer number<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">-128 to 127<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">numVar = 45<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">int<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">16 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">also an integer number. Integer variables can store a greater range of values than Byte variables, however.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">-32 768 to 32 767<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">potVar = 4857<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">long<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">32 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">also an integer number, but a greater range than Byte or Integer variables. Can also be positive or negative.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">-2,147,483,648 to 2,147,483,647<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">starsVar = 65535<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><em>PicBasic Pro:<\/em><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\" width=\"483\" align=\"center\">\n<tbody>\n<tr>\n<td><strong>Data Type <\/strong><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Memory needed<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Description<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Range<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Example<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Bit<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">1 <\/span><\/td>\n<td><span style=\"font-size: xx-small;\">a 1 or 0 value<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">1 or 0<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">thisVar = 1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Byte<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">8 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">a positive integer number<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">0 to 255<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">numVar = 45<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Word<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">16 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">also an integer number. Integer variables can store a greater range of values than Byte variables, however.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">0 to 65,535<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">potVar = 4857<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><em>BX-24:<\/em><\/p>\n<table style=\"height: 386px;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\" width=\"481\" align=\"center\">\n<tbody>\n<tr>\n<td><strong>Data Type <\/strong><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Memory needed<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Description<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Range<\/strong><\/span><\/td>\n<td><span style=\"font-size: xx-small;\"><strong>Example<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Boolean<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">8 bits <\/span><\/td>\n<td><span style=\"font-size: xx-small;\">a true or false value<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">True or False<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">thisVar = true<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Byte<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">8 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">a positive integer number<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">0 to 255<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">numVar = 45<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Integer<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">16 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">also an integer number. Integer variables can store a greater range of values than Byte variables, however, and part of that range can be negative.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">-32 768 to 32 767<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">potVar = 4857<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Long<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">32 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">also an integer number, but a greater range than Byte or Integer variables. Can also be positive or negative.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">-2,147,483,648 to 2,147,483,647<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">starsVar = 65535<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">Single<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">32 bits<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">a floating-point number (i.e. a number with a fractional part, like 2.5, 3.14159, etc.). Note that all floating point numbers must have a decimal, even if they have no fractional part. E.g. 4.0 instead of just 4.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">-3.402,823 E+38 .. 3.402,823 E+38<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">angleVar = 4.5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: xx-small;\">String<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">Varies<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">an array of bytes representing alphanumeric characters.<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">0 to 64 bytes (characters)<\/span><\/td>\n<td><span style=\"font-size: xx-small;\">myStringVar = &#8220;Hello&#8221;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>To understand how variables are stored in memory, it&#8217;s useful to think about what memory is. a computer&#8217;s memory is basically a matrix of switches, laid out in a regular grid, not unlike the switches you see on the back of a lot of electronic gear:<\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tigoe.net\/pcomp\/img\/dip-switches.jpg\" border=\"0\" alt=\"\" width=\"200\" height=\"150\" \/><\/p>\n<\/div>\n<p>Each switch represents the smallest unit of memory, a <strong>bit<\/strong> (usually, we think in terms of bytes when talking about memory. a <strong>byte<\/strong> is simply eight bits). If the switch is on, the bit&#8217;s value is 1. If it&#8217;s off, the value is 0. Each bit has an address in the grid. We can envision a grid that represents that memory like this:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td align=\"center\">bit0<\/td>\n<td align=\"center\">bit1<\/td>\n<td align=\"center\">bit2<\/td>\n<td align=\"center\">bit3<\/td>\n<td align=\"center\">bit4<\/td>\n<td align=\"center\">bit5<\/td>\n<td align=\"center\">bit6<\/td>\n<td align=\"center\">bit7<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">bit8<\/td>\n<td align=\"center\">bit9<\/td>\n<td align=\"center\">bit10<\/td>\n<td align=\"center\">bit11<\/td>\n<td align=\"center\">bit12<\/td>\n<td align=\"center\">bit13<\/td>\n<td align=\"center\">bit14<\/td>\n<td align=\"center\">bit15<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<td align=\"center\">&#8211;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This grid is arranged in rows of 8 bits; each row represents a byte of memory in this illustration.<\/p>\n<div>\n<p>When you declare a variable, the microcontroller picks the next available address and sets aside as many bits are needed for the data type you declare. If you a byte, for example, it sets aside 8 bits. An integer gets 16 bits. A string gets one byte (eight bits) for every character of the string, and a byte to end the string.<\/p>\n<\/div>\n<p>If you made the following variable declarations at the top of your program:<\/p>\n<pre>byte thisVar;\r\nint biggerVar;\r\nbyte anotherVar;\r\nlong reallyBigVar;<\/pre>\n<div>\n<p>The microcontroller might assign memory space for those variables something like this (the bits set aside for each variable are color-coded):<\/p>\n<\/div>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"400\">\n<tbody>\n<tr>\n<td bgcolor=\"#99ffff\">myVar<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">myVar<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"yellow\">integerVar<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">integerVar<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"#99ffff\">anotherVar<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">anotherVar<\/td>\n<\/tr>\n<tr bgcolor=\"#66ff66\">\n<td>reallyBigVar<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr bgcolor=\"#66ff66\">\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr height=\"11\" bgcolor=\"#66ff66\">\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<td height=\"11\">&#8211;<\/td>\n<\/tr>\n<tr bgcolor=\"#66ff66\">\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>reallyBigVar<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>in PicBasic Pro for the PIC microcontroller,you only have bit-, byte-, and word-sized variables, so you your grid might look like this:<\/p>\n<pre>switch1Var var bit\r\nswitch2Var var bit\r\nswitch3Var var bit\r\nthisVar var byte\r\nthatVar var word<\/pre>\n<\/div>\n<div>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"400\">\n<tbody>\n<tr>\n<td bgcolor=\"#99ffff\">switch1Var<\/td>\n<td bgcolor=\"#99ffff\">switch1Var<\/td>\n<td bgcolor=\"#99ffff\">switch2Var<\/td>\n<td bgcolor=\"#99ffff\">switch3Var<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<td bgcolor=\"#99ffff\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"yellow\">thisVar<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">&#8211;<\/td>\n<td bgcolor=\"yellow\">thisVar<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"#3acb4e\">thatVar<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<\/tr>\n<tr>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">&#8211;<\/td>\n<td bgcolor=\"#3acb4e\">thatVar<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p>The space after the bit variables isn&#8217;t filled by the next byte variable. The byte variable starts at the next byte in memory.<\/p>\n<\/div>\n<p>When you refer to the variable, the microcontroller checks to see what&#8217;s in those bits, and gives them to you.<\/p>\n<div>\n<p><strong>So if a bit can be only 0 or 1, how do we get values greater than 1?<\/strong><\/p>\n<p>When you count normally, you count in groups of ten. This is because you have ten fingers. So to represent two groups of ten, you write &#8220;20&#8221;, meaning &#8220;2 tens and 0 ones&#8221;. This counting system is called <em>base ten<\/em>, or <em>decimal notation<\/em>. Each digit place in base ten represents a power of ten: 100 is 10<sup>2<\/sup>, 1000 is 10<sup>3<\/sup>, etc.<\/p>\n<p>Now, imagine you had only two fingers. You might count in groups of two. This is called <em>base two<\/em>, or <em>binary notation.<\/em> So two, for which you write &#8220;2&#8221; in base ten, would be &#8220;10&#8221; in base two, meaning one group of two and 0 ones. Each digit place in base two represents a power of two: 100 is 2<sup>2<\/sup>, or 4 in base ten, 1000 is 2<sup>3<\/sup>, or 8 in base ten, and so forth.<\/p>\n<p>Any number you represent in decimal notation can be converted into binary notation by simply regrouping it in groups of two. Once you&#8217;ve got the number in binary form, you can store it in computer memory, letting each binary digit fill a bit o memory. So if the variable <em>myVar<\/em> from above were equal to 238 (in decimal notation), it would be 11101110 in binary notation. The bits in memory used to store <em>myVar<\/em> would look like this:<\/p>\n<\/div>\n<table style=\"height: 18px;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"445\">\n<tbody>\n<tr>\n<td bgcolor=\"#99ffff\">1<\/td>\n<td bgcolor=\"#99ffff\">1<\/td>\n<td bgcolor=\"#99ffff\">1<\/td>\n<td bgcolor=\"#99ffff\">0<\/td>\n<td bgcolor=\"#99ffff\">1<\/td>\n<td bgcolor=\"#99ffff\">1<\/td>\n<td bgcolor=\"#99ffff\">1<\/td>\n<td bgcolor=\"#99ffff\">0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are three notation systems used most commonly in programming languages to represent numbers: <strong>binary<\/strong> (base two), <strong>decimal<\/strong> (base ten), and <strong>hexadecimal<\/strong> (base sixteen). In hexadecimal notation, the letters A through F represent the decimal numbers 10 through 15. Furthermore, there is a system of notation called ASCII, which stands for American Standard Code for Information Interchange, which represents most alphanumeric characters from the romanized alphabet as number values. More on ASCII can be found in the pages on <a href=\"https:\/\/tigoe.com\/pcomp\/code\/serial-communication\/interpreting-serial-data-bytes\">serial communication<\/a>. For more, see this <a href=\"http:\/\/tigoe.net\/pcomp\/ascii.html\" target=\"_other\">online table<\/a> representing the decimal numbers 0 to 255 in decimal, binary, hexadecimal, and ASCII. While you can work mostly in decimal notation, there are times when it&#8217;s more convenient to represent numbers in ms other than base 10.<\/p>\n<p>Here&#8217;s a chart showing a few number values in the different bases, and the different notation forms for a few programming languages:<\/p>\n<p>Ten:<\/p>\n<div>\n<table style=\"height: 74px;\" border=\"1\" cellspacing=\"0\" width=\"425\" frame=\"void\">\n<colgroup>\n<col width=\"153\"><\/col>\n<col width=\"73\"><\/col>\n<col width=\"103\"><\/col>\n<col width=\"97\"><\/col>\n<\/colgroup>\n<tbody>\n<tr>\n<td width=\"153\" height=\"18\" align=\"left\"><strong>Language<\/strong><\/td>\n<td width=\"73\" align=\"left\"><strong>Decimal<\/strong><\/td>\n<td width=\"103\" align=\"left\"><strong>Hexadecimal<\/strong><\/td>\n<td width=\"97\" align=\"left\"><strong>Binary<\/strong><\/td>\n<\/tr>\n<tr>\n<td height=\"19\" align=\"left\"><em>PicBasic Pro, pBasic<\/em><\/td>\n<td align=\"right\"><code>10<\/code><\/td>\n<td align=\"left\"><code>$A<\/code><\/td>\n<td align=\"left\"><code>%1010<\/code><\/td>\n<\/tr>\n<tr>\n<td height=\"18\" align=\"left\"><em>BX-Basic<\/em><\/td>\n<td align=\"right\"><code>10<\/code><\/td>\n<td align=\"left\"><code>&amp;HA<\/code><\/td>\n<td align=\"left\"><code>bx1010<\/code><\/td>\n<\/tr>\n<tr>\n<td height=\"19\" align=\"left\"><em>Wiring\/Arduino<\/em><\/td>\n<td align=\"right\"><code>10<\/code><\/td>\n<td align=\"left\"><code>0x0A<\/code><\/td>\n<td align=\"left\"><code>B1010<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>One hundred sixty-three:<\/p>\n<table border=\"1\" cellspacing=\"0\" frame=\"void\">\n<colgroup>\n<col width=\"153\"><\/col>\n<col width=\"73\"><\/col>\n<col width=\"103\"><\/col>\n<col width=\"97\"><\/col>\n<\/colgroup>\n<tbody>\n<tr>\n<td height=\"18\" align=\"left\"><strong>Language<\/strong><\/td>\n<td align=\"left\"><strong>Decimal<\/strong><\/td>\n<td align=\"left\"><strong>Hexadecimal<\/strong><\/td>\n<td align=\"left\"><strong>Binary<\/strong><\/td>\n<\/tr>\n<tr>\n<td height=\"19\" align=\"left\"><em>PicBasic Pro, pBasic<\/em><\/td>\n<td align=\"right\"><code>163<\/code><\/td>\n<td align=\"left\"><code>$A3<\/code><\/td>\n<td align=\"left\"><code>%10100011<\/code><\/td>\n<\/tr>\n<tr>\n<td height=\"18\" align=\"left\"><em>BX-Basic<\/em><\/td>\n<td align=\"right\"><code>163<\/code><\/td>\n<td align=\"left\"><code>&amp;HA3<\/code><\/td>\n<td align=\"left\"><code>bx1010_0011<\/code><\/td>\n<\/tr>\n<tr>\n<td height=\"19\" align=\"left\"><em>Wiring\/Arduino<\/em><\/td>\n<td align=\"right\"><code>163<\/code><\/td>\n<td align=\"left\"><code>0x0A3<\/code><\/td>\n<td align=\"left\"><code>B10100011<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong>Variable scope<\/strong><\/p>\n<p>Variables are <strong>local<\/strong> to a particular subroutine if they are declared in that subroutine. Local variables can&#8217;t be used by subroutines outside the one that declares them, and the memory space allotted to them is released and the value lost when the subroutine ends. Variables are <strong>global<\/strong> when they are declared at the beginning of a piece of code, outside all subroutines. Global variables are accessible to all subroutines in a the code, and their value is maintained for the duration of the program. Usually you use global variables for values that will need to be kept in memory for future use by other subroutines, and local variables as a &#8220;scratch pad&#8221; to store values while calculating within a subroutine.<\/p>\n<p>PicBasic Pro\u00a0 and pBasic variables are all global in scope.<\/p>\n<p><strong>Doing Arithmetic With Variables<\/strong><\/p>\n<div>\n<p>There are certain symbols you&#8217;ll need to do math in a program. They&#8217;re pretty much the same symbols as you use in other programming languages:<\/p>\n<table style=\"height: 130px;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"394\">\n<tbody>\n<tr>\n<td><strong>Operation<\/strong><\/td>\n<td><strong>BX-24<\/strong><\/td>\n<td><strong>PBPro<\/strong><\/td>\n<td><strong>Wiring\/Arduino<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Addition<\/td>\n<td>+<\/td>\n<td>+<\/td>\n<td>+<\/td>\n<\/tr>\n<tr>\n<td>Subtraction<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>Multiplication<\/td>\n<td>*<\/td>\n<td>*<\/td>\n<td>*<\/td>\n<\/tr>\n<tr>\n<td>Divide (float)<\/td>\n<td>\/<\/td>\n<td>none<\/td>\n<td>none<\/td>\n<\/tr>\n<tr>\n<td>Divide (integer)<\/td>\n<td>\\<\/td>\n<td>\/<\/td>\n<td>\/<\/td>\n<\/tr>\n<tr>\n<td>Modulus<\/td>\n<td>Mod<\/td>\n<td>\/\/<\/td>\n<td>%<\/td>\n<\/tr>\n<tr>\n<td>Absolute value<\/td>\n<td>Abs<\/td>\n<td>Abs<\/td>\n<td>abs<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div><strong>Data Type Conversions<\/strong><\/div>\n<div><strong><br \/>\n<\/strong><\/div>\n<div>On the PIC, when you&#8217;re adding, subtracting, multiplying, or dividing values stored in variables, you can mix data types, as long as whatever variable holds the result is big enough. So you need to use data types that can fit the results you expect. For example:<\/div>\n<div>\n<pre>sensorVar var byte\r\ncounterVar var byte\r\nbigVar var word\r\n\r\nbigVar = 500\r\nsensorVar = 200\r\ncounterVar = 70<\/pre>\n<pre>sensorVar = sensorVar + counterVar<\/pre>\n<p>The result is 270, which is more than can fit in a byte. What happens in this case is that the variable rolls over. If we put 256 in the variable, it reads as 0, 257, reads as 1, and so forth. Counting this way, 270 would read as 14.<\/p>\n<pre>sensorVar = bigVar - 10<\/pre>\n<p>The result is 490.  This doesn&#8217;t fit in a byte variable, so the result will roll over. The result would read as 235.<\/p>\n<p><strong>BX-Basic Data Type Conversions<br \/>\n<\/strong><\/p>\n<p>In BX-Basic, when you&#8217;re adding, subtracting, multiplying, or dividing values stored in variables, you must make sure that the variables you&#8217;re operating on are of the same type. For example:<\/p>\n<\/div>\n<pre>dim someVar as byte\r\ndim anotherVar as byte\r\ndim smallVar as byte\r\ndim bigVar as integer\r\ndim yetAnotherVar as integer\r\n\r\nSub main()\r\n\tcall delay(0.5)  ' start  program with a half-second delay \r\n\r\n\tsomeVar = anotherVar + smallVar\r\n                     ' allowed, because all three\r\n                     ' variables are data type byte\r\n\r\n\tsomevar = anotherVar * 3\r\n                     ' allowed, because the BX-24\r\n                     ' interprets 3 as a byte\r\n\r\n\tsomeVar = anotherVar + bigVar\r\n                     ' not allowed, because\r\n                     ' bigVar is an integer, while\r\n                     ' anotherVar and someVar are bytes\r\n\r\n\tsomeVar = anotherVar - 568\r\n                     ' not allowed, because 568\r\n                     ' is larger than a byte\r\n\r\n\tyetAnotherVar = bigvar + 568\r\n                     ' allowed, because 568 will fit\r\n                     ' in an integer variable\r\n\r\n\tyetAnotherVar = bigvar * someVar\r\n                     ' not allowed, because someVar\r\n                     ' is a byte and the other two\r\n                     ' variables are integers\r\n\r\nend sub<\/pre>\n<div>\n<p>There are conversion functions to convert data types. For example:<\/p>\n<p><em>BX-Basic:<\/em><\/p>\n<\/div>\n<pre>bigVar = cInt(someVar)\r\n                     ' sets bigVar = an integer\r\n                     ' of equal value to someVar's value\r\n\r\ndebug.print cStr(65)\r\n                     ' converts value to ASCII bytes,\r\n                     ' the character \"6\" and the character \"5\"<\/pre>\n<p>See the BX-24 system library for all the conversion functions.<\/p>\n<h3>Constants<\/h3>\n<p>In addition to variables, every programming language also includes constants, which are simply variables that don\u2019t change. They\u2019re a useful way to label and change numbers that get used repeatedly within your program. For example, imagine you&#8217;re writing a program that runs a servo motor. Servo motors have a minimum and maximum pulse width that doesn\u2019t change, although each servo\u2019s minimum and maximum might be somewhat different. Rather than change every occurrence of the minimum and maximum numbers in the program, we make them constants, so we only have to change the number in one place.<\/p>\n<p>In PicBasic Pro, constants are declared at the beginning of your program, like so:<\/p>\n<pre>MinPulse con 100<\/pre>\n<p>Then you can refer to them in the program just like you do variables, like so:<\/p>\n<pre>PulseWidth = minPulse + angleVar<\/pre>\n<p>In BX Basic, we also have to declare the type of the constant, like so:<\/p>\n<pre>Const minPulse as single = 0.001<\/pre>\n<p>You don\u2019t have to use constants in your programs, but they\u2019re handy to know about, and you will encounter them in other people\u2019s programs.<\/p>\n<p>In Wiring\/Arduino, there are two ways you can declare constants. You can use the const keyword, like so:<\/p>\n<pre>const int LEDpin = 3;\r\nconst int sensorMax = 253;<\/pre>\n<p>Or you can use a function called <strong>define<\/strong>:<\/p>\n<pre>#define LEDpin 3\r\n#define sensorMax 253<\/pre>\n<p>Note that defines are always preceded by a #, and are don&#8217;t have a semicolon at the end of the line. Defines always come at the beginning of the program. They actually work a bit like aliases. What happens is that you define a number as a name, and before compiling, the compiler checks for all occurrences of that name in the program and replaces it with the number. This way, defines don&#8217;t take up any memory, but you get all the convenience of a named constant. There are several defines in the libraries of the Arduino core libraries, so it&#8217;s preferable to use const instead of #define for constants.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables are places in computer memory for storing changing information. You use variables to keep records for your program, such as whether the user has pushed a button or not, how many times she&#8217;s pushed the button, how many times the microcontroller has flashed a light, how much time has passed since the last button &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/code\/variables\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Variables&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":916,"menu_order":12,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-489","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":15,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/489\/revisions"}],"predecessor-version":[{"id":918,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/489\/revisions\/918"}],"up":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/pages\/916"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}