Advertisement
Guest User

Untitled

a guest
Dec 13th, 2010
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. /*
  2. *DeskPal - Persuasive Desktop Companion
  3. *Making Things Interactive Fall 2010 final project
  4. *Author: Clifton Lin
  5. *Date: NOV 2010
  6. *Related
  7. */
  8.  
  9. #define IDLE 1
  10. #define NORMAL 2
  11. #define HAPPY 3
  12. #define BORED 4
  13. #define SAD 5
  14.  
  15. //assign rows, columns to correct arduino pins
  16. const int row[6] = {
  17. 16,14,13,7,9,11};
  18.  
  19. const int col[6] = {
  20. 5,15,12,6,8,10};
  21.  
  22. //output pins
  23. const int vibrationPin = 4;
  24. const int speakerPin = 3;
  25.  
  26. //input pins
  27. const int lightSensorPin = 18;
  28. const int irSensorPin = 19;
  29.  
  30. //constant thresholds
  31. const int DIST_THRES = 140;
  32. const long ATTENTION_SPAN = 60000; //10min -> millisec
  33. const long SAD_SPAN = 60000; //1 min
  34.  
  35. //variable threshold adjusted with the environment
  36. int lightTouchThres;
  37.  
  38. //2D array representing pixels on LED Matrix
  39. int pixels[6][6];
  40.  
  41. //1: to reference sensor value; 0: enable deskPal
  42. int DEBUG = 0;
  43.  
  44. //flag keepping track of current state
  45. int currentState;
  46.  
  47. //time for calculating state time
  48. long startNormalTime, startHappyTime, startSadTime;
  49.  
  50. //delay count to for bad posture detection
  51. int badPostureCount;
  52.  
  53. void setup(){
  54.  
  55. //PINS SETUP
  56. // iterate over the pins:
  57. for (int thisPin = 0; thisPin < 6; thisPin++) {
  58. // initialize the output pins:
  59. pinMode(col[thisPin], OUTPUT);
  60. pinMode(row[thisPin], OUTPUT);
  61. }
  62. pinMode(vibrationPin, OUTPUT);
  63. pinMode(speakerPin, OUTPUT);
  64. pinMode(lightSensorPin, INPUT);
  65. pinMode(irSensorPin, INPUT);
  66.  
  67. digitalWrite(vibrationPin, LOW);
  68. digitalWrite(speakerPin,LOW);
  69.  
  70. clearMatrix();
  71.  
  72. if(DEBUG == 1){
  73. Serial.begin(9600); // Set serial out if we want debugging
  74. }
  75.  
  76. currentState = IDLE;
  77. badPostureCount = 0;
  78. lightTouchThres = 120;
  79. }
  80.  
  81.  
  82. void loop(){
  83.  
  84. //caluculate the threshold for touch interaction based on environment lighting
  85. lightTouchThres = analogRead(lightSensorPin)/2;
  86.  
  87. //for viewing the sensor value
  88. if(DEBUG == 1){
  89. displaySensorValue();
  90. }
  91. else{
  92.  
  93. //draw emotion on the LED matrix
  94. displayEmotion();
  95.  
  96. //Implementing states and trasitions between different emotions
  97. if(currentState == IDLE){
  98. if(analogRead(lightSensorPin) > 170){
  99. currentState = NORMAL;
  100. startNormalTime = millis();
  101. }
  102. }
  103. else{
  104.  
  105. if(currentState == NORMAL){
  106. long currentTime = millis();
  107. long normalDuration = currentTime - startNormalTime;
  108.  
  109. //if normal for longer than 10 min...
  110. if(normalDuration > ATTENTION_SPAN){
  111. currentState = BORED;
  112. }
  113. //if normal for longer than 20 min...
  114. else if(normalDuration > 1200000 && analogRead(lightSensorPin) < 100){
  115. currentState = IDLE;
  116. }
  117. //react on touch
  118. if(analogRead(lightSensorPin) < lightTouchThres){
  119. animateLove();
  120. currentState = HAPPY;
  121. startHappyTime = millis();
  122. }
  123. }
  124. else if(currentState == BORED){
  125. //react on touch
  126. if(analogRead(lightSensorPin) < lightTouchThres){
  127. animateLove();
  128. currentState = HAPPY;
  129. startHappyTime = millis();
  130. }
  131. }
  132. else if(currentState == HAPPY){
  133. long currentTime = millis();
  134. long happyDuration = currentTime - startHappyTime;
  135. //if happy for longer than 10 min...
  136. if(happyDuration > ATTENTION_SPAN){
  137. currentState = NORMAL;
  138. startNormalTime = millis();
  139. }
  140.  
  141. if(analogRead(lightSensorPin) < lightTouchThres){
  142. animateLove();
  143. currentState = HAPPY;
  144. startHappyTime = millis();
  145. }
  146. }
  147. else if(currentState == SAD){
  148. long currentTime = millis();
  149. long sadDuration = currentTime - startSadTime;
  150.  
  151. if(analogRead(lightSensorPin) < lightTouchThres){
  152. animateLove();
  153. currentState = HAPPY;
  154. startHappyTime = millis();
  155. }
  156. //if(sadDuration > SAD_SPAN){
  157. //if the user retains reasonable posture...
  158. if(analogRead(irSensorPin) < DIST_THRES){
  159. currentState = NORMAL;
  160. startNormalTime = millis();
  161. }
  162. //}
  163. }
  164.  
  165. //for every states except IDLE, always react to irSensor value < check if user's head is too close to table...
  166. if(analogRead(irSensorPin) > DIST_THRES){
  167. badPostureCount += 1;
  168.  
  169. if(badPostureCount == 5){
  170. animateMad();
  171. currentState = SAD;
  172. startSadTime = millis();
  173.  
  174. badPostureCount = 0;
  175. }
  176. }
  177. else{
  178. badPostureCount = 0;
  179. }
  180. }
  181. }
  182. }
  183.  
  184. //function that call relative display functions according to currentState
  185. void displayEmotion(){
  186. switch(currentState){
  187. case IDLE:
  188. animateIdle();
  189. break;
  190. case NORMAL:
  191. animateNormal();
  192. break;
  193. case HAPPY:
  194. animateHappy();
  195. break;
  196. case BORED:
  197. animateBored();
  198. break;
  199. case SAD:
  200. animateSad();
  201. break;
  202. }
  203. }
  204.  
  205. //function for viewing values on the serial monitor
  206. void displaySensorValue(){
  207.  
  208. Serial.println("light");
  209. Serial.println(analogRead(lightSensorPin));
  210. Serial.println("IR");
  211. Serial.println(analogRead(irSensorPin));
  212. delay(1000);
  213.  
  214. }
  215.  
  216. //function that generates beep
  217. void beep(int speakerPin){
  218. analogWrite(speakerPin, 20);
  219. delay(100);
  220. analogWrite(speakerPin, 0);
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement