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:40]
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 212: Line 213:
 \\ \\
 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!  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>+</class>
  
-==== 10 PRINT HELLO \\ 20 GOTO 10  ====+==== 10 PRINT HELLO ==== 
 +==== 20 GOTO 10  ====
  
  
Line 221: 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 231: 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 398: 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 421: 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 502: 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 510: 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 607: 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 624: 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 665: 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 692: Line 710:
  
 Here's one possible solution: Here's one possible solution:
- +<class white> 
-<​code ​C>/*+<​code>/​*
  * Drive size calculator!  * Drive size calculator!
  */  */
Line 742: Line 760:
 } }
 </​code>​ </​code>​
 +</​class>​
  
    
/home/ladyada/public_html/wiki/data/attic/tutorials/learn/arduino/lesson4.html.1287675638.txt.gz · Last modified: 2016/01/28 18:05 (external edit)