Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- modified by varind in 2013 and again by Hannes in 2014
- this code is public domain, enjoy!
- */
- #include <SPI.h>
- #include <LiquidCrystal.h>
- #include <fix_fft.h>
- //#include <Servo.h>
- #define LCHAN 5
- #define RCHAN 4
- const int channels = 2;
- const int xres = 16; //16 is goed, 32 maakt hem duidelijker
- const int yres = 16; // 8 voor onderste gedeelte, 16 voor hele display
- const int gain = 3;
- //int decayrate = 2; // larger number is longer decay
- int decaytest = 1;
- char im[64], data[64];
- char Rim[64], Rdata[64];
- char data_avgs[32];
- float peaks[32];
- int i = 0,val,Rval;
- int x = 0, y=0, z=0;
- int q = 1; // Band kiezen voor VU uitslag 74hc595- 1
- int r = 1; // Band kiezen voor VU uitslag 74hc595- 2
- // Liquid Crystal latch pin
- LiquidCrystal lcd(9);
- //const int LEDpins[6] = {3,4,5,6,7,8};
- int current;
- int highest = 350;
- int latchPin = 4;
- int clockPin = 2;
- int dataPin = 3;
- int volume[9] = {0b00000000, 0b00000001, 0b00000011, 0b00000111,
- 0b00001111, 0b00011111, 0b00111111, 0b01111111,
- 0b11111111};
- int current2;
- int highest2 = 350;
- int latchPin2 = 7;
- int clockPin2 = 8;
- int dataPin2 = 6;
- int volume2[9] = {0b00000000, 0b00000001, 0b00000011, 0b00000111,
- 0b00001111, 0b00011111, 0b00111111, 0b01111111,
- 0b11111111};
- /*
- Servo servo1, servo2, servo3, servo4;
- int startpos = 90;
- int calibrate = 0; //locks servos at startpos (lowest value)
- int pos1 = startpos, pos2 = startpos, pos3 = startpos, pos4 = startpos;
- int servomap, servomap2;
- */
- // VU METER CHARACTERS
- byte v1[8] = {
- B00000,B00000,B00000,B00000,B00000,B00000,B00000,B11111};
- byte v2[8] = {
- B00000,B00000,B00000,B00000,B00000,B00000,B11111,B11111};
- byte v3[8] = {
- B00000,B00000,B00000,B00000,B00000,B11111,B11111,B11111};
- byte v4[8] = {
- B00000,B00000,B00000,B00000,B11111,B11111,B11111,B11111};
- byte v5[8] = {
- B00000,B00000,B00000,B11111,B11111,B11111,B11111,B11111};
- byte v6[8] = {
- B00000,B00000,B11111,B11111,B11111,B11111,B11111,B11111};
- byte v7[8] = {
- B00000,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
- byte v8[8] = {
- B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
- void setup() {
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(latchPin2, OUTPUT);
- pinMode(clockPin2, OUTPUT);
- pinMode(dataPin2, OUTPUT);
- Serial.begin(9600);
- // ============= LEDS ==============
- // for (int x=0; x<6; x++) {
- // pinMode(LEDpins[x], OUTPUT);
- //}
- // ============= LEDS END ==============
- lcd.begin(16, 2);
- lcd.clear();
- lcd.createChar(1, v1);
- lcd.createChar(2, v2);
- lcd.createChar(3, v3);
- lcd.createChar(4, v4);
- lcd.createChar(5, v5);
- lcd.createChar(6, v6);
- lcd.createChar(7, v7);
- lcd.createChar(8, v8);
- //analogReference(DEFAULT);
- // servo1.attach(5);
- // servo2.attach(6);
- // servo3.attach(9);
- // servo4.attach(10);
- // servo1.write(pos1);
- // servo2.write(pos2);
- // servo3.write(pos3);
- // servo4.write(pos4);
- }
- void loop() {
- // delay(10);
- for (i=0; i < 64; i++){
- val = ((analogRead(LCHAN) / 4 ) - 128); // chose how to interpret the data from analog in
- data[i] = val;
- im[i] = 0;
- if (channels ==2){
- Rval = ((analogRead(RCHAN) / 4 ) - 128); // chose how to interpret the data from analog in
- Rdata[i] = Rval;
- Rim[i] = 0;
- }
- };
- fix_fft(data,im,6,0); // Send the data through fft
- if (channels == 2){
- fix_fft(Rdata,Rim,6,0); // Send the data through fft
- }
- // get the absolute value of the values in the array, so we're only dealing with positive numbers
- for (i=0; i< 32 ;i++){
- data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
- }
- if (channels ==2){
- for (i=16; i< 32 ;i++){
- data[i] = sqrt(Rdata[i-16] * Rdata[i-16] + Rim[i-16] * Rim[i-16]);
- }
- }
- // todo: average as many or as little dynamically based on yres
- for (i=0; i<32; i++) {
- data_avgs[i] = (data[i]);// + data[i*2+1]);// + data[i*3 + 2]);// + data[i*4 + 3]); // add 3 samples to be averaged, use 4 when yres < 16
- data_avgs[i] = constrain(data_avgs[i],0,9-gain); //data samples * range (0-9) = 9
- data_avgs[i] = map(data_avgs[i], 0, 9-gain, 0, yres); // remap averaged values
- }
- //servos();
- decay(1);
- //thirtytwoband();
- mono();
- //stereo8();
- //stereo16();
- } // end loop
- void decay(int decayrate){
- //// reduce the values of the last peaks by 1
- if (decaytest == decayrate){
- for (x=0; x < 32; x++) {
- peaks[x] = peaks[x] - 1; // subtract 1 from each column peaks
- decaytest = 0;
- }
- }
- decaytest++;
- }
- void mono(){
- for (x=0; x < xres; x++) { // repeat for each column of the display horizontal resolution
- y = data_avgs[x]; // get current column value
- z= peaks[x];
- if (y > z){
- peaks[x]=y;
- }
- y= peaks[x];
- current = map(data[q], 0, 18, 0, 9);
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, LSBFIRST, volume[current]);
- digitalWrite(latchPin, HIGH);
- current2 = map(data[r], 0, 18, 0, 9);
- digitalWrite(latchPin2, LOW);
- shiftOut(dataPin2, clockPin2, LSBFIRST, volume2[current2]);
- digitalWrite(latchPin2, HIGH);
- /*
- if (data[q] >1) {
- digitalWrite(LEDpins[0], HIGH);
- } else {
- digitalWrite(LEDpins[0], LOW);
- }
- if (data[q] >3) {
- digitalWrite(LEDpins[1], HIGH);
- } else {
- digitalWrite(LEDpins[1], LOW);
- }
- if (data[q] >5) {
- digitalWrite(LEDpins[2], HIGH);
- } else {
- digitalWrite(LEDpins[2], LOW);
- }
- if (data[q] >7) {
- digitalWrite(LEDpins[3], HIGH);
- } else {
- digitalWrite(LEDpins[3], LOW);
- }
- if (data[q] >9) {
- digitalWrite(LEDpins[4], HIGH);
- } else {
- digitalWrite(LEDpins[4], LOW);
- }
- if (data[q] >11) {
- digitalWrite(LEDpins[5], HIGH);
- } else {
- digitalWrite(LEDpins[5], LOW);
- }
- if (data[q] >13) {
- digitalWrite(LEDpins[6], HIGH);
- } else {
- digitalWrite(LEDpins[6], LOW);
- }
- */
- if (y <= 8){
- lcd.setCursor(x,0); // clear first row
- lcd.print(" ");
- lcd.setCursor(x,1); // draw second row
- if (y == 0){
- lcd.print(" "); // save a glyph
- }
- else {
- lcd.write(y);
- }
- }
- else{
- lcd.setCursor(x,0); // draw first row
- if (y == 9){
- lcd.print(" ");
- }
- else {
- lcd.write(y-8); // same chars 1-8 as 9-16
- }
- lcd.setCursor(x,1);
- lcd.write(8);
- } // end display
- } // end xres
- }
- /*
- void stereo8(){
- for (x=0; x < 8; x++) {
- y = data_avgs[x];
- z= peaks[x];
- if (y > z){
- peaks[x]=y;
- }
- y= peaks[x];
- if (y <= 8){
- lcd.setCursor(x,0); // clear first row
- lcd.print(" ");
- lcd.setCursor(x,1); // draw second row
- if (y == 0){
- lcd.print(" "); // save a glyph
- }
- else {
- lcd.write(y);
- }
- }
- else{
- lcd.setCursor(x,0); // draw first row
- if (y == 9){
- lcd.write(" ");
- }
- else {
- lcd.write(y-8); // same chars 1-8 as 9-16
- }
- lcd.setCursor(x,1);
- lcd.write(8);
- }
- }
- for (x=16; x < 32; x++) {
- y = data_avgs[x];
- z= peaks[x];
- if (y > z){
- peaks[x]=y;
- }
- y= peaks[x];
- if (y <= 8){
- lcd.setCursor(x-8,0); // clear first row
- lcd.print(" ");
- lcd.setCursor(x-8,1); // draw second row
- if (y == 0){
- lcd.print(" "); // save a glyph
- }
- else {
- lcd.write(y);
- }
- }
- else{
- lcd.setCursor(x-8,0); // draw first row
- if (y == 9){
- lcd.write(" ");
- }
- else {
- lcd.write(y-8); // same chars 1-8 as 9-16
- }
- lcd.setCursor(x-8,1);
- lcd.write(8);
- }
- }
- }
- void stereo16(){
- for (x=0; x < 16; x++) {
- y = data_avgs[x];
- z= peaks[x];
- if (y > z){
- peaks[x]=y;
- }
- y= peaks[x];
- if (x < xres){
- lcd.setCursor(x,0); // draw first row
- if (y == 0){
- lcd.print(" "); // save a glyph
- }
- else {
- lcd.write(y);
- }
- }
- }
- for (x=16; x < 32; x++) {
- y = data_avgs[x];
- z= peaks[x];
- if (y > z){
- peaks[x]=y;
- }
- y= peaks[x];
- if (x-16 < xres){
- lcd.setCursor(x-16,1); // draw second row
- if (y == 0){
- lcd.print(" "); // save a glyph
- }
- else {
- lcd.write(y);
- }
- }
- }
- }
- void thirtytwoband(){
- for (x=0; x < 32; x++) {
- y = data_avgs[x];
- z= peaks[x];
- if (y > z){
- peaks[x]=y;
- }
- y= peaks[x];
- if (x < 16){
- lcd.setCursor(x,0); // draw second row
- if (y == 0){
- lcd.print(" "); // save a glyph
- }
- else {
- lcd.write(y);
- }
- }
- else{
- lcd.setCursor(x-16,1);
- if (y == 0){
- lcd.print(" ");
- }
- else {
- lcd.write(y);
- }
- }
- }
- }
- void goservo(){
- for (x=0; x < xres; x++) { // repeat for each column of the display horizontal resolution
- y = data_avgs[x]; // get current column value
- if (calibrate != 1){
- servomap = map(y, 0, yres, 90, 180);
- servomap2 = map(y, 0, yres, 90, 00);
- if ( x== xres/8){
- servo1.write(servomap);
- }
- if (x == xres/4){
- servo2.write(servomap2);
- }
- if (x == xres/2){
- servo3.write(servomap);
- }
- if (x == xres/1){
- servo4.write(servomap2);
- }
- }
- }
- } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement