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

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Lab 2 Writeup - Dan Robinson

Page history last edited by Dan Robinson 13 years ago

1. a) To changw the rate of blinking, change the arguments to the delay function. (If we want the "on" time and the "off" times to be equal, these need to stay the same. Otherwise, we can encode whatever on-off ratio we want.)

   b) We need a resistor in series at some point either between the pin and the LED high pin or between the LED low pin and ground.

 

2. a) We need to change the LED pin used from 13 to 9 (i.e. change the 13 in line 30 to 9).

   b) All we need to change is the "if" statement on line 48 to "if (buttonState == LOW) {". My code is below.

 

// constants won't change. They're used here to
// set pin numbers:
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 LOW:
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH); 
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

 

 

3. a) No modifications are needed to run the given code, but we could incorporate a button in the exact same way as above if we'd like (i.e. set the LED to the value we were going to set it to if the buttonState is LOW, 0 otherwise, for example).

   b) We could change the rate of fading by changing the delay between iterations of the for loop or the increment in the for loop, or both.

   c) I threw together a simple sine wave fade; the code is below. 

 

/*
  Dan's sine wave LED fade. Woot!
 */

#define PI 3.14159265

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

void setup()  {
  pinMode(buttonPin, INPUT);
}

void loop()  {
  double val = 0;
  while (val < 2*PI) {
    analogWrite(ledPin, 255*(-cos(val)+1)/2);
    delay(6);                           
    val += 0.01;
  }

 

I wanted to incorporate the button, so I threw together the following. The concept is simple; when the button is pressed, the LED starts through its cycle and continues as long as the button is held. When the button is released, the LED is off.

 

/*
  Dan's sine wave LED fade with button. Woot!
 */

#define PI 3.14159265

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

 

void setup()  {
  pinMode(buttonPin, INPUT);
}

void loop()  {
  double val = 0;
  while (val < 2*PI && digitalRead(buttonPin) == LOW) {
    analogWrite(ledPin, 255*(-cos(val)+1)/2);
    delay(6);                           
    val += 0.01;
  }
  analogWrite(ledPin, 0);
}

 

 

 

Part 2

 

1. a) Our minimum resistence should be (5V - 3.4V) / 20mA = 80 ohms.

 

My product is a small wobbling pig. A plastic pig stands on a base with a solar panel. When the panel is illuminated, the pig wobbles back and forth. My circuit is fairly simple; a small solar panel provides around 0.7V (as measured in lab) and is in series with a capacitor. The panel-capacitor series is attached to two ends of a tiny, long copper wire, which is coiled what looks like thousands of times.

2. a) No; the device is purely analog.

   b) The only sensor is the solar panel. When illuminated, the current through the wire causes a magnetic field that moves a magnet on the bottom of a pendulum which is attached to the body of the pig.

   c) The only power comes from the panel. The capacitor regulates this power source; I believe it evens out spikiness in the voltage supplied, but my E40 knowledge is rusty.

   d) Information is stored in the momentum of the pig and in the charge of the capacitor, but mostly in the former.

 

The voltage provided by the solar panel is too small to power an LED, so I wired the panel into the voltage step circuit provided in our kits. I measured the output voltage of this combination to be about 1.1V, still too small, but two of these in series could be easily used to power an LED (i.e. by removing the leads to the copper coil and replacing them with an LED).

Comments (2)

Wendy Ju said

at 2:12 pm on Apr 18, 2011

Howdy! Can you post photos/videos of your Frankenlite?

redswood@... said

at 3:56 pm on Apr 23, 2011

Great work!
It is better to upload a photo or video in future labs.
I am anxious to watch the video demo of the LED with sine wave fade.
For the extra question:
This Lab used an 8-bit (from 0 to 255) Pulse-Width Modulation (PWM) to change the duty cycle of an LED, via one of the digital pins (of an Arduino) that support PWM output.
The example code that comes with the Arduino to fade an LED is as follows:
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
This code linearly changes the percentage of the duty cycle. However, humans perceive brightness not linearly (but approximately logarithmically) to the percentage of the duty cycle.
To have the LED have a linear fading effect, we can either use a lookup table that has been tested to give a linear perception, or use an exponential function to approximate that lookup table

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