User Tools

Site Tools


tutorials:learn:arduino:lesson4.html

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorials:learn:arduino:lesson4.html [2010/10/21 15:34]
daigo
tutorials:learn:arduino:lesson4.html [2016/01/28 18:05] (current)
Line 34: Line 34:
 Information is passed back & forth between the computer and Arduino by, essentially,​ setting a pin high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it. It's a little like [[http://​en.wikipedia.org/​wiki/​Morse_code|Morse code]], where you can use //dits// and //dahs// to send messages by [[http://​en.wikipedia.org/​wiki/​Telegram|telegram.]] In this case, instead of a long cable, its only a few feet.  Information is passed back & forth between the computer and Arduino by, essentially,​ setting a pin high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it. It's a little like [[http://​en.wikipedia.org/​wiki/​Morse_code|Morse code]], where you can use //dits// and //dahs// to send messages by [[http://​en.wikipedia.org/​wiki/​Telegram|telegram.]] In this case, instead of a long cable, its only a few feet. 
  
-{{  http://​www.ladyada.net/​images/​arduino/​serialdata.gif?​nolink&​581x298 ​ |}} \\ //This is as good as Microsoft Visio can do, yay!//+{{  http://​www.ladyada.net/​images/​arduino/​serialdata.gif?​nolink&​581x298 ​ |}}  
 +<class center>//This is as good as Microsoft Visio can do, yay!//</​class>​
  
 (Now, people who are all geeked-out will probably get angry at this point because I'm simplifying things. Well guess what, its an Arduino tutorial, not a OSI Physical Network Architecture tutorial.) (Now, people who are all geeked-out will probably get angry at this point because I'm simplifying things. Well guess what, its an Arduino tutorial, not a OSI Physical Network Architecture tutorial.)
Line 119: Line 120:
 OK first thing to notice is that there'​s nothing in the **loop** procedure! We've gutted it...and put some stuff into the **setup** procedure. OK first thing to notice is that there'​s nothing in the **loop** procedure! We've gutted it...and put some stuff into the **setup** procedure.
  
 +<class warning>
 Even if we have nothing in the **setup **or **loop** procedures, the Arduino requires them to be there. That way it knows you really mean to do nothing, as opposed to forgetting to include them! Even if we have nothing in the **setup **or **loop** procedures, the Arduino requires them to be there. That way it knows you really mean to do nothing, as opposed to forgetting to include them!
 +</​class>​
 The first line of code in the **setup** procedure is this one:  The first line of code in the **setup** procedure is this one: 
  
Line 151: Line 153:
  
  
-  ***If the Arduino transfers data at 9600 bits per second and you're sending 12 bytes of data, how long does it take to send over this information? ​\\ **//​Highlight the text below for the answer//**         \\ **12 bytes of data equals 12 * 8 = 96 bits of data. If we can transfer 9600 bits per second, then 96 bits takes 1/100th of a second!  +**If the Arduino transfers data at 9600 bits per second and you're sending 12 bytes of data, how long does it take to send over this information?​** ​\\ //Highlight the text below for the answer// ​        ​\\ ​ 
-  ***If the Arduino transfers data at 19200 bits per second (19200 baud) and you're sending 12 bytes of data, how long does it take to send over this information?​ \\ **//Highlight the text below for the answer//**  ​\\ ​**This is twice as fast as before, so it will take half the time, about 1/200th of a second.+<class white>12 bytes of data equals 12 * 8 = 96 bits of data. If we can transfer 9600 bits per second, then 96 bits takes 1/100th of a second! ​</​class>​ 
 +**If the Arduino transfers data at 19200 bits per second (19200 baud) and you're sending 12 bytes of data, how long does it take to send over this information?​** \\ //Highlight the text below for the answer// ​ \\ 
 +<class white>This is twice as fast as before, so it will take half the time, about 1/200th of a second.</​class>​
  
 Good, now compile the sketch and upload it to your Arduino.... ​ Good, now compile the sketch and upload it to your Arduino.... ​
Line 178: Line 182:
  
 ^ ^Windows^Mac OS X^Linux^ ^ ^Windows^Mac OS X^Linux^
-^NG^|Arduino does not reset.|Arduino does not reset.|Arduino does not reset.| +^NG|Arduino does not reset.|Arduino does not reset.|Arduino does not reset.| 
-^Diecimila^|Arduino resets, starts the sketch a few seconds later |Arduino resets, starts the sketch a few seconds later |Arduino resets, starts the sketch a few seconds later |+^Diecimila|Arduino resets, starts the sketch a few seconds later |Arduino resets, starts the sketch a few seconds later |Arduino resets, starts the sketch a few seconds later |
  
  
Line 192: Line 196:
 Voila! It is our sketch! ​ Voila! It is our sketch! ​
  
-**Baud rate match up!**If you ever find that you're getting a whole lot of gibberish instead of proper text, make sure that you have the correct baud rate selected in the drop down menu of the Serial Monitor. Note that this communication baud rate is indepedent of the upload process, which is fixed at 19200 bps.+<class warning>​ 
 +**Baud rate match up!** \\ 
 +If you ever find that you're getting a whole lot of gibberish instead of proper text, make sure that you have the correct baud rate selected in the drop down menu of the Serial Monitor. Note that this communication baud rate is indepedent of the upload process, which is fixed at 19200 bps. 
 +</​class>​
  
 Next, try pressing the reset button a few times to make more Hello Worlds! appear. If you have an NG, this may be a bit annoying but do it anyways. Next, try pressing the reset button a few times to make more Hello Worlds! appear. If you have an NG, this may be a bit annoying but do it anyways.
Line 202: Line 209:
 Each time you reset the Arduino, it performs the **setup** procedure, and prints out Hello again. If you look closely at the Arduino, you will also see the little **TX** LED blink just as it prints out this message. That's your indication that data was sent.  Each time you reset the Arduino, it performs the **setup** procedure, and prints out Hello again. If you look closely at the Arduino, you will also see the little **TX** LED blink just as it prints out this message. That's your indication that data was sent. 
  
-**What'​s Send do? **When you **println** you are sending data from the Arduino to the computer. The Send button (and the text input next to it) are used to send data **to** the Arduino. We aren't going to be using it in this lesson so don't be surprised that it doesn'​t do anything when you click it! +<class note> 
 +**What'​s Send do? ** 
 +\\ 
 +When you **println** you are sending data from the Arduino to the computer. The Send button (and the text input next to it) are used to send data **to** the Arduino. We aren't going to be using it in this lesson so don't be surprised that it doesn'​t do anything when you click it!  
 +</​class>​
  
- +==== 10 PRINT HELLO ==== 
-==== 10 PRINT HELLO \\ 20 GOTO 10  ====+==== 20 GOTO 10  ====
  
  
Line 212: Line 223:
 **Quick quiz!** **Quick quiz!**
  
- +**What simple modification should we perform to make the Arduino print Hello World over and over again?**  \\ //Highlight the text below for the answer// ​        ​\\ ​ 
-  ***What simple modification should we perform to make the Arduino print Hello World over and over again? ​ \\ **//Highlight the text below for the answer// ​        ** \\ **Simply move the** Serial.println("​Hello world!"​);​** statement from the **setup** procedure to the** loop **procedure. ​+<class white>Simply move the** Serial.println("​Hello world!"​);​** statement from the **setup** procedure to the** loop **procedure. ​</​class>​
  
 Perform this modification and then compile and upload the new hyper-hello sketch. Then start up the serial monitor. You will see Hello World! scroll by super fast! Perform this modification and then compile and upload the new hyper-hello sketch. Then start up the serial monitor. You will see Hello World! scroll by super fast!
Line 222: Line 233:
  
  
-  ***Whats going on with the TX LED?  \\ **//Highlight the text below for the answer// ​** \\ **It's lit, not blinking +**Whats going on with the TX LED?  ​**\\ //Highlight the text below for the answer// ​ \\  
-  ***Try waving the Arduino around in a dark room, what do you see?  ​\\ **//​Highlight the text below for the answer \\ //There are little dotted light trails  +<class white>It's lit, not blinking</​class>​ 
-  ***What'​s going on here? Hint: Remember [[http://​www.ladyada.net/​learn/​arduino/​lesson2.html|lesson 2]]?  ​\\ **//​Highlight the text below for the answer// ​ \\ The data is being transmitted so fast, that we can't see the TX LED blinking...it'​s sending data many times a second! ​+**Try waving the Arduino around in a dark room, what do you see?  **\\ //Highlight the text below for the answer \\ // 
 +<class white>There are little dotted light trails ​</​class>​ 
 +**What'​s going on here? Hint: Remember [[http://​www.ladyada.net/​learn/​arduino/​lesson2.html|lesson 2]]?  **\\ //Highlight the text below for the answer// ​ \\  
 +<class white>The data is being transmitted so fast, that we can't see the TX LED blinking...it'​s sending data many times a second! ​</​class>​
  
 Make the Arduino chill out a little by adding a one second delay to the sketch, so that it only prints out Hello World once a second. Make the Arduino chill out a little by adding a one second delay to the sketch, so that it only prints out Hello World once a second.
Line 389: Line 403:
 </​code>​ It turns out that this is totally OK, it just means that we don't know what **h **is going to store yet, because we're going to calculate it later. Since it's not assigned to a value upon creation, the Arduino just creates the box, the stuff inside is whatever was in left over in memory. ​ </​code>​ It turns out that this is totally OK, it just means that we don't know what **h **is going to store yet, because we're going to calculate it later. Since it's not assigned to a value upon creation, the Arduino just creates the box, the stuff inside is whatever was in left over in memory. ​
  
 +<class warning>
 **Default values** **Default values**
- 
- 
  
 If you don't assign a value to a variable, it could be any value. Make sure you don't try to use the variable before you assign it a value! If you don't assign a value to a variable, it could be any value. Make sure you don't try to use the variable before you assign it a value!
 +</​class>​
  
 Later on in the sketch, we assign it the value. ​ Later on in the sketch, we assign it the value. ​
Line 412: Line 426:
  
  
-  ***Lets say you have a variable "​foo"​ which contains a number. You'd like to find the square root of the square root of this number. What line of code would print out this value?** \\ //Highlight the text below for the answer// \\ Serial.println( sqrt( sqrt(foo) ); \\ First take the square root of **foo**, then take the square root of that and then use that value as the input to **println**+**Lets say you have a variable "​foo"​ which contains a number. You'd like to find the square root of the square root of this number. What line of code would print out this value?** \\ //Highlight the text below for the answer// \\  
 +<class white>Serial.println( sqrt( sqrt(foo) ); \\ First take the square root of **foo**, then take the square root of that and then use that value as the input to **println** ​</​class>​
  
 **Now its your turn to create a calculator**. ​ \\ You'll create a **Ohm'​s law** calculator. Ohm's law says that **Voltage = Current * Resistance**. (This is a pretty useful law which forms the basis of electronics,​ and we'll study it in depth more later.) Starting with two variables, **i** for current and **r **for resistance, have it print out the amount of voltage that can be measured accross the resistor. **Now its your turn to create a calculator**. ​ \\ You'll create a **Ohm'​s law** calculator. Ohm's law says that **Voltage = Current * Resistance**. (This is a pretty useful law which forms the basis of electronics,​ and we'll study it in depth more later.) Starting with two variables, **i** for current and **r **for resistance, have it print out the amount of voltage that can be measured accross the resistor.
Line 493: Line 508:
 Keeping that in mind, remember in [[http://​www.ladyada.net/​learn/​arduino/​lesson2.html|lesson 2]] we said that when we define a variable we also define the **box-type** of the variable? The box is where we store the data, in this case the **type** is **int**. It turns out that an **int** type can store only 2 bytes. ​ Keeping that in mind, remember in [[http://​www.ladyada.net/​learn/​arduino/​lesson2.html|lesson 2]] we said that when we define a variable we also define the **box-type** of the variable? The box is where we store the data, in this case the **type** is **int**. It turns out that an **int** type can store only 2 bytes. ​
  
-**Quick quiz!** \\ **How many bits are in 2 bytes?** \\ //Highlight the text below for the answer// ​ \\ There are 8 bits in 1 byte so 2 bytes is 16 bits+**Quick quiz!** \\  
 + 
 +**How many bits are in 2 bytes?** \\ //Highlight the text below for the answer// ​ \\  
 +<class white>There are 8 bits in 1 byte so 2 bytes is 16 bits</​class>​
  
 To figure out how big a number we can store in a 2 byte-sized box use a calculator and take 2 to the power of the number of bits (since each bit can store 2 values, 0 or 1). Then we subtract 1 because like in the car odometer, you can't actually display the final value, 10000. So, in this case the largest number is 2<​sup>​16</​sup>​ - 1 = 65535. Since the number we're trying to store (102400) is larger than that, we see that "​rollover."​ To figure out how big a number we can store in a 2 byte-sized box use a calculator and take 2 to the power of the number of bits (since each bit can store 2 values, 0 or 1). Then we subtract 1 because like in the car odometer, you can't actually display the final value, 10000. So, in this case the largest number is 2<​sup>​16</​sup>​ - 1 = 65535. Since the number we're trying to store (102400) is larger than that, we see that "​rollover."​
Line 501: Line 519:
  
 ^Type^Size (bits)^Size (bytes)^ ^Type^Size (bits)^Size (bytes)^
-^byte^|8|1| +^byte|8|1| 
-^int^|16|2| +^int|16|2| 
-^long^|32|4|+^long|32|4| 
 It looks like we want to use the **long** type. So lets make that change It looks like we want to use the **long** type. So lets make that change
  
Line 598: Line 617:
  
 ^Type^Size (bits)^Size (bytes)^Minimum Value^Maximum Value^ ^Type^Size (bits)^Size (bytes)^Minimum Value^Maximum Value^
-^unsigned byte^|8|1|0|255| +^unsigned byte|8|1|0|255| 
-^byte ^|8|1|-128|127| +^byte |8|1|-128|127| 
-^unsigned int ^|16|2|0|65535| +^unsigned int |16|2|0|65535| 
-^int^|16|2|-32768|32767| +^int|16|2|-32768|32767| 
-^unsigned long ^|32|4|0|4294967295| +^unsigned long |32|4|0|4294967295| 
-^long^|32|4|-2147483648|2147483647|+^long|32|4|-2147483648|2147483647| 
 + 
 **Quick quiz!** **Quick quiz!**
  
  
-  ***Lets say you have a program that stores the age of a human in years (which so far is no more than 122), whats a good data type to use?** \\ Highlight the text below for the answer ​        \\ You probably want to use a **byte **type +**Lets say you have a program that stores the age of a human in years (which so far is no more than 122), whats a good data type to use?** \\ Highlight the text below for the answer ​        ​\\ ​ 
-  *** Lets say you want to store the age of a human in seconds, what is an appropriate data type now? \\ **Highlight the text below for the answer ​      \\ 110 years = 3468960000 seconds. You'll need to store this in an **unsigned long** variable.+<class white>You probably want to use a **byte **type</​class>​ 
 +** Lets say you want to store the age of a human in seconds, what is an appropriate data type now? **\\ Highlight the text below for the answer ​      ​\\ ​ 
 +<class white>110 years = 3468960000 seconds. You'll need to store this in an **unsigned long** variable.</​class>​
  
 +<class note>
 **Why Types? ** **Why Types? **
  
Line 615: Line 639:
  
 Well, on your desktop computer, with gigabytes of memory (RAM), this is a reasonable thing to do. However, the tiny tiny computer in the Arduino has a grand total of 1 Kilobyte of memory. And some of that is used for background stuff you don't see. For small sketches sure you can make everything a **long** and be done with it, but if you have a bigger sketch, you'll run out of memory really fast and then you'll have major problems. So in this case, every byte counts! ​ Well, on your desktop computer, with gigabytes of memory (RAM), this is a reasonable thing to do. However, the tiny tiny computer in the Arduino has a grand total of 1 Kilobyte of memory. And some of that is used for background stuff you don't see. For small sketches sure you can make everything a **long** and be done with it, but if you have a bigger sketch, you'll run out of memory really fast and then you'll have major problems. So in this case, every byte counts! ​
 +</​class>​
  
  
Line 656: Line 680:
  
  
-  ***Let'​s say we have a variable that is byte type, it's signed by default. It starts out with the value 127 and we add one to the variable, what will the new variable value be?** \\ Highlight the text below for the answer ​          \\ It's a signed variable so it will roll over to -128 +**Let'​s say we have a variable that is byte type, it's signed by default. It starts out with the value 127 and we add one to the variable, what will the new variable value be?** \\ Highlight the text below for the answer ​          ​\\ ​ 
-  ***Let'​s say now it is an unsigned byte type, what happens now?** \\ Highlight the text below for the answer ​          \\ Since it is unsigned, it can store much more data, so it will be able to hold the value 128.  +<class white>It's a signed variable so it will roll over to -128</​class>​ 
-  ***If we have an unsigned byte and it starts out with the value 250 and we add 10, what will the value be? \\ **//​Highlight the text below for the answer// ​        \\ Even though thie variable can store a lot, it can't store more than the number 255, so it will rollover and we'll end up with the number 4.+**Let'​s say now it is an unsigned byte type, what happens now?** \\ Highlight the text below for the answer ​          ​\\ ​ 
 +<class white>Since it is unsigned, it can store much more data, so it will be able to hold the value 128. </​class>​ 
 +**If we have an unsigned byte and it starts out with the value 250 and we add 10, what will the value be? \\ **//​Highlight the text below for the answer// ​        \\ 
 +<class white> ​Even though thie variable can store a lot, it can't store more than the number 255, so it will rollover and we'll end up with the number 4.</​class>​
  
 Now it's your turn! Now it's your turn!
Line 683: Line 710:
  
 Here's one possible solution: Here's one possible solution:
- +<class white> 
-<​code ​C>/*+<​code>/​*
  * Drive size calculator!  * Drive size calculator!
  */  */
Line 733: Line 760:
 } }
 </​code>​ </​code>​
 +</​class>​
  
    
/home/ladyada/public_html/wiki/data/attic/tutorials/learn/arduino/lesson4.html.1287675252.txt.gz · Last modified: 2016/01/28 18:05 (external edit)