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=0; i<=300; 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=300; 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 /= 50; // convert 0-1024 to 0-128
return v; // returns final value
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment