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:50]
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 425: 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 506: 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 514: 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 611: 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 628: 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 669: 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 696: Line 710:
  
 Here's one possible solution: Here's one possible solution:
- +<class white> 
-<​code ​C>/*+<​code>/​*
  * Drive size calculator!  * Drive size calculator!
  */  */
Line 746: Line 760:
 } }
 </​code>​ </​code>​
 +</​class>​
  
    
/home/ladyada/public_html/wiki/data/attic/tutorials/learn/arduino/lesson4.html.1287676225.txt.gz · Last modified: 2016/01/28 18:05 (external edit)