Last active
June 28, 2016 14:00
-
-
Save dakrawczyk/473bc3975e2317fd1cb985d2f9f711d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 index = 0; | |
bool isPresent = false; | |
#define THRESHOLD 50 | |
#define BUFFSIZE 25 | |
int buffer[BUFFSIZE]; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(A0,INPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
buffer[idx] = map(analogRead(A0),0,800,0,1024); | |
idx= (idx + 1) % BUFFSIZE; | |
if ((average() < THRESHOLD) && isPresent) | |
{ | |
Serial.println("Nobody around"); | |
isPresent = false; | |
} | |
if ((average() >= THRESHOLD) && !isPresent) | |
{ | |
Serial.println("Somebody Detected!!"); | |
isPresent = true; | |
} | |
Serial.println(average()); | |
delay(100); | |
} | |
int average() | |
{ | |
int sum = 0; | |
for (int i=0; i<BUFFSIZE; i++) | |
{ | |
sum = sum + buffer[i]; | |
} | |
return sum / BUFFSIZE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment