近づくと光と音で警告します。
フルカラーLEDで通常は青、反応すると赤、しばらくは緑が点灯します。
LEDが赤で点灯中は振動モーターで鳴ってくれます。
ケースは丸く作ったのですが思い直して両側を削って作りました。
のっかってる赤トンボは愛嬌です(頭でバランスがとれてます)。
こんな感じで動きます。
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
int vibPin = 12; | |
int gPin = 11; | |
int rPin = 13; | |
int bPin = 10; | |
long start; | |
boolean ledOn = false; | |
int count = 0; | |
long tSnd = -1; | |
int sndLevel = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(bPin, OUTPUT); | |
pinMode(gPin, OUTPUT); | |
pinMode(rPin, OUTPUT); | |
pinMode(vibPin, OUTPUT); | |
// HIGH = off, LOW = on | |
digitalWrite(bPin, HIGH); | |
digitalWrite(rPin, HIGH); | |
digitalWrite(gPin, HIGH); | |
start = millis(); | |
} | |
boolean irOn(int ir) { | |
// Serial.println(ir); | |
return 50 < ir; | |
} | |
boolean micOn(int mic) { | |
Serial.println(mic); | |
if (sndLevel == 0) { | |
sndLevel = mic; | |
return false; | |
} | |
return 100 < mic - sndLevel; | |
} | |
void loop() { | |
int ir = analogRead(A0); | |
int mic = analogRead(A1); | |
long now = millis(); | |
if ( irOn(ir) || now - tSnd < 3000 || micOn(mic)) { | |
if (100 < now - start) { | |
ledOn = !ledOn; | |
if (micOn(mic)) { | |
tSnd = now; | |
} | |
start = now; | |
} | |
count = 0; | |
} else if ((800 < now - start) && (count <= 10)) { | |
ledOn = !ledOn; | |
count++; | |
start = now; | |
} else if (1700 < now - start) { | |
ledOn = !ledOn; | |
start = now; | |
} | |
if (irOn(ir) || now - tSnd < 3000 || micOn(mic)) { | |
digitalWrite(vibPin, HIGH); | |
} else { | |
digitalWrite(vibPin, LOW); | |
} | |
if (ledOn) { | |
if (irOn(ir) || now - tSnd < 3000 || micOn(mic)) { | |
digitalWrite(rPin, LOW); | |
} else { | |
if (10 < count) { | |
digitalWrite(bPin, LOW); | |
} else { | |
digitalWrite(gPin, LOW); | |
} | |
} | |
} else { | |
digitalWrite(bPin, HIGH); | |
digitalWrite(rPin, HIGH); | |
digitalWrite(gPin, HIGH); | |
} | |
delay(10); | |
} |