Wednesday, October 14, 2009

Exercise_3

video

int ledPin = 9; // declare Pin 9
int analogPin = 2; // Analog pin 2

void setup(){} // no setup needed

void loop() //Function read the analog value and set a delay time and controls the speed at which an LED brightens and dims
{
for (int i=100; i<=10; i++) // give value of 255 for variable "i"
// The above "for loop" function starts the integer "i" at 0, and tests to see if i is still less or equal to 255 and if true, increments "i" by 1 and executes the enclosed statements. i++ is the same as i=i +1 or i by +1

{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
for (int i=255; i>=0; i--) // if i is 255 then i should be 0
{
analogWrite(ledPin, i); // sets led pin 9 brightess level to i
delay(delayVal()); // generate time value and pauses
}
}

int delayVal() // declare variable for delay time value
{
int v; // create temporary variable
v = analogRead(analogPin); // read analog value
v /= 20; // convert 0-1024 to 0-128
return v; // returns final value
}

No comments:

Post a Comment