Components and supplies
Knob, Flatted Shaft
Disused Ear Phones
Unicorn HAT
Femail Jack
OLED Display 128x32
Plastic Enclosure, Junction Box
Arduino Nano R3
Rotary Encoder with Push-Button
Push Button normally open
Tools and machines
Drill / Driver, Cordless
Project description
Code
Audiometric Testing Device
arduino
It's a device used to test your hearing
1#include <Volume.h> 2#include <RotaryEncoder.h> 3#include <Adafruit_NeoPixel.h> 4#include <OneButton.h> 5#include <U8glib.h> 6 7#define PIN 8 8Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800); 9U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); // I2C / TWI 10 11Volume vol; 12// Change these two numbers to the pins connected to your encoder. 13// Best Performance: both pins have interrupt capability 14// Good Performance: only the first pin has interrupt capability 15// Low Performance: neither pin has interrupt capability 16 17// ----- Rotary settings here ---- 18#define ROTARYSTEPS 1 19#define ROTARYMIN 1 20#define ROTARYMAX 8 21int lastPos = 0; 22int exitFlag = 0; 23// Setup a RoraryEncoder for pins A2 and A3: 24 RotaryEncoder encoder(A2, A3); 25 26// Last known rotary position. 27// Setup a new OneButton on pin A1. 28OneButton button(A1, true); 29 30int risultati[3][8]; 31int j=0; 32int x=0; 33int y=0; 34int orecchio=1; 35int pos = 1; 36char db[9][10]= 37{ "0 db", 38 "10 db", 39 "20 db", 40 "30 db", 41 "40 db", 42 "50 db", 43 "60 db", 44 "70 db", 45 "80 db" 46}; 47char freq[8][10] 48{ "125 Hz", 49 "250 Hz", 50 "500 Hz", 51 "1000 Hz", 52 "2000 Hz", 53 "3000 Hz", 54 "4000 Hz", 55 "8000 Hz" 56}; 57 58int freqn[8][2] 59{ 60 125, 250, 500, 1000, 2000, 3000, 4000, 8000 61}; 62 63char ear[3][11] 64{ "Volume:", 65 "Vol Left:", 66 "Vol Righ:" 67}; 68 69 70 71 72void setup() { 73 strip.begin(); 74 strip.show(); // Initialize all pixels to 'off' 75 76 encoder.setPosition(0 / ROTARYSTEPS); // start with the value of 0. 77 78 u8g.setColorIndex(1); // pixel on for Display 79 80 button.attachLongPressStop(longPressStop); 81 82 83 vol.begin(); // After calling this, delay() and delayMicroseconds will no longer work 84 // correctly! Instead, use vol.delay() and vol.delayMicroseconds() for 85 // the correct timing 86 87 vol.setMasterVolume(3.00); // Self-explanatory enough, right? Try lowering this value if the speaker is too loud! (0.00 - 1.00) 88 vol.delay(500); 89 90 Serial.begin(115200); 91 Serial.println("Volume test with Encoder:"); 92 93} 94 95 96void loop() { 97 98 button.tick(); 99 100 encoder.tick(); 101 102 int newPos = encoder.getPosition(); 103 if (pos != newPos) { 104 105 if (newPos < ROTARYMIN) { 106 encoder.setPosition(ROTARYMIN / ROTARYSTEPS); 107 newPos = ROTARYMIN; 108 } else if (newPos > ROTARYMAX) { 109 encoder.setPosition(ROTARYMAX / ROTARYSTEPS); 110 newPos = ROTARYMAX; 111 } 112 113 Serial.print(newPos); 114 Serial.print(", "); 115 Serial.print(x); 116 Serial.println(); 117 118 if (orecchio < 3){ 119 u8g.firstPage(); 120 do { 121 draw(ear[orecchio], 0, 12); 122 draw("Freq:", 0, 30); 123 draw(db[newPos], 80, 12); 124 draw(freq[x], 50, 30); 125 } while( u8g.nextPage() ); 126 } else { 127 u8g.firstPage(); 128 do { 129 draw(" FINAL GRAPH ", 0, 12); 130 draw(" EXECUTED ", 0, 32); 131 } while( u8g.nextPage() ); 132 } 133 134 pos = newPos; 135 vol.tone (freqn[x],pos); 136 137 if (orecchio < 3) 138 for (j=(x*8); j<64;j++) 139 strip.setPixelColor(j, 0, 0, 0); 140 strip.show(); 141 142 if ((x==0 or x==2 or x==4 or x==6) and orecchio==1) 143 strip.setPixelColor((pos-1)+(x*8), 10, 0, 0); 144 if ((x==1 or x==3 or x==5 or x==7) and orecchio==1) 145 strip.setPixelColor(((x+1)*8)-(pos), 10, 0, 0); 146 if ((x==0 or x==2 or x==4 or x==6) and orecchio==2) 147 strip.setPixelColor((pos-1)+(x*8), 0, 0, 10); 148 if ((x==1 or x==3 or x==5 or x==7) and orecchio==2) 149 strip.setPixelColor(((x+1)*8)-(pos), 0, 0, 10); 150 strip.show(); 151 } 152 153// vol.delay(10); // solo per test 154 155} 156 157 158 159// This function will be called once, when the button1 is released after beeing pressed for a long time. 160void longPressStop() { 161 Serial.print("Button 1 longPress stop, x="); 162// scanf("%d", &risultati[orecchio][pos]); 163 risultati[orecchio][x]=pos; 164 x=x+1; 165 pos=0; 166 encoder.setPosition(1); 167 if (x>7 and orecchio == 1) { 168 x=0; 169 orecchio =2; 170 } 171 if (x>7 and orecchio == 2) { 172 u8g.firstPage(); 173 do { 174 draw(" PLEASE WAIT ", 0, 12); 175 draw(" FINAL GRAPH ", 0, 32); 176 } while( u8g.nextPage() ); 177 strip.clear(); 178 for (j=1; j<3;j++) { 179 Serial.println(); 180 for (x=0; x<8;x++) { 181 if ((x==0 or x==2 or x==4 or x==6) and j==1) 182 strip.setPixelColor((risultati[j][x]-1)+(x*8), 10, 0, 0); 183 if ((x==1 or x==3 or x==5 or x==7) and j==1) 184 strip.setPixelColor(((x+1)*8)-(risultati[j][x]), 10, 0, 0); 185 if ((x==0 or x==2 or x==4 or x==6) and j==2) 186 if (strip.getPixelColor((risultati[j][x]-1)+(x*8)) == 0) 187 strip.setPixelColor((risultati[j][x]-1)+(x*8), 0, 0, 10); 188 else 189 strip.setPixelColor((risultati[j][x]-1)+(x*8), 10, 0, 10); 190 if ((x==1 or x==3 or x==5 or x==7) and j==2) 191 if (strip.getPixelColor(((x+1)*8)-(risultati[j][x])) == 0) 192 strip.setPixelColor(((x+1)*8)-(risultati[j][x]), 0, 0, 10); 193 else 194 strip.setPixelColor(((x+1)*8)-(risultati[j][x]), 10, 0, 10); 195 strip.show(); 196 Serial.print(risultati[j][x]); 197 Serial.print(", "); 198 vol.delay(1000); 199 } 200 201 } 202 x=0; 203 orecchio = 3; 204 } 205 206 Serial.println(x); 207 Serial.print("Ear = "); 208 Serial.println(orecchio); 209 210} // longPressStop1 211 212void draw(char* parola, int posx, int posy) { 213 // graphic commands to redraw the complete screen should be placed here 214 u8g.setFont(u8g_font_unifont); 215 //u8g.setFont(u8g_font_osb21); 216 u8g.drawStr( posx, posy, parola); 217} 218
Downloadable files
Arduino Audiometric Device
It'a a device that can be used to test your hearing
Arduino Audiometric Device
Arduino Audiometric Device
It'a a device that can be used to test your hearing
Arduino Audiometric Device
Comments
Only logged in users can leave comments
cstram
0 Followers
•0 Projects
Table of contents
Intro
5
0