| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Social distancing? Try a better way to work remotely on your online files. Dokkio, a new product from PBworks, can help your team find, organize, and collaborate on your Drive, Gmail, Dropbox, Box, and Slack files. Sign up for free.

View
 

Lab2_Sierra

Page history last edited by Roberto Sierra 8 years, 11 months ago

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)?

  • The “digitalwrite” line. If we want to turn it on, we should write “LOW” and if we want to turn it off, then we must type “HIGH”.

 

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

  • The “delay” instruction. The number inside the parenthesis indicates how fast will the LED blink. They are milliseconds.

 

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

  • A resistor to limit current. If we’re using a red LED, a 220-ohm resistor will work.

2.-

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

  • The variable declaration part. In our case we need pin 2 and pin 9.

 

  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?

  • The variable declaration; “int ledpin = 9”.

 

  1. How would you change the rate of fading?

  • By changing the delay value.

 

  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?

  • If we consider that a diode doesn’t have a linear voltage-current relation, we can see this graph to see if there’s a linear region that can help us to achieve this. From the datasheet provided we can see the following graph:

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.)

  • As the voltage in the LED is 3.2 V and the maximum current is 30mA, using KVL:

 

             -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?"

  • No, there’s no computational device in my keyboard since it has an old PS2 connector.

 

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

  • Yes, it has some lines that conduct a signal when one of the keys is pressed. Each key has a train of pulses that is only for each key and that info is transmitted to the computed and interpreted by it.

 

  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?

  • The device is powered with voltage from the computer. There are no transformers or regulation and it uses 5V.

 

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

  • There’s no information stored.

 

      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;

    }

  }

}

 

 

 

Comments (1)

Benjamin Tee said

at 10:52 am on Jul 24, 2011

Nice work! Great job with the hack!

You don't have permission to comment on this page.