Lab2_Sierra


Roberto Sierra

EE47

Lab 2

 

Frankenlight

Part A

1.-

  1. What line(s) of code do you need to change to make the LED blink (like, at all)?

 

  1. What line(s) of code do you need to change to change the rate of blinking?

 

  1. What circuit element would you want to add to protect the board and LED?

2.-

  1. Which lines do you need to modify to correspond with your button and LED pins? 

 

  1. Modify the code or the circuit so that the LED lights only while the button is depressed. Include your code in your lab write-up.

 

const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 9; // the number of the LED pin

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop(){

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, LOW);

}

else {

// turn LED off:

digitalWrite(ledPin, HIGH);

}

}

3.

  1. Which lines of code do you need to modify to correspond with your button and LED pins?

 

  1. How would you change the rate of fading?

 

  1. (Extra) Since the human eye doesn't see increases in brightness linearly how could you change the code to make the light appear to fade linearly?

We can see that the LED has something similar to a linear region after 1.8 volts, so we manage to supply the LED with 1.8 volts as a minimum, then we can have a linearly increase.

In the code, we must modify the range of values in PWM so that we can go from 1.8 volts to 5 volts.

 

Part B

1.-

  1. What is the minimum resistor size that should be used with these LEDs? (Hint: think about your voltage supply and what the diode voltage drop means.)

 

             -5V + 30mA*R + 3.2V = 0

             R = 1.8/30mA = 60 ohms 

2.- Device - Keyboard

  1. Is there computation in your device? Where is it? What do you think is happening inside the "computer?"

 

  1. Are there sensors on your device? How do they work? How is the sensed information conveyed to other portions of the device?

 

  1. How is the device powered? Is there any transformation or regulation of the power? How is that done? What voltages are used throughout the system?

 

  1. Is information stored in your device? Where? How?

 

      e.   Schematic



 

 

4.- My own light

I used the microcontroller and a keyboard to light up my LED. The user can do basic stuff like turning on and off the LED; the commands are “n” for on and “f” for off. The user can interact via serial monitor and can decide the brightness of the LED, just like a dimmer. If the “d” key is pressed, then the user enters to a “Dimmer” menu, where they can increase or decrease the intensity using the Up Arrow and Down Arrow keys. Finally, if the user selects a number, from 1 to 9, the LED will blink at different rates; it blinks faster as the number increases.

 

 

 

 

 

 

 

 

Dimmer function

 

Schematic

 

Code:

 

#include <PS2Keyboard.h>

 

const int data = 8;

const int clk  = 5;

const int led  = 9;

 

PS2Keyboard keyboard;

 

void setup() {

  delay(1000);

  keyboard.begin(data, clk);

  Serial.begin(9600);

  pinMode(led,OUTPUT);

  //Serial.println("Keyboard Test:");

//  Serial.println("n - On");

//  Serial.println("f - Off");

//  Serial.println("d - Dimmer");

//  Serial.println("number - Blink");

}

 

void loop() {

 

  if (keyboard.available()) {

    boolean level;

    boolean blinking;

    char l = 0;

    char b = 0;

    int value = 0;

    char c = keyboard.read();    // read the next key

 

    switch(c){

      case 'n' : digitalWrite(led,HIGH);

                 Serial.println("On");

                 break;

      case 'f' : digitalWrite(led,LOW);

                 Serial.println("Off");

                 break;

      case 'd' : Serial.println("Dimmer");

                 analogWrite(led,0);

                 Serial.println("Page Up to increase - Page Down to decrease");

                 do{

                   do{

                     level = keyboard.available();

                   }while(level != true);

                   l = keyboard.read();

                   if(l == PS2_UPARROW){

                     if(value == 250){ 

                       Serial.println("Max value reached");

                       analogWrite(led,250); 

                       value = 250;

                     }else{

                       value+=10;

                       analogWrite(led,value);

                       Serial.println(value);

                     }  

                   }else if(l == PS2_DOWNARROW){

                     if (value == 0){

                       Serial.println("Min value reached");

                       analogWrite(led,0); 

                       value = 0;

                     }else{

                       value-=10;

                       analogWrite(led,value);

                       Serial.println(value);

                     }

                   }else if(l == PS2_ESC){

                     Serial.println("Goodbye");

                     digitalWrite(led,LOW);

                   }else{

                      Serial.println("Invalid command");

                   }

                 }while(l != PS2_ESC);

                 break;

      case '1' : Serial.println("Level 1");

                 do{

                   digitalWrite(led,HIGH);

                   delay(1000);

                   digitalWrite(led,LOW);

                   delay(1000);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

      case '2' : Serial.println("Level 2");

                 do{

                   digitalWrite(led,HIGH);

                   delay(800);

                   digitalWrite(led,LOW);

                   delay(800);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

      case '3' : Serial.println("Level 3");

                 do{

                   digitalWrite(led,HIGH);

                   delay(600);

                   digitalWrite(led,LOW);

                   delay(600);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

      case '4' : Serial.println("Level 4");

                 do{

                   digitalWrite(led,HIGH);

                   delay(400);

                   digitalWrite(led,LOW);

                   delay(400);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

      case '5' : Serial.println("Level 5");

                 do{

                   digitalWrite(led,HIGH);

                   delay(300);

                   digitalWrite(led,LOW);

                   delay(300);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

 

      case '6' : Serial.println("Level 6");

                 do{

                   digitalWrite(led,HIGH);

                   delay(200);

                   digitalWrite(led,LOW);

                   delay(200);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

 

      case '7' : Serial.println("Level 7");

                 do{

                   digitalWrite(led,HIGH);

                   delay(100);

                   digitalWrite(led,LOW);

                   delay(100);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

 

      case '8' : Serial.println("Level 8");

                 do{

                   digitalWrite(led,HIGH);

                   delay(80);

                   digitalWrite(led,LOW);

                   delay(80);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

 

      case '9' : Serial.println("Level 9");

                 do{

                   digitalWrite(led,HIGH);

                   delay(50);

                   digitalWrite(led,LOW);

                   delay(50);

                   if(blinking = keyboard.available()){

                     b = keyboard.read();

                   }

                 }while(b != PS2_ESC);

                 break;

 

      default  : Serial.println("No function assigned");

                 break;

    }

  }

}