Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *DeskPal - Persuasive Desktop Companion
- *Making Things Interactive Fall 2010 final project
- *Author: Clifton Lin
- *Date: NOV 2010
- *Related
- */
- #define IDLE 1
- #define NORMAL 2
- #define HAPPY 3
- #define BORED 4
- #define SAD 5
- //assign rows, columns to correct arduino pins
- const int row[6] = {
- 16,14,13,7,9,11};
- const int col[6] = {
- 5,15,12,6,8,10};
- //output pins
- const int vibrationPin = 4;
- const int speakerPin = 3;
- //input pins
- const int lightSensorPin = 18;
- const int irSensorPin = 19;
- //constant thresholds
- const int DIST_THRES = 140;
- const long ATTENTION_SPAN = 60000; //10min -> millisec
- const long SAD_SPAN = 60000; //1 min
- //variable threshold adjusted with the environment
- int lightTouchThres;
- //2D array representing pixels on LED Matrix
- int pixels[6][6];
- //1: to reference sensor value; 0: enable deskPal
- int DEBUG = 0;
- //flag keepping track of current state
- int currentState;
- //time for calculating state time
- long startNormalTime, startHappyTime, startSadTime;
- //delay count to for bad posture detection
- int badPostureCount;
- void setup(){
- //PINS SETUP
- // iterate over the pins:
- for (int thisPin = 0; thisPin < 6; thisPin++) {
- // initialize the output pins:
- pinMode(col[thisPin], OUTPUT);
- pinMode(row[thisPin], OUTPUT);
- }
- pinMode(vibrationPin, OUTPUT);
- pinMode(speakerPin, OUTPUT);
- pinMode(lightSensorPin, INPUT);
- pinMode(irSensorPin, INPUT);
- digitalWrite(vibrationPin, LOW);
- digitalWrite(speakerPin,LOW);
- clearMatrix();
- if(DEBUG == 1){
- Serial.begin(9600); // Set serial out if we want debugging
- }
- currentState = IDLE;
- badPostureCount = 0;
- lightTouchThres = 120;
- }
- void loop(){
- //caluculate the threshold for touch interaction based on environment lighting
- lightTouchThres = analogRead(lightSensorPin)/2;
- //for viewing the sensor value
- if(DEBUG == 1){
- displaySensorValue();
- }
- else{
- //draw emotion on the LED matrix
- displayEmotion();
- //Implementing states and trasitions between different emotions
- if(currentState == IDLE){
- if(analogRead(lightSensorPin) > 170){
- currentState = NORMAL;
- startNormalTime = millis();
- }
- }
- else{
- if(currentState == NORMAL){
- long currentTime = millis();
- long normalDuration = currentTime - startNormalTime;
- //if normal for longer than 10 min...
- if(normalDuration > ATTENTION_SPAN){
- currentState = BORED;
- }
- //if normal for longer than 20 min...
- else if(normalDuration > 1200000 && analogRead(lightSensorPin) < 100){
- currentState = IDLE;
- }
- //react on touch
- if(analogRead(lightSensorPin) < lightTouchThres){
- animateLove();
- currentState = HAPPY;
- startHappyTime = millis();
- }
- }
- else if(currentState == BORED){
- //react on touch
- if(analogRead(lightSensorPin) < lightTouchThres){
- animateLove();
- currentState = HAPPY;
- startHappyTime = millis();
- }
- }
- else if(currentState == HAPPY){
- long currentTime = millis();
- long happyDuration = currentTime - startHappyTime;
- //if happy for longer than 10 min...
- if(happyDuration > ATTENTION_SPAN){
- currentState = NORMAL;
- startNormalTime = millis();
- }
- if(analogRead(lightSensorPin) < lightTouchThres){
- animateLove();
- currentState = HAPPY;
- startHappyTime = millis();
- }
- }
- else if(currentState == SAD){
- long currentTime = millis();
- long sadDuration = currentTime - startSadTime;
- if(analogRead(lightSensorPin) < lightTouchThres){
- animateLove();
- currentState = HAPPY;
- startHappyTime = millis();
- }
- //if(sadDuration > SAD_SPAN){
- //if the user retains reasonable posture...
- if(analogRead(irSensorPin) < DIST_THRES){
- currentState = NORMAL;
- startNormalTime = millis();
- }
- //}
- }
- //for every states except IDLE, always react to irSensor value < check if user's head is too close to table...
- if(analogRead(irSensorPin) > DIST_THRES){
- badPostureCount += 1;
- if(badPostureCount == 5){
- animateMad();
- currentState = SAD;
- startSadTime = millis();
- badPostureCount = 0;
- }
- }
- else{
- badPostureCount = 0;
- }
- }
- }
- }
- //function that call relative display functions according to currentState
- void displayEmotion(){
- switch(currentState){
- case IDLE:
- animateIdle();
- break;
- case NORMAL:
- animateNormal();
- break;
- case HAPPY:
- animateHappy();
- break;
- case BORED:
- animateBored();
- break;
- case SAD:
- animateSad();
- break;
- }
- }
- //function for viewing values on the serial monitor
- void displaySensorValue(){
- Serial.println("light");
- Serial.println(analogRead(lightSensorPin));
- Serial.println("IR");
- Serial.println(analogRead(irSensorPin));
- delay(1000);
- }
- //function that generates beep
- void beep(int speakerPin){
- analogWrite(speakerPin, 20);
- delay(100);
- analogWrite(speakerPin, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement