Guest User

Untitled

a guest
Apr 10th, 2016
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/interrupt.h>
  2. #include <avr/pgmspace.h>
  3. #include "audio.h"
  4.  
  5. struct fadeLED {
  6.   int pin;
  7.   int brit;
  8.   int fade;
  9. };
  10.  
  11. const int ledCount = 4;
  12. const int ledHigh = 200;
  13. const int fadeStep = 10;
  14.  
  15. fadeLED leds[ledCount];
  16.  
  17. unsigned long pMilis = 0;
  18. long interval = 20;
  19.  
  20. int i;
  21.  
  22. int curLed = 0;
  23.  
  24. volatile uint16_t pos;
  25.  
  26.  
  27. void start_timer(){
  28.     TCCR0A = (1 << WGM01);
  29.     OCR0A = ((F_CPU / 8 / SAMPLE_RATE)-1);
  30.     TIMSK0 |= (1 << OCIE0A);
  31.     TCCR0B = (1 << CS01);
  32. }
  33.  
  34. void stop_timer(){
  35.     cli();
  36.     TIMSK0 &=~ (1 << OCIE0A);
  37.     TCNT0 = 0;
  38. }
  39.  
  40. void start_pwm(){
  41.     TCCR1A = (1 << COM1A1) | (1 << WGM10);
  42.     TCCR1B = (1 << WGM12) | (1 << CS10);
  43.     OCR1A = 128;
  44. }
  45.  
  46. void setup() {
  47.   fadeLED tled = {3, 0, 5};
  48.   leds[0] = tled;
  49.   tled = {5, ledHigh, fadeStep};
  50.   leds[1] = tled;
  51.   tled = {6, ledHigh, fadeStep};
  52.   leds[2] = tled;
  53.   tled = {10, ledHigh, fadeStep};
  54.   leds[3] = tled;
  55.  
  56.   for(i=0; i < ledCount; i++) {
  57.     pinMode(leds[i].pin, OUTPUT);
  58.     analogWrite(leds[i].pin, leds[i].brit);
  59.   }
  60.  
  61.   DDRB = (1 << PB1);
  62.   DDRB |= (1 << PB5);
  63.  
  64.   start_pwm();
  65.   delay(500);
  66.   pos = (START_POS + 0);
  67.   sei();
  68.   start_timer();
  69. }
  70.  
  71. void loop() {
  72.   if (pos == AUDIO_SIZE) {
  73.       stop_timer();
  74.       pos = (START_POS + 0);
  75.       PORTB |= (1 << PB5);
  76.   }
  77.   interval = round(analogRead(0)/10);
  78.   unsigned long curMilis = millis();
  79.   if(curMilis - pMilis >= interval) {
  80.     pMilis = curMilis;
  81.     int nextLed = curLed + 1;
  82.     if(nextLed >= ledCount) {
  83.       nextLed = 0;
  84.     }
  85.     bool shift = false;
  86.     bool setNext = false;
  87.     leds[curLed].brit += leds[curLed].fade;
  88.     if(leds[curLed].brit <= 0 || leds[curLed].brit >= ledHigh) {
  89.       if(leds[curLed].brit > ledHigh) leds[curLed].brit = ledHigh;
  90.       if(leds[curLed].brit < 0) leds[curLed].brit = 0;
  91.       if(leds[curLed].brit >= ledHigh) {
  92.         shift = true;
  93.       }
  94.       leds[curLed].fade = -leds[curLed].fade;
  95.       if(leds[curLed].fade > 0) {
  96.         leds[nextLed].fade = -fadeStep;
  97.         setNext = true;
  98.       }
  99.     }
  100.     if(setNext) {
  101.       leds[nextLed].brit += leds[nextLed].fade;
  102.       if(leds[nextLed].brit > ledHigh) {
  103.         leds[nextLed].brit = ledHigh;
  104.       }
  105.       if(leds[nextLed].brit < 0) {
  106.         leds[nextLed].brit = 0;
  107.       }
  108.     }
  109.     for(i=0; i < ledCount; i++) {
  110.       if(i == curLed || i == nextLed) {
  111.         analogWrite(leds[i].pin, leds[i].brit);
  112.       }
  113.     }
  114.     if(shift) {
  115.       curLed++;
  116.       if(curLed >= ledCount) {
  117.         curLed = 0;
  118.       }
  119.     }
  120.   }
  121. }
  122.  
  123. ISR(TIMER0_COMPA_vect)
  124. {
  125.     OCR1A = pgm_read_byte(&AUDIO_DATA[pos]);
  126.     if (pos < AUDIO_SIZE) {
  127.       pos++;
  128.     }
  129. }
RAW Paste Data