From 1ea3012576bd381a4b87908cd43eb2acc9a422e3 Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Wed, 12 Feb 2020 17:37:10 +0100 Subject: [PATCH 1/9] struktur und tuner variablen --- .vscode/arduino.json | 7 ++++++ .vscode/c_cpp_properties.json | 16 +++++++++++++ main.ino | 42 ++++++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 .vscode/arduino.json create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/arduino.json b/.vscode/arduino.json new file mode 100644 index 0000000..3a5bfa1 --- /dev/null +++ b/.vscode/arduino.json @@ -0,0 +1,7 @@ +{ + "board": "arduino:avr:nano", + "configuration": "cpu=atmega328", + "sketch": "main.ino", + "port": "COM4", + "programmer": "AVR ISP" +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..fd902e9 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "D:\\Tools\\_development\\arduino-1.8.11\\tools\\**", + "D:\\Tools\\_development\\arduino-1.8.11\\hardware\\arduino\\avr\\**" + ], + "forcedInclude": [ + "D:\\Tools\\_development\\arduino-1.8.11\\hardware\\arduino\\avr\\cores\\arduino\\Arduino.h" + ], + "intelliSenseMode": "msvc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/main.ino b/main.ino index a30efe3..840bef3 100644 --- a/main.ino +++ b/main.ino @@ -5,18 +5,53 @@ #include -// Adafruit SSD1306 - Version: 2.1.0 +// Adafruit SSD1306 - Version: Latest #include #include - +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#pragma region DISPLAY +// DISPLAY #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); - +#pragma endregion + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#pragma region TUNER +// TUNER +//clipping indicator variables +boolean clipping = 0; + +//data storage variables +byte newData = 0; +byte prevData = 0; +unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally +int timer[10];//storage for timing of events +int slope[10];//storage for slope of events +unsigned int totalTimer;//used to calculate period +unsigned int period;//storage for period of wave +byte index = 0;//current storage index +float frequency;//storage for frequency calculations +int maxSlope = 0;//used to calculate max slope as trigger point +int newSlope;//storage for incoming slope data + +//variables for decided whether you have a match +byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long +byte slopeTol = 3;//slope tolerance- adjust this if you need +int timerTol = 10;//timer tolerance- adjust this if you need + +//variables for amp detection +unsigned int ampTimer = 0; +byte maxAmp = 0; +byte checkMaxAmp; +byte ampThreshold = 30;//raise if you have a very noisy signal +#pragma endregion + + void setup() { //Initialize display by providing the display type and its I2C address. @@ -25,6 +60,7 @@ void setup() display.setTextSize(1); display.setTextColor(WHITE); } + void loop() { byte poti_threshold = (analogRead(A0)/4); From c0c7fae1e7c26bdb77e53772c98fe2241c94daab Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Wed, 12 Feb 2020 21:48:05 +0100 Subject: [PATCH 2/9] den Registern auf der Spur --- main.ino | 150 +++++++++++++++++++++++++++++++++++++++++++--- tuner.ino_ | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 312 insertions(+), 9 deletions(-) create mode 100644 tuner.ino_ diff --git a/main.ino b/main.ino index 840bef3..48e2027 100644 --- a/main.ino +++ b/main.ino @@ -5,7 +5,7 @@ #include -// Adafruit SSD1306 - Version: Latest +// Adafruit SSD1306 - Version: 2.1.0 #include #include @@ -21,7 +21,7 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #pragma endregion ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#pragma region TUNER +#pragma region TUNER variables // TUNER //clipping indicator variables boolean clipping = 0; @@ -49,9 +49,12 @@ unsigned int ampTimer = 0; byte maxAmp = 0; byte checkMaxAmp; byte ampThreshold = 30;//raise if you have a very noisy signal + #pragma endregion +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// FUNCTIONS void setup() { //Initialize display by providing the display type and its I2C address. @@ -59,25 +62,63 @@ void setup() //Set the text size and color. display.setTextSize(1); display.setTextColor(WHITE); + + /* cli();//disable interrupts + //set up continuous sampling of analog pin 0 at 38.5kHz + //clear ADCSRA and ADCSRB registers + ADCSRA = 0; + //ADCSRB = 0; + + ADMUX |= (1 << REFS0); //set reference voltage + ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only + + ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz + ADCSRA |= (1 << ADATE); //enabble auto trigger + ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete + ADCSRA |= (1 << ADEN); //enable ADC + ADCSRA |= (1 << ADSC); //start ADC measurements + + sei();//enable interrupts */ + + } + + + + + + + void loop() { - byte poti_threshold = (analogRead(A0)/4); - byte poti_ratio = (analogRead(A1)/4); - float audio_in = analogRead(A2)*(5.0 / 1023.0); + byte poti_threshold = (analogRead(A1)/4); + byte poti_ratio = (analogRead(A2)/4); + float audio_in = analogRead(A0)*(5.0 / 1023.0); + + byte reg = ADCSRA; //Clear previous image. display.clearDisplay(); display.setCursor(0, 10); - display.print(audio_in); + display.print(reg); + + checkClipping(); + if (checkMaxAmp>ampThreshold){ + frequency = 38462/float(period);//calculate frequency timer rate/period + + //print results + display.print(frequency); + } + + if (poti_threshold > 0){ drawCompSettings(poti_threshold,poti_ratio); } - + display.display(); - delay(30); + delay(100); } @@ -105,4 +146,95 @@ void drawCompSettings (byte thresh, float ratio) { display.setCursor(SCREEN_WIDTH/2, 20); display.print(ratio); display.fillTriangle(threshX+1 , threshY-1, SCREEN_WIDTH, (threshY-((float)threshY/ratio)), SCREEN_WIDTH, threshY-1, WHITE); -} \ No newline at end of file +} + + + + + + + +ISR(ADC_vect) {//when new ADC value ready + + PORTB &= B11101111;//set pin 12 low + prevData = newData;//store previous value + newData = ADCH;//get value from A0 + if (prevData < 127 && newData >=127){//if increasing and crossing midpoint + newSlope = newData - prevData;//calculate slope + if (abs(newSlope-maxSlope) 9){ + reset(); + } + } + } + else if (newSlope>maxSlope){//if new slope is much larger than max slope + maxSlope = newSlope; + time = 0;//reset clock + noMatch = 0; + index = 0;//reset index + } + else{//slope not steep enough + noMatch++;//increment no match counter + if (noMatch>9){ + reset(); + } + } + } + + if (newData == 0 || newData == 1023){//if clipping + clipping = 1;//currently clipping + Serial.println("clipping"); + } + + time++;//increment timer at rate of 38.5kHz + + ampTimer++;//increment amplitude timer + if (abs(127-ADCH)>maxAmp){ + maxAmp = abs(127-ADCH); + } + if (ampTimer==1000){ + ampTimer = 0; + checkMaxAmp = maxAmp; + maxAmp = 0; + } + +} + +void reset(){//clean out some variables + index = 0;//reset index + noMatch = 0;//reset match couner + maxSlope = 0;//reset slope +} + + +void checkClipping(){//manage clipping indication + if (clipping){//if currently clipping + clipping = 0; + } +} + diff --git a/tuner.ino_ b/tuner.ino_ new file mode 100644 index 0000000..7851031 --- /dev/null +++ b/tuner.ino_ @@ -0,0 +1,171 @@ +/* +* Modified Arduino Frequency Detection +* by Nicole Grimwood +* +* For more information please visit: +* https://www.instructables.com/id/Arduino-Guitar-Tuner/ +* +* +* Slightly edited version of: +* Arduino Frequency Detection +* created October 7, 2012 +* by Amanda Ghassaei +* +* This code is in the public domain. +*/ + +//clipping indicator variables +boolean clipping = 0; + +//data storage variables +byte newData = 0; +byte prevData = 0; +unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally +int timer[10];//storage for timing of events +int slope[10];//storage for slope of events +unsigned int totalTimer;//used to calculate period +unsigned int period;//storage for period of wave +byte index = 0;//current storage index +float frequency;//storage for frequency calculations +int maxSlope = 0;//used to calculate max slope as trigger point +int newSlope;//storage for incoming slope data + +//variables for decided whether you have a match +byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long +byte slopeTol = 3;//slope tolerance- adjust this if you need +int timerTol = 10;//timer tolerance- adjust this if you need + +//variables for amp detection +unsigned int ampTimer = 0; +byte maxAmp = 0; +byte checkMaxAmp; +byte ampThreshold = 30;//raise if you have a very noisy signal + +void setup(){ + + Serial.begin(9600); + + pinMode(13,OUTPUT);//led indicator pin + pinMode(12,OUTPUT);//output pin + + cli();//diable interrupts + + //set up continuous sampling of analog pin 0 at 38.5kHz + + //clear ADCSRA and ADCSRB registers + ADCSRA = 0; + ADCSRB = 0; + + ADMUX |= (1 << REFS0); //set reference voltage + ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only + + ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz + ADCSRA |= (1 << ADATE); //enabble auto trigger + ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete + ADCSRA |= (1 << ADEN); //enable ADC + ADCSRA |= (1 << ADSC); //start ADC measurements + + sei();//enable interrupts +} + +ISR(ADC_vect) {//when new ADC value ready + + PORTB &= B11101111;//set pin 12 low + prevData = newData;//store previous value + newData = ADCH;//get value from A0 + if (prevData < 127 && newData >=127){//if increasing and crossing midpoint + newSlope = newData - prevData;//calculate slope + if (abs(newSlope-maxSlope) 9){ + reset(); + } + } + } + else if (newSlope>maxSlope){//if new slope is much larger than max slope + maxSlope = newSlope; + time = 0;//reset clock + noMatch = 0; + index = 0;//reset index + } + else{//slope not steep enough + noMatch++;//increment no match counter + if (noMatch>9){ + reset(); + } + } + } + + if (newData == 0 || newData == 1023){//if clipping + clipping = 1;//currently clipping + Serial.println("clipping"); + } + + time++;//increment timer at rate of 38.5kHz + + ampTimer++;//increment amplitude timer + if (abs(127-ADCH)>maxAmp){ + maxAmp = abs(127-ADCH); + } + if (ampTimer==1000){ + ampTimer = 0; + checkMaxAmp = maxAmp; + maxAmp = 0; + } + +} + +void reset(){//clean out some variables + index = 0;//reset index + noMatch = 0;//reset match couner + maxSlope = 0;//reset slope +} + + +void checkClipping(){//manage clipping indication + if (clipping){//if currently clipping + clipping = 0; + } +} + + +void loop(){ + + checkClipping(); + + + if (checkMaxAmp>ampThreshold){ + frequency = 38462/float(period);//calculate frequency timer rate/period + + //print results + Serial.print(frequency); + Serial.println(" hz"); + } + + delay(100); + +} From 2b386af7a2390e0a5c9a1e3989e14cf7aff78e80 Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Fri, 14 Feb 2020 14:28:29 +0100 Subject: [PATCH 3/9] tuner funktioniert, display nicht mehr --- main.ino | 179 +++++++----------------------------------------------- tuner.cpp | 34 +++++++++++ tuner.h | 16 +++++ 3 files changed, 72 insertions(+), 157 deletions(-) create mode 100644 tuner.cpp create mode 100644 tuner.h diff --git a/main.ino b/main.ino index 48e2027..38bbdb3 100644 --- a/main.ino +++ b/main.ino @@ -9,6 +9,11 @@ #include #include +// eigene Programm Dateien +#include "tuner.h" + +#define MODE 1 // 0 = COMPRESSOR / 1 = TUNER + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #pragma region DISPLAY @@ -22,65 +27,20 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #pragma region TUNER variables -// TUNER -//clipping indicator variables -boolean clipping = 0; - -//data storage variables -byte newData = 0; -byte prevData = 0; -unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally -int timer[10];//storage for timing of events -int slope[10];//storage for slope of events -unsigned int totalTimer;//used to calculate period -unsigned int period;//storage for period of wave -byte index = 0;//current storage index -float frequency;//storage for frequency calculations -int maxSlope = 0;//used to calculate max slope as trigger point -int newSlope;//storage for incoming slope data - -//variables for decided whether you have a match -byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long -byte slopeTol = 3;//slope tolerance- adjust this if you need -int timerTol = 10;//timer tolerance- adjust this if you need - -//variables for amp detection -unsigned int ampTimer = 0; -byte maxAmp = 0; -byte checkMaxAmp; -byte ampThreshold = 30;//raise if you have a very noisy signal +// TUNER #pragma endregion - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // FUNCTIONS void setup() { + Serial.begin(9600); //Initialize display by providing the display type and its I2C address. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Set the text size and color. display.setTextSize(1); display.setTextColor(WHITE); - - /* cli();//disable interrupts - //set up continuous sampling of analog pin 0 at 38.5kHz - //clear ADCSRA and ADCSRB registers - ADCSRA = 0; - //ADCSRB = 0; - - ADMUX |= (1 << REFS0); //set reference voltage - ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only - - ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz - ADCSRA |= (1 << ADATE); //enabble auto trigger - ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete - ADCSRA |= (1 << ADEN); //enable ADC - ADCSRA |= (1 << ADSC); //start ADC measurements - - sei();//enable interrupts */ - - } @@ -92,33 +52,29 @@ void setup() void loop() { - byte poti_threshold = (analogRead(A1)/4); - byte poti_ratio = (analogRead(A2)/4); - float audio_in = analogRead(A0)*(5.0 / 1023.0); - - byte reg = ADCSRA; - //Clear previous image. display.clearDisplay(); - display.setCursor(0, 10); - display.print(reg); - checkClipping(); - if (checkMaxAmp>ampThreshold){ - frequency = 38462/float(period);//calculate frequency timer rate/period + if (MODE == 0){ //COMPRESSOR + byte poti_threshold = (analogRead(A1)/4); + byte poti_ratio = (analogRead(A2)/4); + + if (poti_threshold > 0){ + drawCompSettings(poti_threshold,poti_ratio); + } + + delay(100); + } - //print results - display.print(frequency); + display.setCursor(0, 10); + + if (MODE == 1){ //TUNER + tuner(); } - if (poti_threshold > 0){ - drawCompSettings(poti_threshold,poti_ratio); - } - display.display(); - delay(100); } @@ -146,95 +102,4 @@ void drawCompSettings (byte thresh, float ratio) { display.setCursor(SCREEN_WIDTH/2, 20); display.print(ratio); display.fillTriangle(threshX+1 , threshY-1, SCREEN_WIDTH, (threshY-((float)threshY/ratio)), SCREEN_WIDTH, threshY-1, WHITE); -} - - - - - - - -ISR(ADC_vect) {//when new ADC value ready - - PORTB &= B11101111;//set pin 12 low - prevData = newData;//store previous value - newData = ADCH;//get value from A0 - if (prevData < 127 && newData >=127){//if increasing and crossing midpoint - newSlope = newData - prevData;//calculate slope - if (abs(newSlope-maxSlope) 9){ - reset(); - } - } - } - else if (newSlope>maxSlope){//if new slope is much larger than max slope - maxSlope = newSlope; - time = 0;//reset clock - noMatch = 0; - index = 0;//reset index - } - else{//slope not steep enough - noMatch++;//increment no match counter - if (noMatch>9){ - reset(); - } - } - } - - if (newData == 0 || newData == 1023){//if clipping - clipping = 1;//currently clipping - Serial.println("clipping"); - } - - time++;//increment timer at rate of 38.5kHz - - ampTimer++;//increment amplitude timer - if (abs(127-ADCH)>maxAmp){ - maxAmp = abs(127-ADCH); - } - if (ampTimer==1000){ - ampTimer = 0; - checkMaxAmp = maxAmp; - maxAmp = 0; - } - -} - -void reset(){//clean out some variables - index = 0;//reset index - noMatch = 0;//reset match couner - maxSlope = 0;//reset slope -} - - -void checkClipping(){//manage clipping indication - if (clipping){//if currently clipping - clipping = 0; - } -} - +} \ No newline at end of file diff --git a/tuner.cpp b/tuner.cpp new file mode 100644 index 0000000..db52009 --- /dev/null +++ b/tuner.cpp @@ -0,0 +1,34 @@ +#include "tuner.h" +#include "Arduino.h" + +int zaehler; +unsigned long timer; +unsigned long timerOld; +unsigned long startzeit; +unsigned long messzeit = 1000000; + + + +void tuner (){ + if ((micros() - startzeit) >= messzeit) + { + float f = timer; //Datentyp 'float', wegen untenstehender Division + f = 1000000/f; //Aus Periodendauer Frequenz berechnen + detachInterrupt(0); + + Serial.print("Messung: "); + Serial.println(f, 1); + + attachInterrupt(digitalPinToInterrupt(2), Messung, RISING); + zaehler = 0; //Frequenzzähler zurücksetzen + startzeit = micros(); //Zeitpunkt der letzten Ausgabe speichern + } +} + + +void Messung() +{ + zaehler++; + timer = micros() - timerOld; + timerOld = micros(); +} diff --git a/tuner.h b/tuner.h new file mode 100644 index 0000000..3a8edbf --- /dev/null +++ b/tuner.h @@ -0,0 +1,16 @@ +#ifndef TUNER_H +#define TUNER_H + +extern int zaehler; +extern unsigned long timer; +extern unsigned long timerOld; +extern unsigned long startzeit; +extern unsigned long messzeit; + +extern void tuner(); +extern void Messung(); + + +#endif /* TUNER_H */ + + From 32c133a24020270d98442a7fb0325bdd1e13f139 Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Fri, 14 Feb 2020 14:31:20 +0100 Subject: [PATCH 4/9] der beste Tuner bisher --- freq.ino_ | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 freq.ino_ diff --git a/freq.ino_ b/freq.ino_ new file mode 100644 index 0000000..1f11f21 --- /dev/null +++ b/freq.ino_ @@ -0,0 +1,50 @@ +//Credits: http://www.breadboarding.de/frequenzmessung-arduino/ + +int zaehler; +unsigned long timer; +unsigned long timerOld; +unsigned long startzeit; +unsigned long messzeit = 1000000; + +void setup(){ + + Serial.begin(9600); + + +} + + +void loop(){ + if ((micros() - startzeit) >= messzeit) + { + float f = timer; //Datentyp 'float', wegen untenstehender Division + f = 1000000/f; //Aus Periodendauer Frequenz berechnen + detachInterrupt(0); + if(f >= 300){ + Serial.print("Zaehler: "); + Serial.println(zaehler); + } + else if(f < 300 && f >= 30){ + Serial.print("Messung: "); + Serial.println(f, 1); + } + else if(f < 30 && f >= 3){ + Serial.print("Messung: "); + Serial.println(f, 3); + } + else if(f < 3){ + Serial.print("Messung: "); + Serial.println(f, 5); + } + attachInterrupt(digitalPinToInterrupt(2), Messung, RISING); + zaehler = 0; //Frequenzzähler zurücksetzen + startzeit = micros(); //Zeitpunkt der letzten Ausgabe speichern + } +} + +void Messung() +{ + zaehler++; + timer = micros() - timerOld; + timerOld = micros(); +} \ No newline at end of file From 89681131c4638ebcd1e7e283f710914767d15bcf Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Fri, 14 Feb 2020 14:35:25 +0100 Subject: [PATCH 5/9] doch noch --- .vscode/arduino.json | 2 +- tuner2.ino_ | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tuner2.ino_ diff --git a/.vscode/arduino.json b/.vscode/arduino.json index 3a5bfa1..9916af2 100644 --- a/.vscode/arduino.json +++ b/.vscode/arduino.json @@ -3,5 +3,5 @@ "configuration": "cpu=atmega328", "sketch": "main.ino", "port": "COM4", - "programmer": "AVR ISP" + "output": "../ArduinoOutput" } \ No newline at end of file diff --git a/tuner2.ino_ b/tuner2.ino_ new file mode 100644 index 0000000..8186cde --- /dev/null +++ b/tuner2.ino_ @@ -0,0 +1,66 @@ + + +#define LENGTH 512 + +byte rawData[LENGTH]; +int count; + +// Sample Frequency in kHz +const float sample_freq = 8919; + +int len = sizeof(rawData); +int i,k; +long sum, sum_old; +int thresh = 0; +float freq_per = 0; +byte pd_state = 0; + +void setup(){ + //analogReference(EXTERNAL); // Connect to 3.3V + analogRead(A0); + Serial.begin(115200); + count = 0; +} + + +void loop(){ + + if (count < LENGTH) { + count++; + rawData[count] = analogRead(A0)>>2; + } + else { + sum = 0; + pd_state = 0; + int period = 0; + for(i=0; i < len; i++) + { + // Autocorrelation + sum_old = sum; + sum = 0; + for(k=0; k < len-i; k++) sum += (rawData[k]-128)*(rawData[k+i]-128)/256; + // Serial.println(sum); + + // Peak Detect State Machine + if (pd_state == 2 && (sum-sum_old) <=0) + { + period = i; + pd_state = 3; + } + if (pd_state == 1 && (sum > thresh) && (sum-sum_old) > 0) pd_state = 2; + if (!i) { + thresh = sum * 0.5; + pd_state = 1; + } + } + // for(i=0; i < len; i++) Serial.println(rawData[i]); + // Frequency identified in Hz + if (thresh >100) { + freq_per = sample_freq/period; + Serial.println(freq_per); + } + count = 0; + } +} + + From 0f28d95e14bc7b96be4c6721497e71a8850b61a1 Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Fri, 14 Feb 2020 15:06:43 +0100 Subject: [PATCH 6/9] tuner funktionert mit tft --- main.ino | 62 +++++++++++++++++++++++++++++++------------------------ tuner.cpp | 12 ----------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/main.ino b/main.ino index 38bbdb3..4392581 100644 --- a/main.ino +++ b/main.ino @@ -1,36 +1,29 @@ -// Adafruit GFX Library - Version: Latest +#include +#include #include -#include -#include -#include - - -// Adafruit SSD1306 - Version: 2.1.0 #include -#include - // eigene Programm Dateien #include "tuner.h" -#define MODE 1 // 0 = COMPRESSOR / 1 = TUNER -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#pragma region DISPLAY -// DISPLAY #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) -Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -#pragma endregion +extern Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + + +#define MODE 1 // 0 = COMPRESSOR / 1 = TUNER + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#pragma region TUNER variables -// TUNER +#pragma region DISPLAY +// DISPLAY #pragma endregion + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // FUNCTIONS void setup() @@ -40,7 +33,7 @@ void setup() display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Set the text size and color. display.setTextSize(1); - display.setTextColor(WHITE); + display.setTextColor(SSD1306_WHITE); } @@ -53,9 +46,9 @@ void setup() void loop() { //Clear previous image. - display.clearDisplay(); if (MODE == 0){ //COMPRESSOR + display.clearDisplay(); byte poti_threshold = (analogRead(A1)/4); byte poti_ratio = (analogRead(A2)/4); @@ -63,18 +56,33 @@ void loop() drawCompSettings(poti_threshold,poti_ratio); } + display.display(); delay(100); } - display.setCursor(0, 10); - - if (MODE == 1){ //TUNER - tuner(); + //display.setCursor(0, 10); + + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // TUNER + if (MODE == 1){ + if ((micros() - startzeit) >= messzeit) + { + display.clearDisplay(); + float f = timer; //Datentyp 'float', wegen untenstehender Division + f = 1000000/f; //Aus Periodendauer Frequenz berechnen + detachInterrupt(0); + display.setCursor(0, 10); + display.print(f); + display.display(); + + attachInterrupt(digitalPinToInterrupt(2), Messung, RISING); + zaehler = 0; //Frequenzzähler zurücksetzen + startzeit = micros(); //Zeitpunkt der letzten Ausgabe speichern + } } - } @@ -94,12 +102,12 @@ void drawCompSettings (byte thresh, float ratio) { // draw Threshhold display.setCursor(SCREEN_WIDTH/2, 10); display.print(thresh); - display.fillTriangle(SCREEN_HEIGHT, SCREEN_HEIGHT, threshX, threshY, threshX, SCREEN_HEIGHT, WHITE); - display.fillRect(threshX, threshY, (128-threshX), (SCREEN_HEIGHT-threshY), WHITE); + display.fillTriangle(SCREEN_HEIGHT, SCREEN_HEIGHT, threshX, threshY, threshX, SCREEN_HEIGHT, SSD1306_WHITE); + display.fillRect(threshX, threshY, (128-threshX), (SCREEN_HEIGHT-threshY), SSD1306_WHITE); //calculate ratio ratio = (((float)ratio*19)/potiMax)+1; // Die 20 ist das Maximum aus dem Compressor Script // 19+1 damit der Minimal-Wert 1 ist display.setCursor(SCREEN_WIDTH/2, 20); display.print(ratio); - display.fillTriangle(threshX+1 , threshY-1, SCREEN_WIDTH, (threshY-((float)threshY/ratio)), SCREEN_WIDTH, threshY-1, WHITE); + display.fillTriangle(threshX+1 , threshY-1, SCREEN_WIDTH, (threshY-((float)threshY/ratio)), SCREEN_WIDTH, threshY-1, SSD1306_WHITE); } \ No newline at end of file diff --git a/tuner.cpp b/tuner.cpp index db52009..092b7ac 100644 --- a/tuner.cpp +++ b/tuner.cpp @@ -10,19 +10,7 @@ unsigned long messzeit = 1000000; void tuner (){ - if ((micros() - startzeit) >= messzeit) - { - float f = timer; //Datentyp 'float', wegen untenstehender Division - f = 1000000/f; //Aus Periodendauer Frequenz berechnen - detachInterrupt(0); - Serial.print("Messung: "); - Serial.println(f, 1); - - attachInterrupt(digitalPinToInterrupt(2), Messung, RISING); - zaehler = 0; //Frequenzzähler zurücksetzen - startzeit = micros(); //Zeitpunkt der letzten Ausgabe speichern - } } From 7e41de7c29c3a30c50a7e9dcc7754362e84039aa Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Fri, 14 Feb 2020 17:06:55 +0100 Subject: [PATCH 7/9] Ton erkennung funktioniert --- main.ino | 23 ++++++++++++++++++++--- tuner.cpp | 38 ++++++++++++++++++++++++++++++++------ tuner.h | 4 +++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/main.ino b/main.ino index 4392581..b74db59 100644 --- a/main.ino +++ b/main.ino @@ -69,12 +69,27 @@ void loop() { display.clearDisplay(); float f = timer; //Datentyp 'float', wegen untenstehender Division - f = 1000000/f; //Aus Periodendauer Frequenz berechnen - detachInterrupt(0); + f = (messzeit*4)/f; //Aus Periodendauer Frequenz berechnen + detachInterrupt(digitalPinToInterrupt(2)); + /////////////////////////////////////////////// display.setCursor(0, 10); display.print(f); + display.setCursor(0, 20); + + + float edgeL = 0; //set the lower edge of the spectrum + float edgeH = 0; //set the higher edge of the spectrum + for (int i=1; i < (sizeof(freq)/sizeof(freq[0]))-1; i++){ + // Mitte zwischen aktuellem Ton und unterem und oberem berechnen, und alls Abgrenzung setzen + edgeL = freq[i][0]-((freq[i][0]-freq[i-1][0])/2); + edgeH = freq[i][0]+((freq[i+1][0]-freq[i][0])/2); + if ((f > edgeL) and (f < edgeH)) { + + display.print(notes[(int)freq[i][1]]); + } + } display.display(); - + /////////////////////////////////////////////// attachInterrupt(digitalPinToInterrupt(2), Messung, RISING); zaehler = 0; //Frequenzzähler zurücksetzen startzeit = micros(); //Zeitpunkt der letzten Ausgabe speichern @@ -86,6 +101,8 @@ void loop() } + + void drawCompSettings (byte thresh, float ratio) { // Variable declaration #define potiMax 255 diff --git a/tuner.cpp b/tuner.cpp index 092b7ac..179f16e 100644 --- a/tuner.cpp +++ b/tuner.cpp @@ -5,13 +5,39 @@ int zaehler; unsigned long timer; unsigned long timerOld; unsigned long startzeit; -unsigned long messzeit = 1000000; +unsigned long messzeit = 1000000/4; - - -void tuner (){ - -} +// 0 1 2 3 4 5 6 +char notes[7] = {'C','D','E','F','G','A','B'}; +float freq[27][2] = { + {27.5, 5}, + {30.9, 6}, + {32.7, 0}, + {36.7, 1}, + {41.2, 2}, + {43.7, 3}, + {49.0, 4}, + {55.0, 5}, + {61.7, 6}, + {65.4, 0}, + {73.4, 1}, + {82.4, 2}, + {87.3, 3}, + {98.0, 4}, + {110.0,5}, + {123.4,6}, + {130.8,0}, + {146.8,1}, + {164.8,2}, + {174.6,3}, + {196.0,4}, + {220.0,5}, + {246.9,6}, + {261.6,0}, + {293.7,1}, + {329.6,2}, + {349.2,3} +}; void Messung() diff --git a/tuner.h b/tuner.h index 3a8edbf..de12545 100644 --- a/tuner.h +++ b/tuner.h @@ -7,9 +7,11 @@ extern unsigned long timerOld; extern unsigned long startzeit; extern unsigned long messzeit; -extern void tuner(); extern void Messung(); +extern char notes[7]; +extern float freq[27][2]; + #endif /* TUNER_H */ From 862feebd6e19893c11d5232058c01daac4d59289 Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Fri, 14 Feb 2020 17:32:22 +0100 Subject: [PATCH 8/9] Tuner Pin v1 --- main.ino | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/main.ino b/main.ino index b74db59..267d736 100644 --- a/main.ino +++ b/main.ino @@ -7,20 +7,23 @@ -#define SCREEN_WIDTH 128 // OLED display width, in pixels -#define SCREEN_HEIGHT 64 // OLED display height, in pixels -#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) -extern Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); +#define MODE 1 // 0 = COMPRESSOR / 1 = TUNER -#define MODE 1 // 0 = COMPRESSOR / 1 = TUNER +float edgeL = 0; //set the lower edge of the spectrum +float edgeH = 0; //set the higher edge of the spectrum +float f; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #pragma region DISPLAY // DISPLAY +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels +#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) +extern Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #pragma endregion @@ -38,11 +41,6 @@ void setup() - - - - - void loop() { //Clear previous image. @@ -68,7 +66,7 @@ void loop() if ((micros() - startzeit) >= messzeit) { display.clearDisplay(); - float f = timer; //Datentyp 'float', wegen untenstehender Division + f = timer; //Datentyp 'float', wegen untenstehender Division f = (messzeit*4)/f; //Aus Periodendauer Frequenz berechnen detachInterrupt(digitalPinToInterrupt(2)); /////////////////////////////////////////////// @@ -77,14 +75,12 @@ void loop() display.setCursor(0, 20); - float edgeL = 0; //set the lower edge of the spectrum - float edgeH = 0; //set the higher edge of the spectrum for (int i=1; i < (sizeof(freq)/sizeof(freq[0]))-1; i++){ // Mitte zwischen aktuellem Ton und unterem und oberem berechnen, und alls Abgrenzung setzen edgeL = freq[i][0]-((freq[i][0]-freq[i-1][0])/2); edgeH = freq[i][0]+((freq[i+1][0]-freq[i][0])/2); if ((f > edgeL) and (f < edgeH)) { - + drawTunerPin(); display.print(notes[(int)freq[i][1]]); } } @@ -102,6 +98,13 @@ void loop() +void drawTunerPin (){ + float range = edgeH - edgeL; + float frqInRng = f - edgeL; + int coordX = frqInRng * 128 / range; + display.drawFastVLine(coordX, 20, 30, SSD1306_WHITE); +} + void drawCompSettings (byte thresh, float ratio) { // Variable declaration From 2ec37b400a5bccec41a7c7921982f5131e5daea2 Mon Sep 17 00:00:00 2001 From: Andy Brunner Date: Sat, 15 Feb 2020 09:58:44 +0100 Subject: [PATCH 9/9] =?UTF-8?q?average=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 42 ++++++++++++++++++++++++++++++++++++++++++ main.ino | 3 ++- tuner.cpp | 21 ++++++++++++++++++--- tuner.h | 6 +++--- 4 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8435089 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,42 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Arduino", + "type": "arduino", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "MIMode": "gdb", + "targetArchitecture": "arm", + "miDebuggerPath": "", + "debugServerPath": "", + "debugServerArgs": "", + "customLaunchSetupCommands": [ + { + "text": "target remote localhost:3333" + }, + { + "text": "file \"${file}\"" + }, + { + "text": "load" + }, + { + "text": "monitor reset halt" + }, + { + "text": "monitor reset init" + } + ], + "stopAtEntry": true, + "serverStarted": "Info\\ :\\ [\\w\\d\\.]*:\\ hardware", + "launchCompleteCommand": "exec-continue", + "filterStderr": true, + "args": [] + } + ] +} \ No newline at end of file diff --git a/main.ino b/main.ino index 267d736..7a3326b 100644 --- a/main.ino +++ b/main.ino @@ -70,8 +70,9 @@ void loop() f = (messzeit*4)/f; //Aus Periodendauer Frequenz berechnen detachInterrupt(digitalPinToInterrupt(2)); /////////////////////////////////////////////// + float avrgFreq = getAvrgFreq(f); display.setCursor(0, 10); - display.print(f); + display.print(avrgFreq); display.setCursor(0, 20); diff --git a/tuner.cpp b/tuner.cpp index 179f16e..804aaa6 100644 --- a/tuner.cpp +++ b/tuner.cpp @@ -1,11 +1,12 @@ #include "tuner.h" #include "Arduino.h" -int zaehler; -unsigned long timer; -unsigned long timerOld; + +int zaehler,zAvrg=0; +unsigned long timer, timerOld; unsigned long startzeit; unsigned long messzeit = 1000000/4; +float frqColl[5]; // 0 1 2 3 4 5 6 char notes[7] = {'C','D','E','F','G','A','B'}; @@ -46,3 +47,17 @@ void Messung() timer = micros() - timerOld; timerOld = micros(); } + + +float getAvrgFreq(float fA){ + if(zAvrg>=5)zAvrg=0; //Zähler zurücksetzen + if(isinf(fA))fA=0; //um fehlerhafte Kalkulation zu vermeiden + frqColl[zAvrg] = fA; + zAvrg++; + + float avrg=0.00f; + for (int i=0; i < 5; i++){ + avrg = avrg + frqColl[i]; + } + return (avrg/5); +} \ No newline at end of file diff --git a/tuner.h b/tuner.h index de12545..f9d946b 100644 --- a/tuner.h +++ b/tuner.h @@ -7,12 +7,12 @@ extern unsigned long timerOld; extern unsigned long startzeit; extern unsigned long messzeit; -extern void Messung(); +extern void Messung(); +extern float getAvrgFreq(float); -extern char notes[7]; +extern char notes[7]; extern float freq[27][2]; - #endif /* TUNER_H */