231a ak realLab4

From CSclasswiki
Jump to: navigation, search

Main | Lab | Project | Demos | Resources

Lab 3: Experimenting with Arduino and Wiki pages

Lession 1

Completed all the procedures, observation: Green LED on, Yellow LED blinks at frequency 1Hz.

Lesson 2

Modified program

 void loop()                     // run over and over again
 {
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(500);                  // waits for a second
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(500);                  // waits for a second
 }
  • Observation: The LEDs blinks twice as fast.


Exercise 1
Modify the code so that the light is on for 100 msec and off for 900 msec

 void loop()                     // run over and over again
 {
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(100);                  // waits for 100 msec
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(900);                  // waits for 900 msec
 }


Exercise 2
Modify the code so that the light is on for 50 msec and off for 50 msec. What happens?

 void loop()                     // run over and over again
 {
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(50);                  // waits for 50 msec
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(50);                  // waits for 50 msec
 }
  • Observation: The LED blinks extremely fast!


Exercise 3
Modify the code so that the light is on for 10 ms and off for 10 ms. What happens?

 void loop()                     // run over and over again
 {
   digitalWrite(ledPin, HIGH);   // sets the LED on
   delay(10);                  // waits for a second
   digitalWrite(ledPin, LOW);    // sets the LED off
   delay(10);                  // waits for a second
 }
  • Observation: The LED is on all the time.


Now pick up the Arduino and gently wave it back and forth, in a dark room. What happens?

  • Observation: It creates a trail of light.


What do you think is happening here?

  • The yellow LED is blinking at at the frequency of 100Hz, which cannot be tell my human eyes. So we can only see it on all the time. When waving it across dark room, the LED light creates the trail.


Exercise 4
Turn LEDs off.

void loop()                     // run over and over again
{
  digitalWrite(ledPin, LOW);   // sets the LED off
}

Lesson 4

Quiz 1
If your hard disk is 200 Gigabytes, how many bytes is that? Use a calculator with lots of digits!

  • 200 GB = 1024 MB = 1024*1024 KB = 1024*1024*1024 Bytes = 1073741824 Bytes


Hard drive makers are quite sneaky, you'll notice that they define GB as being 1000 MB, and 1 MB = 1000 KB, etc. Given this fact, how many bytes can you really store in your 200GB drive?

  • 200 GB = 1000 *1000*1000 Bytes = 1 billion Bytes


How much less storage do you get thanks to the marketing guy who came up with this trick?

  • delta = 1073741824 - 1000000000 = 73741824 Bytes


Hello world!

 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 */
 
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Hello world!");  // prints hello with ending line break 
}

void loop()                       // run over and over again
{
                                  // do nothing!
}
  • Note that must press reset on the Arduino to print out "Hello world!"


Quiz 2
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?

  • 12 byte = 12 * 8 bits = 96 bits, t = 96 / 9600 sec = 1/100 sec


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?

  • 12 byte = 12 * 8 bits = 96 bits, t = 96 / 19200 = 1/200 sec


Print Hello over and over again

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop()                       // run over and over again
{
   Serial.println("Hello world!");  // prints hello with ending line break  
}