/*
Hair-o-rama
Chris Kairalla
*/
#define smooth 1 //2 smooths the last two nums, 3 smooths the last 3...
int pullOldReading = 0; // variable to hold the old analog value for hair pull
int pullValueSmooth = 0;
int thresh = 20; //set this variable to fine tune when hair is getting pulled
int smoothArray[smooth];
boolean ouch = false; //if hair is pulled then ouch is flipped to true
// *******SETUP********
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
// ********LOOP*********
void loop() {
addToArray(); //adds latest reading to the top of the array
int pullSmoothReading = findAverage(); //finds average of all nums in array
if (pullSmoothReading > 15){ //only pay attention to readings above 15
int diff = pullSmoothReading - pullOldReading;
if (diff > thresh or diff< thresh*-1){ //determine if hair is pulled
Serial.print(33, BYTE); //sends exclaimation point
ouch = true;
} else {
Serial.print(46, BYTE); //sends period
ouch = false;
}
Serial.print(smoothReading);
Serial.print(44, BYTE); //print comma
}
pullOldReading = pullSmoothReading;
}
//*** ADD LATEST READING TO ARRAY ***
void addToArray(){
for (int i = smooth-1; i >= 1; i--){
smoothArray[i] = smoothArray[i-1]; //shift every num up one slot
}
smoothArray[0] = analogRead(0); //add latest reading into slot 0
}
//finds the average of all the values in the array
int findAverage(){
int average = 0;
for (int i = 0; i < smooth; i++){
average += smoothArray[i];
}
average = average / smooth;
return average;
}