Created
June 4, 2012 14:36
-
-
Save mia-0032/2868770 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
//入力のピン | |
const int inputPins[2] = {0, 1}; | |
//出力のピン | |
const int redPins[2] = {3, 6}; | |
const int greenPins[2] = {5, 10}; | |
const int bluePins[2] = {11, 9}; | |
//入力ノイズ値 | |
#define NOISE_INPUT 20 | |
//入力の電圧を取得する関数 | |
int getVolume(int); | |
//入力値 | |
int input; | |
//通常のときに各LEDの明るさを計算する関数 | |
int getRedBrightness(int); | |
int getGreenBrightness(int); | |
int getBlueBrightness(int); | |
//現在の電圧最大値を保存。 | |
int currentMaxInput = 0; | |
//2乗の値も保存 | |
float currentMaxInputSquare = 0; | |
//各色の出力値 | |
int red; | |
int green; | |
int blue; | |
//前回の各LEDの出力値を保存 | |
int lastRedLevels[2] = {0, 0}; | |
int lastBlueLevels[2] = {0, 0}; | |
int lastGreenLevels[2] = {0, 0}; | |
void setup() | |
{ | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
//音声入力が小さくなるとそのうち追随したいので。 | |
randomSeed(analogRead(3)); | |
if(random(100) == 0){ | |
currentMaxInput--; | |
} | |
for(int i=0; i < 2; i++){ | |
input = getVolume(inputPins[i]); | |
// Serial.println(currentMaxInput); | |
currentMaxInputSquare = pow(currentMaxInput, 2); | |
// Serial.println(currentMaxInputSquare); | |
red = getRedBrightness(input); | |
green = getGreenBrightness(input); | |
blue = getBlueBrightness(input); | |
red = (int)((lastRedLevels[i] + red) / 2); | |
analogWrite(redPins[i], red); | |
lastRedLevels[i] = red; | |
green = (int)((lastGreenLevels[i] + green) / 2); | |
analogWrite(greenPins[i], green); | |
lastGreenLevels[i] = green; | |
blue = (int)((lastBlueLevels[i] + blue) / 2); | |
analogWrite(bluePins[i], blue); | |
lastBlueLevels[i] = blue; | |
// Serial.println(red); | |
// Serial.println(blue); | |
// Serial.println(green); | |
} | |
} | |
/** | |
* 入力ピンを指定すると音の振幅を取得する。 | |
**/ | |
int getVolume(int analogPin){ | |
int pinInput = analogRead(analogPin); | |
// Serial.println(pinInput); | |
int revisedInput = abs(pinInput - 512); | |
// Serial.println(revisedInput); | |
//noise | |
if(revisedInput < NOISE_INPUT){ | |
revisedInput = 0; | |
} | |
//max input update | |
if(revisedInput > currentMaxInput - 1){ | |
currentMaxInput = revisedInput + 1; | |
} | |
return revisedInput; | |
} | |
/******************************************* | |
* ここから明るさ計算式 | |
********************************************/ | |
/** | |
* 振幅を入力すると赤色のLEDの明るさを計算する。 | |
**/ | |
int getRedBrightness(int revisedInput){ | |
float val_1 = revisedInput - 0.6 * currentMaxInput; | |
float val_2 = revisedInput - 1.4 * currentMaxInput; | |
float red_val = -4 * 255 / currentMaxInputSquare * val_1 * val_2; | |
// Serial.println(red_val); | |
red_val = constrain(red_val, 0, 255); | |
return (int) red_val; | |
} | |
/** | |
* 振幅を入力すると緑色のLEDの明るさを計算する。 | |
**/ | |
int getGreenBrightness(int revisedInput){ | |
float val_1 = revisedInput - 0.6 * currentMaxInput; | |
float val_2 = revisedInput - 0.2 * currentMaxInput; | |
float green_val = -4 * 255 / currentMaxInputSquare * val_1 * val_2; | |
green_val = constrain(green_val, 0, 205); | |
return (int) green_val; | |
} | |
/** | |
* 振幅を入力すると青色のLEDの明るさを計算する。 | |
**/ | |
int getBlueBrightness(int revisedInput){ | |
float val_1 = revisedInput + 0.2 * currentMaxInput; | |
float val_2 = revisedInput - 0.2 * currentMaxInput; | |
float blue_val = -4 * 255 / currentMaxInputSquare * val_1 * val_2; | |
blue_val = constrain(blue_val, 0, 255); | |
return (int) blue_val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment