Friday, July 9, 2010

fibonacci blinker

arduino code:

blinks the led on pin 13 in the fibonacci sequence - at least until it hits the max value of a long, and then rolls over.



int ledPin = 13; // LED connected to digital pin 13
long seq1 = 1; // Sequence number 1
long seq2 = 2; // Sequence number 2
long seq3 = 3; // Sequence number 3
// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
for (int numberOftimes = seq1; numberOftimes >=1; numberOftimes--){
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
delay(5000); // wait 5 seconds between series of blinks
seq1=seq2; //shift the second number into the first variable
seq2=seq3; //shift the third number into the second variable
seq3=seq1+seq2; // calculate the third number (based on previous two)

}

No comments: