Created
June 4, 2012 14:38
-
-
Save mia-0032/2868789 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
//servo | |
#include <Servo.h> | |
#define SERVO_PIN 9 | |
//negi furi | |
#define DEFAULT_DEG 40 | |
#define NEGIFURI_DEG 70 | |
#define NEGIFURI_WAIT 200 | |
#define NEGIFURI_WAIT_DELAY 400 | |
//spectrum shield | |
#define NOISE_INPUT 160 | |
#define SPECTRUM_RESET_PIN 5 | |
#define SPECTRUM_STROBE_PIN 4 | |
//mode switch | |
#define MODE_SWITCH_PIN 8 | |
//coin sensor | |
#define COIN_SENSOR_PIN 10 | |
Servo servo;//サーボのインスタンス | |
const int inputPins[2] = {0, 1}; | |
int Spectrum[2][7]; | |
int currentMax = 10; | |
int lastLevel = 10; | |
// Read 7 band equalizer. | |
void readSpectrum() | |
{ | |
for(int i=0; i<=1; i++){ | |
for(int band=0; band <7; band++){ | |
Spectrum[i][band] = (analogRead(inputPins[i]) + analogRead(inputPins[i]) )/2; //Read twice and take the average by dividing by 2 | |
digitalWrite(SPECTRUM_STROBE_PIN,HIGH); | |
digitalWrite(SPECTRUM_STROBE_PIN,LOW); | |
} | |
} | |
} | |
void negiFuri(){ | |
servo.write(NEGIFURI_DEG); | |
delay(NEGIFURI_WAIT); | |
servo.write(DEFAULT_DEG); | |
delay(NEGIFURI_WAIT_DELAY); | |
} | |
void setup(){ | |
Serial.begin(9600); | |
servo.attach(SERVO_PIN); | |
servo.write(DEFAULT_DEG); | |
pinMode(SPECTRUM_RESET_PIN, OUTPUT); | |
pinMode(SPECTRUM_STROBE_PIN, OUTPUT); | |
//Init spectrum analyzer | |
digitalWrite(SPECTRUM_STROBE_PIN,LOW); | |
delay(1); | |
digitalWrite(SPECTRUM_RESET_PIN,HIGH); | |
delay(1); | |
digitalWrite(SPECTRUM_STROBE_PIN,HIGH); | |
delay(1); | |
digitalWrite(SPECTRUM_STROBE_PIN,LOW); | |
delay(1); | |
digitalWrite(SPECTRUM_RESET_PIN,LOW); | |
delay(5); | |
pinMode(MODE_SWITCH_PIN, INPUT); | |
pinMode(COIN_SENSOR_PIN, INPUT); | |
} | |
void spectrumMode(){ | |
readSpectrum(); | |
int input_base = (int)(Spectrum[0][0] + Spectrum[1][0]); | |
if(input_base < 0){ | |
input_base = 0; | |
} | |
if(input_base > currentMax){ | |
currentMax = input_base; | |
} | |
Serial.print(input_base); | |
if(input_base >= currentMax * 0.6){ | |
negiFuri(); | |
} | |
} | |
void coinSensorMode(){ | |
if(digitalRead(COIN_SENSOR_PIN) == HIGH){ | |
negiFuri(); | |
} | |
} | |
void loop(){ | |
if(digitalRead(MODE_SWITCH_PIN) == LOW){ | |
spectrumMode(); | |
}else{ | |
coinSensorMode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment