Created
January 10, 2017 18:33
-
-
Save Miceuz/a703e4f20fb41356d8257724434a4bdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Bounce2.h> | |
Bounce but1 = Bounce(); | |
Bounce but2 = Bounce(); | |
byte outPin[8] = {2,3,5,6, 8,9,11,12}; // kintamuju masyvas nepaeiliui pasiskirsciusiems pin'ams suzymeti | |
byte outNo = 0; // ciklams prasukti | |
int input; // nuskaityto analoginio iejimo reiksmei irasineti | |
// int lygis = 0; // lygis arba kitaip tariant | |
int koefi = 10; // analoginio signalo iejimo sriprinimo koeficientas (kartais). Leisgtinos ribos 10...50 (bet jei nepakaks, galima meginti ir iki 200). | |
int rfrsh = 50; // refresh rate - sis skaicius nusako kas kiek milisekundziu yra atnaujinamas VU-meterio parodymas | |
void setup() | |
{ | |
while(outNo <= 7) // prasuku "while" ciklaa astuonis kartus ir jis... | |
{ | |
pinMode(outPin[outNo], OUTPUT); // ...jis kiekviename cikle istraukia is masyvo "outPin[]" arduino kiekvieno pin'o isejimo individualu numeri (2,3,5...) | |
outNo++; | |
} | |
pinMode(A0, INPUT); // analoginis iejimas. dabar, rasydamas koda namie ir neturedamas nei schemos nei aparato, nepamenu i kuri ten paduodu garsa, pasiziurek pats ir pakeisk sita, bei kitus A0, i atitinkama | |
pinMode(A4, INPUT_PULLUP); // skaitmeninis iejimas. cia irgi nepamenu, kazkurie is ju dvieju yra iejimai mygtukams. Ateiciai... | |
pinMode(A5, INPUT_PULLUP); // skaitmeninis iejimas. cia irgi nepamenu, kazkurie is ju dvieju yra iejimai mygtukams. Ateiciai... | |
but1.attach(A4); | |
but1.interval(5); // interval in ms | |
but2.attach(A5); | |
but2.interval(5); // interval in ms | |
Serial.begin(9600); | |
} | |
void allOff() { | |
for(int i = 0; i < 8; i++) { | |
digitalWrite(outPin[i], LOW); | |
} | |
} | |
void allOn() { | |
for(int i = 0; i < 8; i++) { | |
digitalWrite(outPin[i], HIGH); | |
} | |
} | |
void bajeris() { | |
allOff(); | |
for(int i = 0; i < 8; i++) { | |
allOff(); | |
digitalWrite(outPin[i], HIGH); | |
delay(100); | |
} | |
for(int i = 7; i >= 0; i--) { | |
allOff(); | |
digitalWrite(outPin[i], HIGH); | |
delay(100); | |
} | |
} | |
void dimm(byte level) { | |
for(int i = 0; i < 7; i++) { | |
digitalWrite(outPin[i], HIGH); | |
} | |
delayMicroseconds(level); | |
for(int i = 0; i < 7; i++) { | |
digitalWrite(outPin[i], LOW); | |
} | |
delayMicroseconds(255 - level); | |
} | |
byte level = 1, fadeDir = 1; | |
unsigned long fadeTs = 0; | |
#define FADE_DELAY 100 | |
byte loops = 0; | |
void fadeInOut() { | |
if(loops++ > 100 ) { | |
if(1 == fadeDir) { | |
level++; | |
if(level > 254) { | |
fadeDir = 0; | |
} | |
} else { | |
level--; | |
if(level < 1) { | |
fadeDir = 1; | |
} | |
} | |
loops = 0; | |
} | |
dimm(level); | |
} | |
byte mode = 0, onOff = 0;; | |
byte isCalibration = 1; | |
unsigned long lastCalibration = 0; | |
int minVal = 1024, maxVal = 0; | |
void vuMeter() { | |
if(input < minVal) { | |
minVal = input; | |
} else if(input > maxVal) { | |
maxVal = input; | |
} | |
input = map(input, minVal, maxVal, 0, 8); | |
for(int i = 0; i < 8; i++) { | |
if(i < input) { | |
digitalWrite(outPin[i], HIGH); | |
} else { | |
digitalWrite(outPin[i], LOW); | |
} | |
} | |
} | |
void loop() { | |
input = analogRead(A0); // nuskaitau analogini garso moduliacijos iejima ir ji irasau i "input" kintamaajii | |
if(but2.update() && but2.fell()) { | |
if(0 == mode) { | |
mode = 1; | |
} else { | |
mode = 0; | |
} | |
} | |
if(but1.update() && but1.fell()) { | |
if(0 == onOff) { | |
onOff = 1; | |
minVal = 1024; | |
maxVal = 0; | |
bajeris(); | |
} else { | |
allOff(); | |
onOff = 0; | |
} | |
} | |
if(1 == onOff) { | |
if(0 == mode) { | |
vuMeter(); | |
} else { | |
fadeInOut(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment