As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Friday, April 25, 2014

Jered and Ciera | Arduino | Fibonacci Sequence Blinking

Writing an Arduino sketch to blink an LED according to the Fibonacci Sequence is fairly straightforward. The two main variables you need to keep track of are the last two numbers in the sequence. Without them, it is impossible to keep track of where you are as you step through the sequence!

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