Final: Serenity Hat
For my final project for Rest of You, I built a hat that helps you maintain serenity by sensing when your expression changes from placid to animated and vibrates a soothing motor at the base of your skull to remind you to maintain your cool. Photos and additional documentation to come.
Arduino Code
/* Serenity Hat
By Kate Monahan
December 2008 */
int stretchPin = 0; // Analog input pin that the stretch sensor is attached to
int switchPin = 2; // Analog input pin that the reset switch is attached to
int stretchValue = 0; // value read from the photoresistor
int switchState = 0; //set the initial switch state to off
int vibPin = 9;
boolean isVibrating = false;
int difference =0;
#define NUMREADINGS 10
int timer = 0; // reset the baseline value each time the switch is thrown
int baselineValue=0; // the neutral expression (at-rest strech sensor value)
int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(vibPin, OUTPUT);
pinMode(switchPin, INPUT); // set the switch pin to be an input
for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
}
void loop() {
switchState = digitalRead(switchPin);
stretchValue = analogRead(stretchPin); // read the stretch sensor value
//Serial.println(stretchValue); // print the stretch sensor value
//Serial.println(switchState);
//Serial.println(baselineValue);
vibrate();
total -= readings[index]; // subtract the last reading
readings[index] = analogRead(stretchPin); // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index
if (index >= NUMREADINGS) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning
average = total / NUMREADINGS; // calculate the average
Serial.println(average); // send it to the computer (as ASCII digits)
if (timer<10){
baselineValue=stretchValue;
timer++;
}
if (switchState == 0){ //reset if switch is turned off
timer = 0;
baselineValue=0;
}
delay(10); // wait 10 milliseconds before the next loop
}
void vibrate(){
difference = (baselineValue-average); //set the sensitivity
if (difference < 0){
difference = (difference * -1); //make all sensitivity values are positive
}
Serial.println(difference);
if (switchState == 1) { //if the switch is on, proceed
if(difference > 7 && isVibrating == false){
//activate the vibrating motor if a change in expression is detected
digitalWrite(vibPin, HIGH);
isVibrating = true;
}
if(difference <=7 && isVibrating == true) {
//deactivate the motor if the expression returns to neutral
digitalWrite(vibPin, LOW);
isVibrating = false;
}
}
else {
digitalWrite(vibPin, LOW);
isVibrating = false;
}
}