Last active
October 16, 2015 15:57
-
-
Save Wei1234c/ec0401aeb224aa314cdd to your computer and use it in GitHub Desktop.
Shot Zombie
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
// http://www.mobile01.com/topicdetail.php?f=37&t=3973404 | |
#define columnCount 3 | |
#define intervalScanning 20 | |
#define holdForTrigger 50 | |
#define thresholdDoorbell 500 | |
byte pinDoorBells[columnCount] = {23, 24, 25}; | |
byte pinTriggers[columnCount] = {2, 3, 4}; | |
int doorBellValues[columnCount]; | |
bool hasVistor = false; | |
byte doorBellWhichRings; | |
void setup() { | |
Serial.begin(9600); | |
for (byte i = 0; i < columnCount; i++) { | |
pinMode(pinTriggers[i], OUTPUT); | |
digitalWrite(pinTriggers[i], LOW); | |
} | |
} | |
void loop() { | |
checkDoorbells(); | |
if (weHaveVisitors()) { | |
shoot(pinTriggers[doorBellWhichRings]); | |
} | |
} | |
byte checkDoorbells() { | |
// 讀取三個 doorbells的數值 | |
delay(intervalScanning); | |
Serial.print("Doorbell values: "); | |
for (byte i = 0; i < columnCount; i++) { | |
doorBellValues[i] = analogRead(pinDoorBells[i]); | |
Serial.print(doorBellValues[i]); | |
Serial.print(", "); | |
} | |
Serial.println(""); | |
// 找出最大值,並觸發對應的trigger | |
doorBellWhichRings = 0; | |
for (byte i = 0; i < columnCount; i++) { | |
if (doorBellValues[i] > doorBellValues[doorBellWhichRings]) { | |
doorBellWhichRings = i; | |
} | |
} | |
return doorBellWhichRings; | |
} | |
bool weHaveVisitors() { | |
// 如果沒有doorbell超過threshold,則等待 | |
hasVistor = false; | |
for (byte i = 0; i < columnCount; i++) { | |
if (doorBellValues[i] >= thresholdDoorbell) { | |
hasVistor = true; | |
} | |
} | |
return hasVistor; | |
} | |
void shoot(byte pinTrigger) { | |
digitalWrite(pinTrigger, HIGH); | |
delay(holdForTrigger); | |
digitalWrite(pinTrigger, LOW); | |
delay(holdForTrigger); | |
Serial.print("Shoot: "); | |
Serial.print(doorBellWhichRings); | |
Serial.print(" - "); | |
Serial.println(pinTrigger); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment