calisir_tolgaferdi_lab2


PART 1

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

 

Since the LED was blinking the moment I connected it to the PC, I don't need to change any code.

 

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

The rate of blinking is determined by the delay function in the loop. For a faster blinking time I would put in a shorter delay value thus have shorter intervals between blinks. And for a slower blinking rate a larger delay value.

 

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

 

Adding resistor would ensure that I don't exceed the maximum voltage for the LED and board.

 

PART 2

 

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

I have to switch the HIGH and the LOW in "digitalWrite(ledPin, LOW)" and "digitalWrite(ledPin, HIGH)"

I also have to make sure that I'm running through pin 9 in my software 

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

  }

}

 

PART 3

 

a) Which line(s) of code do you need to modify to correspond with your LED pin?

 

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

defines the digital pin to which the LED is connected to, so I have to change it to 9.

 

b) How would you change the rate of fading?

I'd modify delay(30), at the end of both the fade in and fade out in the loop. A larger value in () would give me a slower rate of fading and a smaller value in () would result in more frequent fading.

 

c) (Extra) Since the human eye doesn't see increases in brightness linearly and the diode brightness is also nonlinear with voltage, how could you change the code to make the light appear to fade linearly?

 

As a result of the fact that the human eye doesn't see increases in brightness linearly, the bright period appears to be longer in time. To correct this I could set the delay time to be shorter for the fade in so that the bright period would appear to be shorter as it is in reality. Moreover I could code it so that the rate of increase in brightness could speed us as we approach the maximum bright which would also correct the perception of a lengthened bright period.  

Part c? please add video to get extra points