231a-ac Lab4

From CSclasswiki
Jump to: navigation, search

Lesson 1

I opened the Blink sketch, compiled and uploaded it to Arduino. The on-board LED of Pin13 was blinking as expected.


/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */


int ledPin = 13;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}


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

Lesson 2

I did the following exercises using the same blink sketch.

  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 a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(900);                  // waits for a second
}
  1. 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 a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(50);                  // waits for a second
}

The light blinks very fast.

  1. 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
}

The light is no longer blinking

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

The LED creates a dashed trail of light since the LED is blinking too fast that our eyes couldn't notice.

Lesson 4

Hello World (print once)

/*
 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 *
 * This sketch prints "Hello world" to screen
 */


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!
}

Hello World (print every second)

/*
 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 *
 * This sketch prints "Hello world" to screen every second
 */


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
  delay(1000);
}

Math sketch

/*
 * Math is fun!
 */

int a = 5;
int b = 10;
int c = 20;

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

  Serial.println("Here is some math: ");

  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.print("c = ");
  Serial.println(c);

  Serial.print("a + b = ");       // add
  Serial.println(a + b);

  Serial.print("a * c = ");       // multiply
  Serial.println(a * c);
  
  Serial.print("c / b = ");       // divide
  Serial.println(c / b);
  
  Serial.print("b - c = ");       // subtract
  Serial.println(b - c);
}

void loop()                     // we need this to be here even though its empty
{
}