Skip to content

Commit

Permalink
Merge pull request #2 from TheBookThief/feature/tuner
Browse files Browse the repository at this point in the history
Feature/tuner
  • Loading branch information
baartch authored Feb 15, 2020
2 parents c1230f4 + 2ec37b4 commit e8738a5
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"board": "arduino:avr:nano",
"configuration": "cpu=atmega328",
"sketch": "main.ino",
"port": "COM4",
"output": "../ArduinoOutput"
}
16 changes: 16 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
50 changes: 50 additions & 0 deletions freq.ino_
Original file line number Diff line number Diff line change
@@ -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();
}
110 changes: 86 additions & 24 deletions main.ino
Original file line number Diff line number Diff line change
@@ -1,47 +1,109 @@
// Adafruit GFX Library - Version: Latest
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
// eigene Programm Dateien
#include "tuner.h"



// Adafruit SSD1306 - Version: 2.1.0
#include <Adafruit_SSD1306.h>
#include <splash.h>

#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)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

extern Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#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);
display.setTextColor(SSD1306_WHITE);
}



void loop()
{
byte poti_threshold = (analogRead(A0)/4);
byte poti_ratio = (analogRead(A1)/4);
float audio_in = analogRead(A2)*(5.0 / 1023.0);

//Clear previous image.
display.clearDisplay();
display.setCursor(0, 10);
display.print(audio_in);

if (poti_threshold > 0){
drawCompSettings(poti_threshold,poti_ratio);
if (MODE == 0){ //COMPRESSOR
display.clearDisplay();
byte poti_threshold = (analogRead(A1)/4);
byte poti_ratio = (analogRead(A2)/4);

if (poti_threshold > 0){
drawCompSettings(poti_threshold,poti_ratio);
}

display.display();
delay(100);
}

display.display();
delay(30);
//display.setCursor(0, 10);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TUNER
if (MODE == 1){
if ((micros() - startzeit) >= messzeit)
{
display.clearDisplay();
f = timer; //Datentyp 'float', wegen untenstehender Division
f = (messzeit*4)/f; //Aus Periodendauer Frequenz berechnen
detachInterrupt(digitalPinToInterrupt(2));
///////////////////////////////////////////////
float avrgFreq = getAvrgFreq(f);
display.setCursor(0, 10);
display.print(avrgFreq);
display.setCursor(0, 20);


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]]);
}
}
display.display();
///////////////////////////////////////////////
attachInterrupt(digitalPinToInterrupt(2), Messung, RISING);
zaehler = 0; //Frequenzzähler zurücksetzen
startzeit = micros(); //Zeitpunkt der letzten Ausgabe speichern
}
}



}



void drawTunerPin (){
float range = edgeH - edgeL;
float frqInRng = f - edgeL;
int coordX = frqInRng * 128 / range;
display.drawFastVLine(coordX, 20, 30, SSD1306_WHITE);
}


Expand All @@ -61,12 +123,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);
}
63 changes: 63 additions & 0 deletions tuner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "tuner.h"
#include "Arduino.h"


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'};
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()
{
zaehler++;
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);
}
18 changes: 18 additions & 0 deletions tuner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#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 Messung();
extern float getAvrgFreq(float);

extern char notes[7];
extern float freq[27][2];

#endif /* TUNER_H */


Loading

0 comments on commit e8738a5

Please sign in to comment.