{"id":640,"date":"2014-07-10T11:00:52","date_gmt":"2014-07-10T15:00:52","guid":{"rendered":"https:\/\/itp.nyu.edu\/physicalcomputing\/?page_id=640"},"modified":"2022-06-20T23:07:51","modified_gmt":"2022-06-21T03:07:51","slug":"variables","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/lessons\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"\n<p><em>Adapted from <a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/code\/variables\/ \">Variables<\/a><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This tutorial&nbsp;explains how computer programs organize information in computer memory using variables. All computer programming languages use variables to manage memory, so it&#8217;s useful to understand this no matter what programming language or computer you&#8217;re using. Although the following was written with microcontrollers and physical computing applications in mind, it applies to programming in general.<\/p>\n\n\n\n<p>The programming language examples below use a syntax based on the programming language C. That same syntax is used by many other languages, including Arduino (which is written in C), Java, Processing (which is written in Java), JavaScript, and others.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Is_Computer_Memory_Anyway\"><\/span>What Is Computer Memory, Anyway?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A&nbsp;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 as shown in Figure 1:<\/p>\n\n\n\n<figure class=\"wp-block-image alignnone wp-image-1746 size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"150\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/dip-switches.jpg\" alt=\"Five DIP Switches\" class=\"wp-image-1746\"\/><figcaption>Figure 1.&nbsp;DIP Switches<\/figcaption><\/figure>\n\n\n\n<p>Each switch represents the smallest unit of memory, a <strong>bit.<\/strong> &nbsp;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\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>bit0<\/td><td>bit1<\/td><td>bit2<\/td><td>bit3<\/td><td>bit4<\/td><td>bit5<\/td><td>bit6<\/td><td>bit7<\/td><\/tr><tr><td>bit8<\/td><td>bit9<\/td><td>bit10<\/td><td>bit11<\/td><td>bit12<\/td><td>bit13<\/td><td>bit14<\/td><td>bit15<\/td><\/tr><tr><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><tr><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><tr><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><tr><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td><\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><tr><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><tr><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><\/tbody><\/table><figcaption>Table 1. All data that&#8217;s stored in computer memory is stored in these arrays of bits.<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>So if a bit can be only 0 or 1, how do we get values greater than 1?<\/strong><\/p>\n\n\n\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 <strong>base ten,<\/strong> or <strong>decimal notation<\/strong>. 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\n\n\n<p>Now, imagine you had only two fingers. You might count in groups of two. This is called <strong>base two<\/strong>, or <strong>binary notation<\/strong><em>.<\/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\n\n\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 of memory. &nbsp;So the number&nbsp;238 in decimal notation would be 11101110 in binary notation. The bits in memory used to store 238&nbsp;would look like this:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td><meta charset=\"utf-8\">2<sup>7<\/sup> (128)<\/td><td><meta charset=\"utf-8\">2<sup>6<\/sup> (64)<\/td><td><meta charset=\"utf-8\">2<sup>5<\/sup> (32)<\/td><td><meta charset=\"utf-8\">2<sup>4<\/sup> (16)<\/td><td><meta charset=\"utf-8\">2<sup>3<\/sup> (8)<\/td><td><meta charset=\"utf-8\">2<sup>2<\/sup> (4)<\/td><td><meta charset=\"utf-8\">2<sup>1<\/sup> (2)<\/td><td>2<sup>0<\/sup> (1)<\/td><\/tr><tr><td>1<\/td><td>1<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>1<\/td><td>1<\/td><td>0<\/td><\/tr><\/tbody><\/table><figcaption>Table 2. The bits of the decimal value 238. 238 = <meta charset=\"utf-8\">2<sup>7<\/sup> (128) + <meta charset=\"utf-8\">26 (64) +<meta charset=\"utf-8\">2<sup>5<\/sup> (32) +<meta charset=\"utf-8\">2<sup>3<\/sup> (8) + <meta charset=\"utf-8\">2<sup>2<\/sup> (4) + <meta charset=\"utf-8\">2<sup>1<\/sup> (2)<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Arranging_Memory_into_Variable_Space\"><\/span>Arranging Memory into Variable Space<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Programming languages organize computer memory by breaking the grid of bits up into smaller chunks and labeling them with names. &nbsp;Those names are called variables, and they refer to a location in the computer&#8217;s memory. When you ask for the value of a variable, you&#8217;re asking what the states of the switches in that location in memory are.<\/p>\n\n\n\n<p>If you think of your program as a set of instructions, then variables are the words that you use to describe what those instructions act upon.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p>&#8221; When the user has pushed the button two times&#8230; &#8220;<\/p>\n\n\n\n<p>For this you need a variable called buttonPushed, and you need to check when it&#8217;s equal to 2:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nif (buttonPushed == 2)\n<\/pre><\/div>\n\n\n<p>When you want to store a piece of something like the number of times a button&#8217;s been pushed&nbsp;in the computer&#8217;s memory, you give it a name and a <strong>data type,<\/strong> which &nbsp;states how much memory you intend to use. You usually give it an initial value as well. This is called <strong>declaring the variable<\/strong>, and it looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nint sensorValue = 234;\nbyte buttonPushed = 15;\nlong timeSinceStart = 10324;\nboolean isOpen = false;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Data_Types\"><\/span>Data Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Every variable has a&nbsp;<strong>data type<\/strong>. The data type of a variable determines how much of the computer&#8217;s memory the variable will occupy. &nbsp;Different programming languages have different data types.&nbsp;The examples above use data types from the C programming language that Arduino uses.<\/p>\n\n\n\n<p>The first one, <tt>int sensorValue<\/tt>, is an integer data type. Ints in C take up 16 bits (32 bits in the 32-bit boards like the Nano 33 IoT), so they can contain 2<sup>16<\/sup> different values (or 2<sub><sup>32<\/sup><\/sub>). Ints can only&nbsp;contain integers, but they can be positive or negative, so the variable sensorValue above could range from&nbsp;-32,768 to 32,767. That&#8217;s a range from -2<sup>15<\/sup> to 2<sup>15<\/sup>, with one bit used to store the plus or minus sign.<\/p>\n\n\n\n<p>The second, <tt>buttonPushed<\/tt>, is a <tt>byte<\/tt> data type. Bytes&nbsp;take up 8 bits, and can therefore store 2<sup>8<\/sup> or 256 different values, from 0 to 255. Bytes in Arduino are <strong>unsigned<\/strong>, meaning that they can only be positive numbers.<\/p>\n\n\n\n<p>The third, <tt>timeSinceStart<\/tt> is a <tt>long int<\/tt>eger type, for storing very large values. In this instance, it might be storing the number of milliseconds since your program started, which can get big very quickly. Long ints are signed, and can range from -2,147,483,648 to 2,147,483,647. That&#8217;s 2<sup>32<\/sup> possible values.<\/p>\n\n\n\n<p>The fourth, <tt>isOpen<\/tt>, is a boolean variable. &nbsp;Booleans can only&nbsp;true or false, and ideally take up just one bit in memory (though most programming languages use a whole byte for convenience).<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>1<\/td><td>1<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>0<\/td><\/tr><tr><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>1<\/td><td>1<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>0<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>0<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>0<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>&#8211;<\/td><\/tr><\/tbody><\/table><figcaption><meta charset=\"utf-8\">Table 3. A number of variables stored in a processor&#8217;s memory as 1s and 0s.<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\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 declare 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\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_About_Fractional_Numbers\"><\/span>What About Fractional Numbers?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The variable types above are all for whole numbers, or integers. But how do you store a number like 3.1415 or 2.7828 or other fractional numbers? &nbsp;These are called <strong>floating-point numbers<\/strong> in the programming world, and they&#8217;re a special type of variable called a <strong>float<\/strong>. &nbsp;In Arduino, floats are actually 32-bit numbers, stored in 4 bytes of memory. A few of those bits are used to store the decimal point position.<\/p>\n\n\n\n<p>Depending on the processor you are using, the data types might be different sizes. Table 4 shows the different sizes for the data types on the Uno, an 8-bit processor, and the Nano 22 IoT, a 32-bit processor.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Data Type<\/th><th>Uno<\/th><th>Nano 33 IoT<\/th><\/tr><\/thead><tbody><tr><td>byte<\/td><td>1 byte<\/td><td>1 byte<\/td><\/tr><tr><td>int<\/td><td>2<meta charset=\"utf-8\"> bytes<\/td><td>4 bytes<\/td><\/tr><tr><td>float<\/td><td>4 bytes<\/td><td>4 bytes<\/td><\/tr><tr><td>char<\/td><td>1 byte<\/td><td>1 byte<\/td><\/tr><tr><td>long<\/td><td>4<meta charset=\"utf-8\"> bytes<\/td><td>4 bytes<\/td><\/tr><tr><td>short<\/td><td>2<meta charset=\"utf-8\"> bytes<\/td><td>2 bytes<\/td><\/tr><tr><td>double<\/td><td>4<meta charset=\"utf-8\"> bytes<\/td><td>8 bytes<\/td><\/tr><tr><td>bool<\/td><td>1<meta charset=\"utf-8\"> byte<\/td><td>1 byte<\/td><\/tr><\/tbody><\/table><figcaption>Table 4. The basic Arduino data types and their relative sizes on the Uno and Nano 33 IoT<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Here&#8217;s a snippet of code to find out a given data type&#8217;s size on an Arduino:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nSerial.print(sizeof(int));\n<\/pre><\/div>\n\n\n<p>Replace int above with byte, bool, double, float, long, or the data type whose size you want to know. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Choosing_the_Right_Data_Type\"><\/span>Choosing the Right Data Type<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>How do you know what data type to choose when declaring variables? It depends on two factors: what you&#8217;re going to&nbsp;use the variables for, and what functions you plan to use on them. First, consider how likely the numbers you might store are likely to be. For example, if you&#8217;re counting button pushes, you&#8217;re unlikely to get more than a few hundred in a few minutes, so an int or a byte might be fine. But for a number that might get large, like counting the number of milliseconds since some past event, the number could get very large, so you might need a long int.<\/p>\n\n\n\n<p>Different built-in functions of a programming language will require different data types as parameters, so when you can, use data types that match the functions you plan to use. &nbsp;For example, if you were using a variable to store the results of Arduino&#8217;s <tt><a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/time\/millis\/\" target=\"_blank\" rel=\"noreferrer noopener\">millis()<\/a><\/tt> function, you should use a long int, because <tt>millis()<\/tt> returns that data type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Doing_Math_With_Variables\"><\/span><strong>Doing Math&nbsp;With Variables<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When you add, subtract, multiply, or divide with variables in a computer program, the results you get depend on the variable types you used. For example, &nbsp;if you ran the function&nbsp;below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nint voltage = 5;\nint divider = 2;\nint newVoltage = voltage \/ divider;\n<\/pre><\/div>\n\n\n<p>You might think that newVoltage = 2.5, right? Wrong. Because you used ints, the fractional part is gone, so the result would be 2. &nbsp;Here&#8217;s another:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nbyte buttonPushes = 254;\nbuttonPushes = buttonPushes + 4;\n<\/pre><\/div>\n\n\n<p>After this, you&#8217;d expect that buttonPushes = 258, right? Wrong again! &nbsp;Because you used a byte, you can&#8217;t store a value larger than 255, so when the result is larger than that, the number rolls over to the lowest possible value again. The result would be 2.<\/p>\n\n\n\n<p>Wait, what?<\/p>\n\n\n\n<p>Look at it this way. The highest value you can store in a byte is 255. Therefore, if you try to store 256, it rolls over to 0. 257 rolls over to 1. And 258 rolls over to 2. So 254 + 4 in a byte variable yields 2. &nbsp;If you used an int instead of a byte, then you&#8217;d get the result you expect (258) because an int can hold values larger than 255.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Numeric_Notation_Systems\"><\/span>Numeric Notation Systems<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\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:\/\/itp.nyu.edu\/physcomp\/lessons\/interpreting-serial-data\/\">serial communication.<\/a> For more, see this <a href=\"http:\/\/www.asciitable.com\">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\n\n\n<p>Table 5 shows a few number values in the different bases, and the different notation forms:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Decimal value<\/strong><\/td><td><strong>Hexadecimal<\/strong><\/td><td><strong>Binary<\/strong><\/td><\/tr><tr><td>&nbsp;3<\/td><td><span style=\"font-family: monospace, serif;\"><span style=\"font-size: 15px; line-height: 24px;\">0x03<\/span><\/span><\/td><td><code>0b11<\/code><\/td><\/tr><tr><td>12<\/td><td><span style=\"font-family: monospace, serif;\"><span style=\"font-size: 15px; line-height: 24px;\">0x0C<\/span><\/span><\/td><td><code>0b1100<\/code><\/td><\/tr><tr><td>45<\/td><td><code>0x2D<\/code><\/td><td><code>0b101101<\/code><\/td><\/tr><tr><td>234<\/td><td><code>0xEA<\/code><\/td><td><code>0b11101010<\/code><\/td><\/tr><tr><td>1000<\/td><td><code>0x3E8<\/code><\/td><td><code>0b1111101000<\/code><\/td><\/tr><\/tbody><\/table><figcaption>Table 5. A few number values in base 10, and their equivalent values in binary (base 2)<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Because the values are all bits in the computer&#8217;s memory, &nbsp;you can use all of these notation systems interchangeably.&nbsp;Here are a few examples:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nif (colorValue == 0xFF); \/\/ check to see if the color value is 255\n\n\/\/ add 5 to 0x90. Result will be 0x95:\nint channelNumber = 5;\nint midiCommand = 0x90 + channelNumber;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Variable_scope\"><\/span><strong>Variable scope<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Variables are <strong>local<\/strong> to a particular function in your code&nbsp;if they are declared in that function. Local variables can&#8217;t be used by functions&nbsp;outside the one that declares them, and the memory space allotted to them is released &nbsp;when the function&nbsp;ends. Variables are <strong>global<\/strong> when they are declared at the beginning of a&nbsp;program, outside all functions. Global variables are accessible to all functions&nbsp;in a the program, 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 functions, and local variables when you know the value won&#8217;t be used outside that function. In general, it&#8217;s better to default to local variables when you can, to manage memory more efficiently. Here&#8217;s a typical example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nint oldButtonPush = 0;  \/\/ global variable\n\nvoid setup() {\n   Serial.begin(9600);\n}\n\nvoid loop() {\n   int buttonPush = digitalRead(3); \/\/ local variable\n   if (buttonPush != lastButtonPush) {\n      \/\/ the button changed. Do something here\n      \/\/ then store the current button push state\n      \/\/ in the global variable for the next time\n      \/\/ through the loop:\n   oldButtonPush = buttonPush;  \n   }\n}\n<\/pre><\/div>\n\n\n<p>In this example, the variable <tt>buttonPush<\/tt> is local to the loop function. You couldn&#8217;t read it in the setup, or any other function. The variable <tt>oldButtonPush<\/tt>, on the other hand, is global, and can be read by any function. In the example above, the local variable is used to read the latest state of a digital input, and then later, the value is put into the global variable so that you can get a new reading and compare it to the old one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Constants\"><\/span>Constants<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In addition to variables, every programming language also includes <strong>constants<\/strong>, 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\n\n\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\n\n\n<p>In C and therefore in Arduino, there are two ways you can declare constants. You can use the const keyword, like so:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nconst int LEDpin = 3;\nconst int sensorMax = 253;\n<\/pre><\/div>\n\n\n<p>Or you can use &nbsp;<strong>define<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n#define LEDPin 3\n#define sensorMax 253\n<\/pre><\/div>\n\n\n<p>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\n\n\n<p>For more on variables in Arduino, see the <a href=\"https:\/\/docs.arduino.cc\/learn\/programming\/variables\" target=\"_blank\" rel=\"noreferrer noopener\">variable reference page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adapted from Variables Introduction This tutorial&nbsp;explains how computer programs organize information in computer memory using variables. All computer programming languages use variables to manage memory, so it&#8217;s useful to understand this no matter what programming language or computer you&#8217;re using. Although the following was written with microcontrollers and physical computing applications in mind, it applies &hellip; <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/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":13,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[11,52,26],"tags":[],"class_list":["post-640","page","type-page","status-publish","hentry","category-code","category-lesson","category-programming"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/640"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=640"}],"version-history":[{"count":28,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/640\/revisions"}],"predecessor-version":[{"id":9790,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/640\/revisions\/9790"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}