231a-aj lab5

From CSclasswiki
Jump to: navigation, search

Lab 5

Lab partner: Le

Lab 5

Purpose

The purpose of this lab was to interface simple I/O devices to the Arduino

Option 1

We hooked the circuit following the instructions and found the following

It blinks in phase

Option 2

Once again we hooked the computer up the way the instructions told us to and found

"It blinks out of phase"

Connect an LED to Pin 11

The modified sketch looked like the following

  /*Option 3a
  * Lab 5
  */
  int ledPin1 = 13;                // LED connected to digital pin 13
  int ledPin2 = 11;              // LED connected to pin 11
  void setup()                    // run once, when the sketch starts
  {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  }
  void loop()                     // run over and over again
  {
 digitalWrite(ledPin1, HIGH);   // sets the LED on
 digitalWrite(ledPin2, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin1, LOW);    // sets the LED off
 digitalWrite(ledPin2, LOW);    // sets the LED off
 }

This blinks "in phase" is we want out of phase it is the following:

  /*Option 3b
  * Lab 5
  */
  int ledPin1 = 13;                // LED connected to digital pin 13
  int ledPin2 = 11;              // LED connected to pin 11
  void setup()                    // run once, when the sketch starts
  {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  }
  void loop()                     // run over and over again
  {
 digitalWrite(ledPin1, HIGH);   // sets the LED on
 digitalWrite(ledPin2, LOW);   // sets the LED off
 delay(1000);                  // waits for a second
 digitalWrite(ledPin1, LOW);    // sets the LED off
 digitalWrite(ledPin2, HIGH);    // sets the LED on
 }

The different phases would look like this:

  /*Option 3a
  * Lab 5
  */
  int ledPin1 = 13;                // LED connected to digital pin 13
  int ledPin2 = 11;              // LED connected to pin 11
  void setup()                    // run once, when the sketch starts
  {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  }
  void loop()                     // run over and over again
  {
 digitalWrite(ledPin1, HIGH);   // sets the LED on
 digitalWrite(ledPin2, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin1, HIGH);    // sets the LED on
 digitalWrite(ledPin2, LOW);    // sets the LED off
 delay(1000)
 digitalWrite(ledPin1, LOW);   // sets the LED off
 digitalWrite(ledPin2, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin1, LOW);    // sets the LED off
 digitalWrite(ledPin2, LOW);    // sets the LED off
 }

Buzzer

By connecting resistors in series with the buzzer you can lower the voltage across the buzzer to a level the buzzer can handle. We found that connecting two resistors in parallel gave us the best option. We used a 470 Ohm and a 1000 Ohm resitors

Switch

We built a circuit that included a switch that would turn the LED at 11 on and off.

  /*
  *  Switch and LED test program
  */   
   int ledPin = 13; // LED is connected to pin 13
   int switchPin = 11; // switch is connected to pin 11 
   int val; // variable for reading the pin status
   void setup() {
   pinMode(ledPin, OUTPUT);      // Set the LED pin as output
   pinMode(switchPin, INPUT);    // Set the switch pin as input
   }
  void loop(){
  val = digitalRead(switchPin);   // read input value and store it in val
  if (val == LOW) {               // check if the button is pressed
        digitalWrite(ledPin, HIGH);   // turn LED on
   }
  if (val == HIGH) {              // check if the button is not pressed
       digitalWrite(ledPin, LOW);    // turn LED off
  }
  }