<?xml version="1.0"?>
<!--Copyright Brian Starkey 2011-->
<page title="AVR Lightning Effect" dir="projects/lightning" filename="lightning" type="windowpage" root="/xml_95">
<window title="AVR Lightning Effect">
	<menu>
		<menu-item name="File">
			<!--<pdf-subitem name="Get PDF"/>-->
			<home-subitem name="Home" href="{/page/@root}"/>
			<menu-subitem name="XML" href="lightning.xml"/>
		</menu-item>
		<!--include common menu items (dynamically generated)-->
        <autogen type='category' category='all'/>
	</menu>
	<substance>
	<locations name="{/page/@title}">
		<folder name="Documentation">
			<!--<leaf class="pdfleaf" name="proj.pdf" href="proj.pdf" />-->
			<!-- img="/images/pdf_icon.gif" -->
			<leaf class="videoleaf" name="Description" href="#Description"/>
			<leaf class="codeleaf" name="Listing" href="#Listing"/>
		</folder>
		<folder name="Code">
			<leaf class="zipleaf" name="lightning.zip" href="code/lightning.zip"/>
			<leaf class='codeleaf' name="makefile" href="code/makefile"/>
			<leaf class='codeleaf' name="lightning.c" href="code/lightning.c"/>
		</folder>
	</locations>
	<window-contents>
		<row>
			<section-title name="Description"/>
			<video src="http://www.youtube.com/embed/y-bg39shvsc"/>
            <section-content>
			There isn't a whole lot to say about this - it's basically a lightning effect I made for a halloween costume which runs on an attiny2313. If you ever want to add a lightning
            effect to something, feel free to use this code. I was pretty pleased with how it turned out.<br/>
            The flashes are random, and occur in bursts of a random length with random timings between flashes, and the bursts themselves happen at random intervals. The LEDs go to full
            brightness on a 'bolt' and then fade out according to FADE_SPEED. If another 'bolt' happens before the LEDs have fully faded out, they go back to full brightness and start
            fading again. This gives quite a convincing effect which the video doesn't really do justice.<br/>
            It uses timer 0 for the PWM of the LEDs and delays for the timing between bolts and bursts.
			</section-content>
        </row>
        <row>
        <section-title name="Listing"/>
        <code language='cpp'>
<![CDATA[
#define F_CPU 8000000

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>

#define FADE_SPEED 3

unsigned volatile char level;

int main(void) {
	DDRB |= (1 << 2);
	PORTB |= (1 << 2);
	_delay_ms(1000);
	PORTB &= ~(1 << 2);
	OCR0A = 0;
	TCCR0A |= 0x83;
	TIMSK |= (1 << TOIE0);
	sei();
	OCR0A = 0;

	for (;;) {
		unsigned char bolt_count = rand() >> 12;
		while (bolt_count--) {
			unsigned char between_bolts = (rand() >> 8);
			between_bolts <<= 1;
			level = 255;
			OCR0A = level;
			TCCR0B |= 0x03;
			TCCR0A |= 0x83;
			_delay_ms(between_bolts);
		}
		_delay_ms(rand() << 10);
	}
}

ISR(TIMER0_OVF_vect) {
	if (level > FADE_SPEED) {
		level -= FADE_SPEED;
		OCR0A = level;
	}
	else {
		TCCR0A = 0;
		TCCR0B &= ~(0x07);
		OCR0A = 0;
		TCNT0 = 0;
	}
}
]]>
        </code>
</row>

<comments/>

	</window-contents>
	</substance>
</window>

</page>


