Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- #include "audio.h"
- struct fadeLED {
- int pin;
- int brit;
- int fade;
- };
- const int ledCount = 4;
- const int ledHigh = 200;
- const int fadeStep = 10;
- fadeLED leds[ledCount];
- unsigned long pMilis = 0;
- long interval = 20;
- int i;
- int curLed = 0;
- volatile uint16_t pos;
- void start_timer(){
- TCCR0A = (1 << WGM01);
- OCR0A = ((F_CPU / 8 / SAMPLE_RATE)-1);
- TIMSK0 |= (1 << OCIE0A);
- TCCR0B = (1 << CS01);
- }
- void stop_timer(){
- cli();
- TIMSK0 &=~ (1 << OCIE0A);
- TCNT0 = 0;
- }
- void start_pwm(){
- TCCR1A = (1 << COM1A1) | (1 << WGM10);
- TCCR1B = (1 << WGM12) | (1 << CS10);
- OCR1A = 128;
- }
- void setup() {
- fadeLED tled = {3, 0, 5};
- leds[0] = tled;
- tled = {5, ledHigh, fadeStep};
- leds[1] = tled;
- tled = {6, ledHigh, fadeStep};
- leds[2] = tled;
- tled = {10, ledHigh, fadeStep};
- leds[3] = tled;
- for(i=0; i < ledCount; i++) {
- pinMode(leds[i].pin, OUTPUT);
- analogWrite(leds[i].pin, leds[i].brit);
- }
- DDRB = (1 << PB1);
- DDRB |= (1 << PB5);
- start_pwm();
- delay(500);
- pos = (START_POS + 0);
- sei();
- start_timer();
- }
- void loop() {
- if (pos == AUDIO_SIZE) {
- stop_timer();
- pos = (START_POS + 0);
- PORTB |= (1 << PB5);
- }
- interval = round(analogRead(0)/10);
- unsigned long curMilis = millis();
- if(curMilis - pMilis >= interval) {
- pMilis = curMilis;
- int nextLed = curLed + 1;
- if(nextLed >= ledCount) {
- nextLed = 0;
- }
- bool shift = false;
- bool setNext = false;
- leds[curLed].brit += leds[curLed].fade;
- if(leds[curLed].brit <= 0 || leds[curLed].brit >= ledHigh) {
- if(leds[curLed].brit > ledHigh) leds[curLed].brit = ledHigh;
- if(leds[curLed].brit < 0) leds[curLed].brit = 0;
- if(leds[curLed].brit >= ledHigh) {
- shift = true;
- }
- leds[curLed].fade = -leds[curLed].fade;
- if(leds[curLed].fade > 0) {
- leds[nextLed].fade = -fadeStep;
- setNext = true;
- }
- }
- if(setNext) {
- leds[nextLed].brit += leds[nextLed].fade;
- if(leds[nextLed].brit > ledHigh) {
- leds[nextLed].brit = ledHigh;
- }
- if(leds[nextLed].brit < 0) {
- leds[nextLed].brit = 0;
- }
- }
- for(i=0; i < ledCount; i++) {
- if(i == curLed || i == nextLed) {
- analogWrite(leds[i].pin, leds[i].brit);
- }
- }
- if(shift) {
- curLed++;
- if(curLed >= ledCount) {
- curLed = 0;
- }
- }
- }
- }
- ISR(TIMER0_COMPA_vect)
- {
- OCR1A = pgm_read_byte(&AUDIO_DATA[pos]);
- if (pos < AUDIO_SIZE) {
- pos++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement