The second important part of code is a loop that blinks the light repeatedly, however many times you need it to according to where in the sequence you are.
Source code posted below:
/*
Fibonacci Number Sequence counter
by Jered Danielson and Ciera Johl, 25 April 2014
This sketch blinks the LED on "ledPin" according to the Fibonacci Sequence.
The Fibonacci Sequence is defined as Fn = Fn-1 + Fn-2, such as
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... etc.
*/
int ledPin = 13; // LED pin for output
int numberOne = 0; // Fn-2
int numberTwo = 1; // Fn-1
int delayTime = 400; // Delay time in milliseconds between blinks
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
int currentNumber = numberOne + numberTwo; // next number equals the sum of the last two
// Blink the LED "currentNumber" number of times
for(int i = currentNumber; i > 0; i--) {
digitalWrite(ledPin, HIGH);
delay(delayTime/2);
digitalWrite(ledPin, LOW);
delay(delayTime/2);
}
// Update the two stored numbers of the sequence
numberOne = numberTwo;
numberTwo = currentNumber;
delay(delayTime*3);
}
No comments:
Post a Comment