20100808

Fibonacci de Arduino


MAN! It's been a while. Well, not without good cause. Since my last post, I have started developing software for the Cocoa Touch platform, better known as iPad & iPhone platform! Since most of it is copyrighted I haven't been able to post any goodies pertaining to this, but I'll see what I can do.



Anyways, as the title somewhat indicates, I have recently acquired my very own Arduino board, and was glad I did! (Thanks Ma!) My background in C like languages has made the learning fast and efficient, as the syntax isn't too much different at all from traditional C. I had been reading about the Arduino boards for quite some time but skipped over the part where the heart of the board is an ATMEGA328/168!


So, as a first exercise, I decided to do something simple to get acclamated w/ the hardware and software. What better way than the Fibonacci sequence?


Well, not totally. Since the ATMega328 is a 32-bit device, the largest fibbonacci number it can calculate is 2971215073, before the 2^32-1 limitation kicks in. Never the less, it took all of about two hours to get everything up and running, and developing a quick and dirty alogrithm for this.

/*First Arduino*/
/*This project will print out the first 47 fibbonacci numbers*/
/*Copyright 2010 Mikestechspot.blogspot.com©*/

unsigned long nextFibbonacci(unsigned long);

unsigned long longFirstFib = 0;
unsigned long longSecFib = 1;
unsigned long longThirdFib = 1;


void setup(){
Serial.begin(9600);
Serial.println("Starting Fibbonacci Sequence:");
Serial.println(longFirstFib);
Serial.println(longSecFib);
delay(750);
}

void loop(){

longThirdFib = longFirstFib + longSecFib;

Serial.println(longThirdFib, DEC);
delay(750);

if(longThirdFib == 2971215073){ //Sequence is done for an unsigned long data type limitation
Serial.println("Fibbonacci sequence limited to 2971215073, becuase of 32-bit unsigned long");
Serial.println("Fibbnacci sequence complete");
while(true){
continue;
}
}

longFirstFib = longSecFib;
longSecFib = longThirdFib;

}

/*End of Code*/



I didn't comment the code this time, but if you take it line by line you should be fine. Any questions, feel free to comment, or visit the Arduino Language refernce pages and Arduino forums as well.