231a aaLabs

From CSclasswiki
Jump to: navigation, search

Julia Burch's Labs

Lab 4

Lesson One

Blink

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for 1/10 of a second,
 * then off for 9/10 of a 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(100);                  // waits for 1/10 of a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(900);                  // waits for 9/10 of a second
}

Lesson Two

Blink Two

  • Exercise 1 Modify the code so that the light is on for 100 msec and off for 900 msec.
  • Exercise 2 Modify the code so that the light is on for 50 msec and off for 50 msec. What happens? 'The light begins to blink very rapidly.'
  • Exercise 3 Modify the code so that the light is on for 10 ms and off for 10 ms. What happens? 'The light does not appear to blink.'
  • Exercise 4 Now pick up the Arduino and gently wave it back and forth, in a dark room. What happens? 'The light trails dashes through the air as you wave the Arduino back and forth.'
  • Exercise 5 What do you think is happening here? 'This occurs because the Arduino is blinking, it is just occurring too quickly for our eyes to perceive.'

Lesson Four

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
}

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

Have Some Math!

/*
 * 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
{
}